3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * 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) { throw new Exception('Votes count have to be greater than zero!'); } // 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 $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/EeKjt
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/EeKjt
function name:  __construct
number of ops:  4
compiled vars:  !0 = $slots
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
   35     1        ASSIGN_OBJ                                               '_maxSlots'
          2        OP_DATA                                                  !0
   36     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/EeKjt
function name:  setPartyVotes
number of ops:  6
compiled vars:  !0 = $partyName, !1 = $votes
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   47     2        FETCH_OBJ_W                                      $2      '_votes'
          3        ASSIGN_DIM                                               $2, !0
          4        OP_DATA                                                  !1
   48     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 = 11
Branch analysis from position: 7
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 77) Position 1 = 14, Position 2 = 32
Branch analysis from position: 14
2 jumps found. (Code = 78) Position 1 = 15, Position 2 = 32
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 57
Branch analysis from position: 57
2 jumps found. (Code = 44) Position 1 = 60, Position 2 = 48
Branch analysis from position: 60
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 48
2 jumps found. (Code = 44) Position 1 = 60, Position 2 = 48
Branch analysis from position: 60
Branch analysis from position: 48
Branch analysis from position: 32
filename:       /in/EeKjt
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
-------------------------------------------------------------------------------------
   58     0  E >   INIT_FCALL                                               'array_sum'
          1        FETCH_OBJ_R                                      ~6      '_votes'
          2        SEND_VAL                                                 ~6
          3        DO_ICALL                                         $7      
          4        ASSIGN                                                   !0, $7
   59     5        IS_SMALLER_OR_EQUAL                                      !0, 0
          6      > JMPZ                                                     ~9, ->11
   60     7    >   NEW                                              $10     'Exception'
          8        SEND_VAL_EX                                              'Votes+count+have+to+be+greater+than+zero%21'
          9        DO_FCALL                                      0          
         10      > THROW                                         0          $10
   63    11    >   ASSIGN                                                   !1, <array>
   64    12        FETCH_OBJ_R                                      ~13     '_votes'
         13      > FE_RESET_R                                       $14     ~13, ->32
         14    > > FE_FETCH_R                                       ~15     $14, !2, ->32
         15    >   ASSIGN                                                   !3, ~15
   65    16        FETCH_OBJ_R                                      ~17     '_maxSlots'
         17        MUL                                              ~18     !2, ~17
         18        DIV                                              ~19     ~18, !0
         19        ASSIGN                                                   !4, ~19
   66    20        INIT_FCALL                                               'floor'
         21        SEND_VAR                                                 !4
         22        DO_ICALL                                         $23     
         23        FETCH_OBJ_W                                      $21     '_slots'
         24        ASSIGN_DIM                                               $21, !3
         25        OP_DATA                                                  $23
   67    26        FETCH_OBJ_R                                      ~25     '_slots'
         27        FETCH_DIM_R                                      ~26     ~25, !3
         28        SUB                                              ~27     !4, ~26
         29        ASSIGN_DIM                                               !1, !3
         30        OP_DATA                                                  ~27
   64    31      > JMP                                                      ->14
         32    >   FE_FREE                                                  $14
   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/EeKjt
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:
156.7 ms | 1408 KiB | 25 Q