3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Atom; interface CollectionInterface extends \IteratorAggregate { /** * * @param \Atom\AggregatorInterface $aggregator * @return mixed */ function aggregate(\Atom\AggregatorInterface $aggregator); /** * * @param \Atom\EnumeratorInterface $enumerator * @return \Atom\CollectionInterface */ function enumerate(\Atom\EnumeratorInterface $enumerator); /** * * @return array */ function getArray(); } class Collection implements \Atom\CollectionInterface { /** * * @param mixed $iterable * @return \Atom\Collection */ public static function create($iterable) { if (is_array($iterable)) { return new \Atom\Collection(new \ArrayIterator($iterable)); } return new \Atom\Collection(new \IteratorIterator($iterable)); } /** * * @var \Iterator */ private $iterator; /** * * @param \Iterator $iterator */ public function __construct(\Iterator $iterator) { $this->iterator = $iterator; } /** * * @param \Atom\AggregatorInterface $aggregator * @return mixed */ public function aggregate(\Atom\AggregatorInterface $aggregator) { return $aggregator->aggregate($this); } /** * * @param \Atom\EnumeratorInterface $enumerator * @return \Atom\CollectionInterface */ public function enumerate(\Atom\EnumeratorInterface $enumerator) { return self::create($enumerator->enumerate($this)); } /** * * @return \Iterator */ public function getIterator() { return $this->iterator; } /** * * @return array */ public function getArray() { return iterator_to_array($this->iterator); } } interface EnumeratorInterface { /** * * @param \Atom\CollectionInterface $collection * @return mixed */ function enumerate(\Atom\CollectionInterface $collection); } interface AggregatorInterface { /** * * @param \Atom\CollectionInterface $collection * @return mixed */ function aggregate(\Atom\CollectionInterface $collection); } class MapEnumerator implements \Atom\EnumeratorInterface { /** * * @var callable */ private $function; /** * * @param callable $function */ public function __construct(callable $function) { $this->function = $function; } /** * * @param \Atom\CollectionInterface $collection * @return mixed */ public function enumerate(\Atom\CollectionInterface $collection) { $function = $this->function; foreach ($collection as $key => $value) { yield $key => $function($value, $key); } } } class FilterEnumerator implements \Atom\EnumeratorInterface { /** * * @var callable */ private $function; /** * * @param callable $function */ public function __construct(callable $function) { $this->function = $function; } /** * * @param \Atom\CollectionInterface $collection * @return mixed */ public function enumerate(\Atom\CollectionInterface $collection) { $function = $this->function; foreach ($collection as $key => $value) { if ($function($value, $key)) { yield $key => $value; } } } } class SumAggregator implements \Atom\AggregatorInterface { /** * * @param \Atom\CollectionInterface $collection * @return mixed */ public function aggregate(\Atom\CollectionInterface $collection) { return array_sum($collection->getArray()); } } $sum = \Atom\Collection::create([1, 2, 3, 4, 5])->enumerate(new \Atom\MapEnumerator(function ($value) { return $value * 3; }))->enumerate(new \Atom\FilterEnumerator(function ($value) { return $value % 2 == 0; }))->aggregate(new \Atom\SumAggregator()); var_dump($sum);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/q93Ti
function name:  (null)
number of ops:  32
compiled vars:  !0 = $sum
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   DECLARE_CLASS                                            'atom%5Ccollectioninterface'
   30     1        DECLARE_CLASS                                            'atom%5Ccollection'
  125     2        DECLARE_CLASS                                            'atom%5Cmapenumerator'
  158     3        DECLARE_CLASS                                            'atom%5Cfilterenumerator'
  193     4        DECLARE_CLASS                                            'atom%5Csumaggregator'
  208     5        INIT_STATIC_METHOD_CALL                                  'Atom%5CCollection', 'create'
          6        SEND_VAL_EX                                              <array>
          7        DO_FCALL                                      0  $1      
          8        INIT_METHOD_CALL                                         $1, 'enumerate'
          9        NEW                                              $2      'Atom%5CMapEnumerator'
         10        DECLARE_LAMBDA_FUNCTION                                  '%00atom%5C%7Bclosure%7D%2Fin%2Fq93Ti%3A208%245'
  210    11        SEND_VAL_EX                                              ~3
         12        DO_FCALL                                      0          
         13        SEND_VAR_NO_REF_EX                                       $2
         14        DO_FCALL                                      0  $5      
         15        INIT_METHOD_CALL                                         $5, 'enumerate'
         16        NEW                                              $6      'Atom%5CFilterEnumerator'
         17        DECLARE_LAMBDA_FUNCTION                                  '%00atom%5C%7Bclosure%7D%2Fin%2Fq93Ti%3A210%246'
  212    18        SEND_VAL_EX                                              ~7
         19        DO_FCALL                                      0          
         20        SEND_VAR_NO_REF_EX                                       $6
         21        DO_FCALL                                      0  $9      
         22        INIT_METHOD_CALL                                         $9, 'aggregate'
         23        NEW                                              $10     'Atom%5CSumAggregator'
         24        DO_FCALL                                      0          
         25        SEND_VAR_NO_REF_EX                                       $10
         26        DO_FCALL                                      0  $12     
  208    27        ASSIGN                                                   !0, $12
  214    28        INIT_NS_FCALL_BY_NAME                                    'Atom%5Cvar_dump'
         29        SEND_VAR_EX                                              !0
         30        DO_FCALL                                      0          
         31      > RETURN                                                   1

Function %00atom%5C%7Bclosure%7D%2Fin%2Fq93Ti%3A208%245:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/q93Ti
function name:  Atom\{closure}
number of ops:  4
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  208     0  E >   RECV                                             !0      
  209     1        MUL                                              ~1      !0, 3
          2      > RETURN                                                   ~1
  210     3*     > RETURN                                                   null

End of function %00atom%5C%7Bclosure%7D%2Fin%2Fq93Ti%3A208%245

Function %00atom%5C%7Bclosure%7D%2Fin%2Fq93Ti%3A210%246:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/q93Ti
function name:  Atom\{closure}
number of ops:  5
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
          0  E >   RECV                                             !0      
  211     1        MOD                                              ~1      !0, 2
          2        IS_EQUAL                                         ~2      ~1, 0
          3      > RETURN                                                   ~2
  212     4*     > RETURN                                                   null

End of function %00atom%5C%7Bclosure%7D%2Fin%2Fq93Ti%3A210%246

Class Atom\CollectionInterface:
Function aggregate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/q93Ti
function name:  aggregate
number of ops:  2
compiled vars:  !0 = $aggregator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function aggregate

Function enumerate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/q93Ti
function name:  enumerate
number of ops:  2
compiled vars:  !0 = $enumerator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function enumerate

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

End of function getarray

End of class Atom\CollectionInterface.

Class Atom\Collection:
Function create:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 12
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/q93Ti
function name:  create
number of ops:  20
compiled vars:  !0 = $iterable
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   RECV                                             !0      
   40     1        INIT_NS_FCALL_BY_NAME                                    'Atom%5Cis_array'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $1      
          4      > JMPZ                                                     $1, ->12
   41     5    >   NEW                                              $2      'Atom%5CCollection'
          6        NEW                                              $3      'ArrayIterator'
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0          
          9        SEND_VAR_NO_REF_EX                                       $3
         10        DO_FCALL                                      0          
         11      > RETURN                                                   $2
   43    12    >   NEW                                              $6      'Atom%5CCollection'
         13        NEW                                              $7      'IteratorIterator'
         14        SEND_VAR_EX                                              !0
         15        DO_FCALL                                      0          
         16        SEND_VAR_NO_REF_EX                                       $7
         17        DO_FCALL                                      0          
         18      > RETURN                                                   $6
   44    19*     > RETURN                                                   null

End of function create

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

End of function __construct

Function aggregate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/q93Ti
function name:  aggregate
number of ops:  7
compiled vars:  !0 = $aggregator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   66     0  E >   RECV                                             !0      
   68     1        INIT_METHOD_CALL                                         !0, 'aggregate'
          2        FETCH_THIS                                       $1      
          3        SEND_VAR_EX                                              $1
          4        DO_FCALL                                      0  $2      
          5      > RETURN                                                   $2
   69     6*     > RETURN                                                   null

End of function aggregate

Function enumerate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/q93Ti
function name:  enumerate
number of ops:  10
compiled vars:  !0 = $enumerator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   76     0  E >   RECV                                             !0      
   78     1        INIT_STATIC_METHOD_CALL                                  'create'
          2        INIT_METHOD_CALL                                         !0, 'enumerate'
          3        FETCH_THIS                                       $1      
          4        SEND_VAR_EX                                              $1
          5        DO_FCALL                                      0  $2      
          6        SEND_VAR                                                 $2
          7        DO_FCALL                                      0  $3      
          8      > RETURN                                                   $3
   79     9*     > RETURN                                                   null

End of function enumerate

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

End of function getiterator

Function getarray:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/q93Ti
function name:  getArray
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   96     0  E >   INIT_NS_FCALL_BY_NAME                                    'Atom%5Citerator_to_array'
          1        CHECK_FUNC_ARG                                           
          2        FETCH_OBJ_FUNC_ARG                               $0      'iterator'
          3        SEND_FUNC_ARG                                            $0
          4        DO_FCALL                                      0  $1      
          5      > RETURN                                                   $1
   97     6*     > RETURN                                                   null

End of function getarray

End of class Atom\Collection.

Class Atom\EnumeratorInterface:
Function enumerate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/q93Ti
function name:  enumerate
number of ops:  2
compiled vars:  !0 = $collection
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  109     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function enumerate

End of class Atom\EnumeratorInterface.

Class Atom\AggregatorInterface:
Function aggregate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/q93Ti
function name:  aggregate
number of ops:  2
compiled vars:  !0 = $collection
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  121     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function aggregate

End of class Atom\AggregatorInterface.

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

End of function __construct

Function enumerate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 13
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 13
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 13
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 13
filename:       /in/q93Ti
function name:  enumerate
number of ops:  15
compiled vars:  !0 = $collection, !1 = $function, !2 = $value, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  148     0  E >   RECV                                             !0      
          1        GENERATOR_CREATE                                         
  150     2        FETCH_OBJ_R                                      ~4      'function'
          3        ASSIGN                                                   !1, ~4
  151     4      > FE_RESET_R                                       $6      !0, ->13
          5    > > FE_FETCH_R                                       ~7      $6, !2, ->13
          6    >   ASSIGN                                                   !3, ~7
  152     7        INIT_DYNAMIC_CALL                                        !1
          8        SEND_VAR_EX                                              !2
          9        SEND_VAR_EX                                              !3
         10        DO_FCALL                                      0  $9      
         11        YIELD                                                    $9, !3
  151    12      > JMP                                                      ->5
         13    >   FE_FREE                                                  $6
  154    14      > GENERATOR_RETURN                                         

End of function enumerate

End of class Atom\MapEnumerator.

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

End of function __construct

Function enumerate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 14
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 14
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 13
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 13
Branch analysis from position: 14
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 14
filename:       /in/q93Ti
function name:  enumerate
number of ops:  16
compiled vars:  !0 = $collection, !1 = $function, !2 = $value, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  181     0  E >   RECV                                             !0      
          1        GENERATOR_CREATE                                         
  183     2        FETCH_OBJ_R                                      ~4      'function'
          3        ASSIGN                                                   !1, ~4
  184     4      > FE_RESET_R                                       $6      !0, ->14
          5    > > FE_FETCH_R                                       ~7      $6, !2, ->14
          6    >   ASSIGN                                                   !3, ~7
  185     7        INIT_DYNAMIC_CALL                                        !1
          8        SEND_VAR_EX                                              !2
          9        SEND_VAR_EX                                              !3
         10        DO_FCALL                                      0  $9      
         11      > JMPZ                                                     $9, ->13
  186    12    >   YIELD                                                    !2, !3
  184    13    > > JMP                                                      ->5
         14    >   FE_FREE                                                  $6
  189    15      > GENERATOR_RETURN                                         

End of function enumerate

End of class Atom\FilterEnumerator.

Class Atom\SumAggregator:
Function aggregate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/q93Ti
function name:  aggregate
number of ops:  8
compiled vars:  !0 = $collection
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  201     0  E >   RECV                                             !0      
  203     1        INIT_NS_FCALL_BY_NAME                                    'Atom%5Carray_sum'
          2        INIT_METHOD_CALL                                         !0, 'getArray'
          3        DO_FCALL                                      0  $1      
          4        SEND_VAR_NO_REF_EX                                       $1
          5        DO_FCALL                                      0  $2      
          6      > RETURN                                                   $2
  204     7*     > RETURN                                                   null

End of function aggregate

End of class Atom\SumAggregator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
159.49 ms | 1416 KiB | 21 Q