3v4l.org

run code in 500+ PHP versions simultaneously
<?php interface ICupboard { public function getNextCup(): Cup; public function haveCups(): bool; } class InfiniteCupBoard implements ICupboard { public function getNextCup(): Cup { return Cup::createEmpty(); } public function haveCups(): bool { return true; } } interface ITap { public function getWater(): Liquid; } class InfiniteTap implements ITap { private int $waterVolumePerReuqest; public function __construct(int $waterVolumePerReuqest) { $this->waterVolumePerReuqest = $waterVolumePerReuqest; } public function getWater(): Liquid { return Liquid::createVolume($this->waterVolumePerReuqest); } } class Cup { private Liquid $liquid; private int $volume; private function __construct(Liquid $liquid, int $volume = 300) { $this->liquid = $liquid; $this->volume = $volume; } public static function createEmpty(): self { return new self(Liquid::createEmpty()); } public function fill(Liquid $liquid): void { if (false === $this->canAddLiquid($liquid)) { throw new DomainException( sprintf( 'Cannot hold liquid volume. Expected volume: %s. Current volume: %s. Provided volume: %s.', $this->volume, $this->liquid->getVolume(), $liquid->getVolume() ) ); } $this->liquid = $this->liquid->add($liquid); } public function canAddLiquid(Liquid $liquid): bool { return $this->volume >= $this->liquid->getVolume() + $liquid->getVolume(); } } class Liquid { private int $volume; private function __construct(int $volume) { $this->volume = $volume; } public static function createEmpty(): self { return new self(0); } public static function createVolume(int $volume): self { return new self($volume); } public function add(Liquid $liquid): self { return new self($this->volume + $liquid->volume); } public function getVolume(): int { return $this->volume; } } class FillCupsWithWater { private ITap $tap; private ICupboard $cupboard; public function __construct(ITap $tap, ICupboard $cupboard) { $this->tap = $tap; $this->cupboard = $cupboard; } public function __invoke(int $cupsToFillCount): array { $filledCups = []; while($this->cupboard->haveCups() && $cupsToFillCount) { $cup = $this->cupboard->getNextCup(); $water = $this->tap->getWater(); while($cup->canAddLiquid($water)) { $cup->fill($water); } $filledCups[] = $cup; $cupsToFillCount--; } return $filledCups; } } $fillCupsWithWater = new FillCupsWithWater(new InfiniteTap(14), new InfiniteCupBoard); print_r($fillCupsWithWater(4));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AD5bt
function name:  (null)
number of ops:  19
compiled vars:  !0 = $fillCupsWithWater
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    8     0  E >   DECLARE_CLASS                                                'infinitecupboard'
   23     1        DECLARE_CLASS                                                'infinitetap'
  121     2        NEW                                                  $1      'FillCupsWithWater'
          3        NEW                                                  $2      'InfiniteTap'
          4        SEND_VAL_EX                                                  14
          5        DO_FCALL                                          0          
          6        SEND_VAR_NO_REF_EX                                           $2
          7        NEW                                                  $4      'InfiniteCupBoard'
          8        DO_FCALL                                          0          
          9        SEND_VAR_NO_REF_EX                                           $4
         10        DO_FCALL                                          0          
         11        ASSIGN                                                       !0, $1
  123    12        INIT_FCALL                                                   'print_r'
         13        INIT_DYNAMIC_CALL                                            !0
         14        SEND_VAL_EX                                                  4
         15        DO_FCALL                                          0  $8      
         16        SEND_VAR                                                     $8
         17        DO_ICALL                                                     
         18      > RETURN                                                       1

Class ICupboard:
Function getnextcup:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AD5bt
function name:  getNextCup
number of ops:  2
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    4     0  E >   VERIFY_RETURN_TYPE                                           
          1      > RETURN                                                       null

End of function getnextcup

Function havecups:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AD5bt
function name:  haveCups
number of ops:  2
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    5     0  E >   VERIFY_RETURN_TYPE                                           
          1      > RETURN                                                       null

End of function havecups

End of class ICupboard.

Class InfiniteCupBoard:
Function getnextcup:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AD5bt
function name:  getNextCup
number of ops:  6
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   10     0  E >   INIT_STATIC_METHOD_CALL                                      'Cup', 'createEmpty'
          1        DO_FCALL                                          0  $0      
          2        VERIFY_RETURN_TYPE                                           $0
          3      > RETURN                                                       $0
   11     4*       VERIFY_RETURN_TYPE                                           
          5*     > RETURN                                                       null

End of function getnextcup

Function havecups:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AD5bt
function name:  haveCups
number of ops:  3
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   14     0  E > > RETURN                                                       <true>
   15     1*       VERIFY_RETURN_TYPE                                           
          2*     > RETURN                                                       null

End of function havecups

End of class InfiniteCupBoard.

Class ITap:
Function getwater:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AD5bt
function name:  getWater
number of ops:  2
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   19     0  E >   VERIFY_RETURN_TYPE                                           
          1      > RETURN                                                       null

End of function getwater

End of class ITap.

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

End of function __construct

Function getwater:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AD5bt
function name:  getWater
number of ops:  9
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   31     0  E >   INIT_STATIC_METHOD_CALL                                      'Liquid', 'createVolume'
          1        CHECK_FUNC_ARG                                               
          2        FETCH_OBJ_FUNC_ARG                                   $0      'waterVolumePerReuqest'
          3        SEND_FUNC_ARG                                                $0
          4        DO_FCALL                                          0  $1      
          5        VERIFY_RETURN_TYPE                                           $1
          6      > RETURN                                                       $1
   32     7*       VERIFY_RETURN_TYPE                                           
          8*     > RETURN                                                       null

End of function getwater

End of class InfiniteTap.

Class Cup:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AD5bt
function name:  __construct
number of ops:  7
compiled vars:  !0 = $liquid, !1 = $volume
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   39     0  E >   RECV                                                 !0      
          1        RECV_INIT                                            !1      300
   40     2        ASSIGN_OBJ                                                   'liquid'
          3        OP_DATA                                                      !0
   41     4        ASSIGN_OBJ                                                   'volume'
          5        OP_DATA                                                      !1
   42     6      > RETURN                                                       null

End of function __construct

Function createempty:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AD5bt
function name:  createEmpty
number of ops:  9
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   45     0  E >   NEW                              self                $0      
          1        INIT_STATIC_METHOD_CALL                                      'Liquid', 'createEmpty'
          2        DO_FCALL                                          0  $1      
          3        SEND_VAR_NO_REF_EX                                           $1
          4        DO_FCALL                                          0          
          5        VERIFY_RETURN_TYPE                                           $0
          6      > RETURN                                                       $0
   46     7*       VERIFY_RETURN_TYPE                                           
          8*     > RETURN                                                       null

End of function createempty

Function fill:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 23
Branch analysis from position: 6
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AD5bt
function name:  fill
number of ops:  30
compiled vars:  !0 = $liquid
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   48     0  E >   RECV                                                 !0      
   49     1        INIT_METHOD_CALL                                             'canAddLiquid'
          2        SEND_VAR_EX                                                  !0
          3        DO_FCALL                                          0  $1      
          4        TYPE_CHECK                                        4          $1
          5      > JMPZ                                                         ~2, ->23
   50     6    >   NEW                                                  $3      'DomainException'
   53     7        FETCH_OBJ_R                                          ~4      'volume'
   54     8        FETCH_OBJ_R                                          ~5      'liquid'
          9        INIT_METHOD_CALL                                             ~5, 'getVolume'
         10        DO_FCALL                                          0  $6      
   55    11        INIT_METHOD_CALL                                             !0, 'getVolume'
         12        DO_FCALL                                          0  $7      
         13        ROPE_INIT                                         7  ~9      'Cannot+hold+liquid+volume.+Expected+volume%3A+'
         14        ROPE_ADD                                          1  ~9      ~9, ~4
         15        ROPE_ADD                                          2  ~9      ~9, '.+Current+volume%3A+'
         16        ROPE_ADD                                          3  ~9      ~9, $6
         17        ROPE_ADD                                          4  ~9      ~9, '.+Provided+volume%3A+'
         18        ROPE_ADD                                          5  ~9      ~9, $7
         19        ROPE_END                                          6  ~8      ~9, '.'
         20        SEND_VAL_EX                                                  ~8
   50    21        DO_FCALL                                          0          
   55    22      > THROW                                             0          $3
   60    23    >   FETCH_OBJ_R                                          ~15     'liquid'
         24        INIT_METHOD_CALL                                             ~15, 'add'
         25        SEND_VAR_EX                                                  !0
         26        DO_FCALL                                          0  $16     
         27        ASSIGN_OBJ                                                   'liquid'
         28        OP_DATA                                                      $16
   61    29      > RETURN                                                       null

End of function fill

Function canaddliquid:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AD5bt
function name:  canAddLiquid
number of ops:  13
compiled vars:  !0 = $liquid
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   63     0  E >   RECV                                                 !0      
   64     1        FETCH_OBJ_R                                          ~1      'volume'
          2        FETCH_OBJ_R                                          ~2      'liquid'
          3        INIT_METHOD_CALL                                             ~2, 'getVolume'
          4        DO_FCALL                                          0  $3      
          5        INIT_METHOD_CALL                                             !0, 'getVolume'
          6        DO_FCALL                                          0  $4      
          7        ADD                                                  ~5      $3, $4
          8        IS_SMALLER_OR_EQUAL                                  ~6      ~5, ~1
          9        VERIFY_RETURN_TYPE                                           ~6
         10      > RETURN                                                       ~6
   65    11*       VERIFY_RETURN_TYPE                                           
         12*     > RETURN                                                       null

End of function canaddliquid

End of class Cup.

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

End of function __construct

Function createempty:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AD5bt
function name:  createEmpty
number of ops:  7
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   76     0  E >   NEW                              self                $0      
          1        SEND_VAL_EX                                                  0
          2        DO_FCALL                                          0          
          3        VERIFY_RETURN_TYPE                                           $0
          4      > RETURN                                                       $0
   77     5*       VERIFY_RETURN_TYPE                                           
          6*     > RETURN                                                       null

End of function createempty

Function createvolume:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AD5bt
function name:  createVolume
number of ops:  8
compiled vars:  !0 = $volume
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   79     0  E >   RECV                                                 !0      
   80     1        NEW                              self                $1      
          2        SEND_VAR_EX                                                  !0
          3        DO_FCALL                                          0          
          4        VERIFY_RETURN_TYPE                                           $1
          5      > RETURN                                                       $1
   81     6*       VERIFY_RETURN_TYPE                                           
          7*     > RETURN                                                       null

End of function createvolume

Function add:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AD5bt
function name:  add
number of ops:  11
compiled vars:  !0 = $liquid
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   83     0  E >   RECV                                                 !0      
   84     1        NEW                              self                $1      
          2        FETCH_OBJ_R                                          ~2      'volume'
          3        FETCH_OBJ_R                                          ~3      !0, 'volume'
          4        ADD                                                  ~4      ~2, ~3
          5        SEND_VAL_EX                                                  ~4
          6        DO_FCALL                                          0          
          7        VERIFY_RETURN_TYPE                                           $1
          8      > RETURN                                                       $1
   85     9*       VERIFY_RETURN_TYPE                                           
         10*     > RETURN                                                       null

End of function add

Function getvolume:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AD5bt
function name:  getVolume
number of ops:  5
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   88     0  E >   FETCH_OBJ_R                                          ~0      'volume'
          1        VERIFY_RETURN_TYPE                                           ~0
          2      > RETURN                                                       ~0
   89     3*       VERIFY_RETURN_TYPE                                           
          4*     > RETURN                                                       null

End of function getvolume

End of class Liquid.

Class FillCupsWithWater:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AD5bt
function name:  __construct
number of ops:  7
compiled vars:  !0 = $tap, !1 = $cupboard
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   96     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   97     2        ASSIGN_OBJ                                                   'tap'
          3        OP_DATA                                                      !0
   98     4        ASSIGN_OBJ                                                   'cupboard'
          5        OP_DATA                                                      !1
   99     6      > RETURN                                                       null

End of function __construct

Function __invoke:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
2 jumps found. (Code = 46) Position 1 = 26, Position 2 = 27
Branch analysis from position: 26
2 jumps found. (Code = 44) Position 1 = 28, Position 2 = 3
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 19, Position 2 = 12
Branch analysis from position: 19
2 jumps found. (Code = 46) Position 1 = 26, Position 2 = 27
Branch analysis from position: 26
Branch analysis from position: 27
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 19, Position 2 = 12
Branch analysis from position: 19
Branch analysis from position: 12
Branch analysis from position: 27
filename:       /in/AD5bt
function name:  __invoke
number of ops:  32
compiled vars:  !0 = $cupsToFillCount, !1 = $filledCups, !2 = $cup, !3 = $water
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
  101     0  E >   RECV                                                 !0      
  102     1        ASSIGN                                                       !1, <array>
  104     2      > JMP                                                          ->22
  105     3    >   FETCH_OBJ_R                                          ~5      'cupboard'
          4        INIT_METHOD_CALL                                             ~5, 'getNextCup'
          5        DO_FCALL                                          0  $6      
          6        ASSIGN                                                       !2, $6
  106     7        FETCH_OBJ_R                                          ~8      'tap'
          8        INIT_METHOD_CALL                                             ~8, 'getWater'
          9        DO_FCALL                                          0  $9      
         10        ASSIGN                                                       !3, $9
  108    11      > JMP                                                          ->15
  109    12    >   INIT_METHOD_CALL                                             !2, 'fill'
         13        SEND_VAR_EX                                                  !3
         14        DO_FCALL                                          0          
  108    15    >   INIT_METHOD_CALL                                             !2, 'canAddLiquid'
         16        SEND_VAR_EX                                                  !3
         17        DO_FCALL                                          0  $12     
         18      > JMPNZ                                                        $12, ->12
  112    19    >   ASSIGN_DIM                                                   !1
         20        OP_DATA                                                      !2
  114    21        PRE_DEC                                                      !0
  104    22    >   FETCH_OBJ_R                                          ~15     'cupboard'
         23        INIT_METHOD_CALL                                             ~15, 'haveCups'
         24        DO_FCALL                                          0  $16     
         25      > JMPZ_EX                                              ~17     $16, ->27
         26    >   BOOL                                                 ~17     !0
         27    > > JMPNZ                                                        ~17, ->3
  117    28    >   VERIFY_RETURN_TYPE                                           !1
         29      > RETURN                                                       !1
  118    30*       VERIFY_RETURN_TYPE                                           
         31*     > RETURN                                                       null

End of function __invoke

End of class FillCupsWithWater.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
163.51 ms | 2146 KiB | 16 Q