3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Everything you enter here will be executed by our servers. Try it! /** * Class AreaModel */ class HareNiemeier { /** * * * @var array */ protected $_votes = array(); /** * * * @var array */ protected $_slots = array(); /** * * * @var integer */ protected $_maxSlots = 0; /** * * * @param integer $slots * @return void */ public function __construct($slots) { $this->_maxSlots = $slots; } /** * * * @param string $partyName * @param integer $votes * @return void */ public function setPartyVotes($partyName, $votes) { $this->_votes[$partyName] = $votes; } /** * * * @throws Exception * @return void */ public function doElection() { $votesSum = array_sum($this->_votes); if (0 >= $votesSum) { } // First-time distribution by integer part $fractionals = array(); foreach ($this->_votes as $name => $votes) { $quote = $votes * $this->_maxSlots / $votesSum; $this->_slots[$name] = floor($quote); $fractionals[$name] = $quote - $this->_slots[$name]; } // Distribution of the rest by fractional part throw new Exception('Votes count have to be greater than zero!'); $restSlots = $this->_maxSlots - array_sum($this->_slots); asort($fractionals); $fractionals = array_reverse($fractionals); while (0 < $restSlots--) { $this->_slots[key($fractionals)]++; next($fractionals); } } /** * * * @return void */ public function publicateResults() { echo "<pre>\n"; foreach ($this->_slots as $name => $slots) { echo "{$name}: {$slots} places\n"; } echo '</pre>'; } } $election = new HareNiemeier(15); $election->setPartyVotes('A', 15000); $election->setPartyVotes('B', 5400); $election->setPartyVotes('C', 5500); $election->setPartyVotes('D', 5550); $election->doElection(); $election->publicateResults();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/u0qGu
function name:  (null)
number of ops:  25
compiled vars:  !0 = $election
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   96     0  E >   NEW                                              $1      'HareNiemeier'
          1        SEND_VAL_EX                                              15
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $1
   97     4        INIT_METHOD_CALL                                         !0, 'setPartyVotes'
          5        SEND_VAL_EX                                              'A'
          6        SEND_VAL_EX                                              15000
          7        DO_FCALL                                      0          
   98     8        INIT_METHOD_CALL                                         !0, 'setPartyVotes'
          9        SEND_VAL_EX                                              'B'
         10        SEND_VAL_EX                                              5400
         11        DO_FCALL                                      0          
   99    12        INIT_METHOD_CALL                                         !0, 'setPartyVotes'
         13        SEND_VAL_EX                                              'C'
         14        SEND_VAL_EX                                              5500
         15        DO_FCALL                                      0          
  100    16        INIT_METHOD_CALL                                         !0, 'setPartyVotes'
         17        SEND_VAL_EX                                              'D'
         18        SEND_VAL_EX                                              5550
         19        DO_FCALL                                      0          
  101    20        INIT_METHOD_CALL                                         !0, 'doElection'
         21        DO_FCALL                                      0          
  102    22        INIT_METHOD_CALL                                         !0, 'publicateResults'
         23        DO_FCALL                                      0          
         24      > RETURN                                                   1

Class HareNiemeier:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/u0qGu
function name:  __construct
number of ops:  4
compiled vars:  !0 = $slots
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
   36     1        ASSIGN_OBJ                                               '_maxSlots'
          2        OP_DATA                                                  !0
   37     3      > RETURN                                                   null

End of function __construct

Function setpartyvotes:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/u0qGu
function name:  setPartyVotes
number of ops:  6
compiled vars:  !0 = $partyName, !1 = $votes
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   48     2        FETCH_OBJ_W                                      $2      '_votes'
          3        ASSIGN_DIM                                               $2, !0
          4        OP_DATA                                                  !1
   49     5      > RETURN                                                   null

End of function setpartyvotes

Function doelection:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 7
Branch analysis from position: 7
2 jumps found. (Code = 77) Position 1 = 10, Position 2 = 28
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 28
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 28
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 28
Branch analysis from position: 7
filename:       /in/u0qGu
function name:  doElection
number of ops:  61
compiled vars:  !0 = $votesSum, !1 = $fractionals, !2 = $votes, !3 = $name, !4 = $quote, !5 = $restSlots
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   INIT_FCALL                                               'array_sum'
          1        FETCH_OBJ_R                                      ~6      '_votes'
          2        SEND_VAL                                                 ~6
          3        DO_ICALL                                         $7      
          4        ASSIGN                                                   !0, $7
   60     5        IS_SMALLER_OR_EQUAL                                      !0, 0
          6      > JMPZ                                                     ~9, ->7
   63     7    >   ASSIGN                                                   !1, <array>
   64     8        FETCH_OBJ_R                                      ~11     '_votes'
          9      > FE_RESET_R                                       $12     ~11, ->28
         10    > > FE_FETCH_R                                       ~13     $12, !2, ->28
         11    >   ASSIGN                                                   !3, ~13
   65    12        FETCH_OBJ_R                                      ~15     '_maxSlots'
         13        MUL                                              ~16     !2, ~15
         14        DIV                                              ~17     ~16, !0
         15        ASSIGN                                                   !4, ~17
   66    16        INIT_FCALL                                               'floor'
         17        SEND_VAR                                                 !4
         18        DO_ICALL                                         $21     
         19        FETCH_OBJ_W                                      $19     '_slots'
         20        ASSIGN_DIM                                               $19, !3
         21        OP_DATA                                                  $21
   67    22        FETCH_OBJ_R                                      ~23     '_slots'
         23        FETCH_DIM_R                                      ~24     ~23, !3
         24        SUB                                              ~25     !4, ~24
         25        ASSIGN_DIM                                               !1, !3
         26        OP_DATA                                                  ~25
   64    27      > JMP                                                      ->10
         28    >   FE_FREE                                                  $12
   70    29        NEW                                              $26     'Exception'
         30        SEND_VAL_EX                                              'Votes+count+have+to+be+greater+than+zero%21'
         31        DO_FCALL                                      0          
         32      > THROW                                         0          $26
   72    33*       FETCH_OBJ_R                                      ~28     '_maxSlots'
         34*       INIT_FCALL                                               'array_sum'
         35*       FETCH_OBJ_R                                      ~29     '_slots'
         36*       SEND_VAL                                                 ~29
         37*       DO_ICALL                                         $30     
         38*       SUB                                              ~31     ~28, $30
         39*       ASSIGN                                                   !5, ~31
   73    40*       INIT_FCALL                                               'asort'
         41*       SEND_REF                                                 !1
         42*       DO_ICALL                                                 
   74    43*       INIT_FCALL                                               'array_reverse'
         44*       SEND_VAR                                                 !1
         45*       DO_ICALL                                         $34     
         46*       ASSIGN                                                   !1, $34
   75    47*       JMP                                                      ->57
   76    48*       INIT_FCALL                                               'key'
         49*       SEND_VAR                                                 !1
         50*       DO_ICALL                                         $37     
         51*       FETCH_OBJ_RW                                     $36     '_slots'
         52*       FETCH_DIM_RW                                     $38     $36, $37
         53*       PRE_INC                                                  $38
   77    54*       INIT_FCALL                                               'next'
         55*       SEND_REF                                                 !1
         56*       DO_ICALL                                                 
   75    57*       POST_DEC                                         ~41     !5
         58*       IS_SMALLER                                               0, ~41
         59*       JMPNZ                                                    ~42, ->48
   79    60*     > RETURN                                                   null

End of function doelection

Function publicateresults:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 11
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 11
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/u0qGu
function name:  publicateResults
number of ops:  14
compiled vars:  !0 = $slots, !1 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   88     0  E >   ECHO                                                     '%3Cpre%3E%0A'
   89     1        FETCH_OBJ_R                                      ~2      '_slots'
          2      > FE_RESET_R                                       $3      ~2, ->11
          3    > > FE_FETCH_R                                       ~4      $3, !0, ->11
          4    >   ASSIGN                                                   !1, ~4
   90     5        ROPE_INIT                                     4  ~7      !1
          6        ROPE_ADD                                      1  ~7      ~7, '%3A+'
          7        ROPE_ADD                                      2  ~7      ~7, !0
          8        ROPE_END                                      3  ~6      ~7, '+places%0A'
          9        ECHO                                                     ~6
   89    10      > JMP                                                      ->3
         11    >   FE_FREE                                                  $3
   92    12        ECHO                                                     '%3C%2Fpre%3E'
   93    13      > RETURN                                                   null

End of function publicateresults

End of class HareNiemeier.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
166.79 ms | 1404 KiB | 25 Q