3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Collection extends ArrayObject { /** * @var string */ protected $expectedValueType = ''; /** * Collection constructor. * * @param string $expectedValueType * @param array $initialValues */ public function __construct(string $expectedValueType, array $initialValues = []) { $this->setExpectedValueType($expectedValueType); foreach ($initialValues as $initialValue) $this->append($initialValue); } /** * @param $value */ public function append($value) { if (!$this->validateType($value)) throw new \InvalidArgumentException('Expected value type for this collection is ' . $this->getExpectedValueType() . ', ' . gettype($value) . ' given.'); parent::append($value); } /** * @param mixed $value * * @return bool */ public function contains($value): bool { foreach ($this as $v) { if ($v == $value) return true; } return false; } /** * @param $value * * @return false|int|string|mixed */ public function getKey($value) { foreach ($this as $key => $v) { if ($v == $value) return $key; } return false; } /** * @inheritdoc */ public function offsetSet($offset, $value) { if (!$this->validateType($value)) throw new \InvalidArgumentException('Expected value type for this collection is ' . $this->getExpectedValueType() . ', ' . gettype($value) . ' given.'); parent::offsetSet($offset, $value); } /** * @param mixed $value */ public function remove($value) { if (!$this->contains($value)) throw new \InvalidArgumentException('The given value does not exist in this collection.'); $this->offsetUnset($this->getKey($value)); } /** * @param $value * * @return bool */ public function validateType($value): bool { $expectedValueType = $this->getExpectedValueType(); switch ($expectedValueType) { case 'string': return is_string($value); case 'int': case 'integer': return is_int($value); case 'float': case 'double': return is_float($value); case 'array': return is_array($value); case 'object': return is_object($value); case 'callable': return is_callable($value); default: return ($value instanceof $expectedValueType); } } /** * @return string */ public function getExpectedValueType(): string { return $this->expectedValueType; } /** * @param string $expectedValueType */ public function setExpectedValueType(string $expectedValueType) { if (in_array(strtolower($expectedValueType), ['string', 'int', 'integer', 'float', 'double', 'bool', 'boolean', 'array', 'object', 'callable'])) $expectedValueType = strtolower($expectedValueType); $this->expectedValueType = $expectedValueType; } } $coll = new Collection ('string',['a','b']); foreach($coll as $item){ var_dump($item); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 11
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 11
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/leppJ
function name:  (null)
number of ops:  13
compiled vars:  !0 = $coll, !1 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  122     0  E >   NEW                                              $2      'Collection'
          1        SEND_VAL_EX                                              'string'
          2        SEND_VAL_EX                                              <array>
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !0, $2
  123     5      > FE_RESET_R                                       $5      !0, ->11
          6    > > FE_FETCH_R                                               $5, !1, ->11
  124     7    >   INIT_FCALL                                               'var_dump'
          8        SEND_VAR                                                 !1
          9        DO_ICALL                                                 
  123    10      > JMP                                                      ->6
         11    >   FE_FREE                                                  $5
  125    12      > RETURN                                                   1

Class Collection:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 11
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 11
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/leppJ
function name:  __construct
number of ops:  13
compiled vars:  !0 = $expectedValueType, !1 = $initialValues, !2 = $initialValue
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
   17     2        INIT_METHOD_CALL                                         'setExpectedValueType'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
   18     5      > FE_RESET_R                                       $4      !1, ->11
          6    > > FE_FETCH_R                                               $4, !2, ->11
   19     7    >   INIT_METHOD_CALL                                         'append'
          8        SEND_VAR_EX                                              !2
          9        DO_FCALL                                      0          
   18    10      > JMP                                                      ->6
         11    >   FE_FREE                                                  $4
   20    12      > RETURN                                                   null

End of function __construct

Function append:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 17
Branch analysis from position: 6
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/leppJ
function name:  append
number of ops:  21
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
   26     1        INIT_METHOD_CALL                                         'validateType'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $1      
          4        BOOL_NOT                                         ~2      $1
          5      > JMPZ                                                     ~2, ->17
   27     6    >   NEW                                              $3      'InvalidArgumentException'
          7        INIT_METHOD_CALL                                         'getExpectedValueType'
          8        DO_FCALL                                      0  $4      
          9        CONCAT                                           ~5      'Expected+value+type+for+this+collection+is+', $4
         10        CONCAT                                           ~6      ~5, '%2C+'
         11        GET_TYPE                                         ~7      !0
         12        CONCAT                                           ~8      ~6, ~7
         13        CONCAT                                           ~9      ~8, '+given.'
         14        SEND_VAL_EX                                              ~9
         15        DO_FCALL                                      0          
         16      > THROW                                         0          $3
   28    17    >   INIT_STATIC_METHOD_CALL                                  'append'
         18        SEND_VAR_EX                                              !0
         19        DO_FCALL                                      0          
   29    20      > RETURN                                                   null

End of function append

Function contains:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 9
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 9
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/leppJ
function name:  contains
number of ops:  13
compiled vars:  !0 = $value, !1 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
   37     1        FETCH_THIS                                       ~2      
          2      > FE_RESET_R                                       $3      ~2, ->9
          3    > > FE_FETCH_R                                               $3, !1, ->9
   39     4    >   IS_EQUAL                                                 !1, !0
          5      > JMPZ                                                     ~4, ->8
   40     6    >   FE_FREE                                                  $3
          7      > RETURN                                                   <true>
   37     8    > > JMP                                                      ->3
          9    >   FE_FREE                                                  $3
   42    10      > RETURN                                                   <false>
   43    11*       VERIFY_RETURN_TYPE                                       
         12*     > RETURN                                                   null

End of function contains

Function getkey:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 10
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 10
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 9
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
filename:       /in/leppJ
function name:  getKey
number of ops:  13
compiled vars:  !0 = $value, !1 = $v, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   RECV                                             !0      
   51     1        FETCH_THIS                                       ~3      
          2      > FE_RESET_R                                       $4      ~3, ->10
          3    > > FE_FETCH_R                                       ~5      $4, !1, ->10
          4    >   ASSIGN                                                   !2, ~5
   53     5        IS_EQUAL                                                 !1, !0
          6      > JMPZ                                                     ~7, ->9
   54     7    >   FE_FREE                                                  $4
          8      > RETURN                                                   !2
   51     9    > > JMP                                                      ->3
         10    >   FE_FREE                                                  $4
   56    11      > RETURN                                                   <false>
   57    12*     > RETURN                                                   null

End of function getkey

Function offsetset:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 18
Branch analysis from position: 7
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/leppJ
function name:  offsetSet
number of ops:  23
compiled vars:  !0 = $offset, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   63     2        INIT_METHOD_CALL                                         'validateType'
          3        SEND_VAR_EX                                              !1
          4        DO_FCALL                                      0  $2      
          5        BOOL_NOT                                         ~3      $2
          6      > JMPZ                                                     ~3, ->18
   64     7    >   NEW                                              $4      'InvalidArgumentException'
          8        INIT_METHOD_CALL                                         'getExpectedValueType'
          9        DO_FCALL                                      0  $5      
         10        CONCAT                                           ~6      'Expected+value+type+for+this+collection+is+', $5
         11        CONCAT                                           ~7      ~6, '%2C+'
         12        GET_TYPE                                         ~8      !1
         13        CONCAT                                           ~9      ~7, ~8
         14        CONCAT                                           ~10     ~9, '+given.'
         15        SEND_VAL_EX                                              ~10
         16        DO_FCALL                                      0          
         17      > THROW                                         0          $4
   65    18    >   INIT_STATIC_METHOD_CALL                                  'offsetSet'
         19        SEND_VAR_EX                                              !0
         20        SEND_VAR_EX                                              !1
         21        DO_FCALL                                      0          
   66    22      > RETURN                                                   null

End of function offsetset

Function remove:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 10
Branch analysis from position: 6
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/leppJ
function name:  remove
number of ops:  17
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   70     0  E >   RECV                                             !0      
   72     1        INIT_METHOD_CALL                                         'contains'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $1      
          4        BOOL_NOT                                         ~2      $1
          5      > JMPZ                                                     ~2, ->10
   73     6    >   NEW                                              $3      'InvalidArgumentException'
          7        SEND_VAL_EX                                              'The+given+value+does+not+exist+in+this+collection.'
          8        DO_FCALL                                      0          
          9      > THROW                                         0          $3
   74    10    >   INIT_METHOD_CALL                                         'offsetUnset'
         11        INIT_METHOD_CALL                                         'getKey'
         12        SEND_VAR_EX                                              !0
         13        DO_FCALL                                      0  $5      
         14        SEND_VAR_NO_REF_EX                                       $5
         15        DO_FCALL                                      0          
   75    16      > RETURN                                                   null

End of function remove

Function validatetype:
Finding entry points
Branch analysis from position: 0
10 jumps found. (Code = 188) Position 1 = 22, Position 2 = 25, Position 3 = 25, Position 4 = 28, Position 5 = 28, Position 6 = 31, Position 7 = 34, Position 8 = 37, Position 9 = 42, Position 10 = 5
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 42
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 7, Position 2 = 22
Branch analysis from position: 7
2 jumps found. (Code = 44) Position 1 = 9, Position 2 = 25
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 11, Position 2 = 25
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 28
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 15, Position 2 = 28
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 17, Position 2 = 31
Branch analysis from position: 17
2 jumps found. (Code = 44) Position 1 = 19, Position 2 = 34
Branch analysis from position: 19
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 37
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 42
Branch analysis from position: 42
Branch analysis from position: 37
Branch analysis from position: 34
Branch analysis from position: 31
Branch analysis from position: 28
Branch analysis from position: 28
Branch analysis from position: 25
Branch analysis from position: 25
Branch analysis from position: 22
filename:       /in/leppJ
function name:  validateType
number of ops:  48
compiled vars:  !0 = $value, !1 = $expectedValueType
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   81     0  E >   RECV                                             !0      
   83     1        INIT_METHOD_CALL                                         'getExpectedValueType'
          2        DO_FCALL                                      0  $2      
          3        ASSIGN                                                   !1, $2
   84     4      > SWITCH_STRING                                            !1, [ 'string':->22, 'int':->25, 'integer':->25, 'float':->28, 'double':->28, 'array':->31, 'object':->34, 'callable':->37, ], ->42
   86     5    >   IS_EQUAL                                                 !1, 'string'
          6      > JMPNZ                                                    ~4, ->22
   88     7    >   IS_EQUAL                                                 !1, 'int'
          8      > JMPNZ                                                    ~4, ->25
   89     9    >   IS_EQUAL                                                 !1, 'integer'
         10      > JMPNZ                                                    ~4, ->25
   91    11    >   IS_EQUAL                                                 !1, 'float'
         12      > JMPNZ                                                    ~4, ->28
   92    13    >   IS_EQUAL                                                 !1, 'double'
         14      > JMPNZ                                                    ~4, ->28
   94    15    >   IS_EQUAL                                                 !1, 'array'
         16      > JMPNZ                                                    ~4, ->31
   96    17    >   IS_EQUAL                                                 !1, 'object'
         18      > JMPNZ                                                    ~4, ->34
   98    19    >   IS_EQUAL                                                 !1, 'callable'
         20      > JMPNZ                                                    ~4, ->37
         21    > > JMP                                                      ->42
   87    22    >   TYPE_CHECK                                   64  ~5      !0
         23        VERIFY_RETURN_TYPE                                       ~5
         24      > RETURN                                                   ~5
   90    25    >   TYPE_CHECK                                   16  ~6      !0
         26        VERIFY_RETURN_TYPE                                       ~6
         27      > RETURN                                                   ~6
   93    28    >   TYPE_CHECK                                   32  ~7      !0
         29        VERIFY_RETURN_TYPE                                       ~7
         30      > RETURN                                                   ~7
   95    31    >   TYPE_CHECK                                  128  ~8      !0
         32        VERIFY_RETURN_TYPE                                       ~8
         33      > RETURN                                                   ~8
   97    34    >   TYPE_CHECK                                  256  ~9      !0
         35        VERIFY_RETURN_TYPE                                       ~9
         36      > RETURN                                                   ~9
   99    37    >   INIT_FCALL                                               'is_callable'
         38        SEND_VAR                                                 !0
         39        DO_ICALL                                         $10     
         40        VERIFY_RETURN_TYPE                                       $10
         41      > RETURN                                                   $10
  101    42    >   FETCH_CLASS                                   0  $11     !1
         43        INSTANCEOF                                       ~12     !0, $11
         44        VERIFY_RETURN_TYPE                                       ~12
         45      > RETURN                                                   ~12
  103    46*       VERIFY_RETURN_TYPE                                       
         47*     > RETURN                                                   null

End of function validatetype

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

End of function getexpectedvaluetype

Function setexpectedvaluetype:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 10
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
filename:       /in/leppJ
function name:  setExpectedValueType
number of ops:  13
compiled vars:  !0 = $expectedValueType
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  114     0  E >   RECV                                             !0      
  116     1        INIT_FCALL                                               'strtolower'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $1      
          4        IN_ARRAY                                                 $1, <array>
          5      > JMPZ                                                     ~2, ->10
  117     6    >   INIT_FCALL                                               'strtolower'
          7        SEND_VAR                                                 !0
          8        DO_ICALL                                         $3      
          9        ASSIGN                                                   !0, $3
  118    10    >   ASSIGN_OBJ                                               'expectedValueType'
         11        OP_DATA                                                  !0
  119    12      > RETURN                                                   null

End of function setexpectedvaluetype

End of class Collection.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
177.9 ms | 1416 KiB | 19 Q