3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CategoryTree { protected $roots = []; public function addCategory($category, $parent) { if ($this->categoryExists($this->roots, $category)) { throw new InvalidArgumentException('test'); } if ($parent === null) { $this->roots[$category] = []; return; } var_dump($parent); $node = $this->findNode($parent, $this->roots); if(!isset($node)) { throw new InvalidArgumentException("Parent node: $parent not found."); } $node[$category] = []; } public function getChildren($parent) { $node = $this->findNode($parent, $this->roots); if(!isset($node)) { throw new InvalidArgumentException('test2'); } return array_keys($node); } public function findNode($node, &$root) { echo 'hej'; if (in_array($node, array_keys($root))) { return $root[$node]; } foreach ($root as $n) { return $this->findNode($node, $n); } } public function categoryExists($nodes, $category) { echo $category; $exists = false; foreach($nodes as $node) { if (in_array($category, $this->getChildren($node))) { return true; } $exists = $this->categoryExists($node, $category); } return $exists; } } // For testing purposes (do not submit uncommented): $c = new CategoryTree; $c->addCategory('A', null); $c->addCategory('B', 'A'); //$c->addCategory('C', 'A'); //echo implode(',', $c->getChildren('A'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/daERR
function name:  (null)
number of ops:  12
compiled vars:  !0 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   NEW                                              $1      'CategoryTree'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   60     3        INIT_METHOD_CALL                                         !0, 'addCategory'
          4        SEND_VAL_EX                                              'A'
          5        SEND_VAL_EX                                              null
          6        DO_FCALL                                      0          
   61     7        INIT_METHOD_CALL                                         !0, 'addCategory'
          8        SEND_VAL_EX                                              'B'
          9        SEND_VAL_EX                                              'A'
         10        DO_FCALL                                      0          
   63    11      > RETURN                                                   1

Class CategoryTree:
Function addcategory:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 13
Branch analysis from position: 9
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 19
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 32, Position 2 = 39
Branch analysis from position: 32
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/daERR
function name:  addCategory
number of ops:  42
compiled vars:  !0 = $category, !1 = $parent, !2 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    9     2        INIT_METHOD_CALL                                         'categoryExists'
          3        CHECK_FUNC_ARG                                           
          4        FETCH_OBJ_FUNC_ARG                               $3      'roots'
          5        SEND_FUNC_ARG                                            $3
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0  $4      
          8      > JMPZ                                                     $4, ->13
   10     9    >   NEW                                              $5      'InvalidArgumentException'
         10        SEND_VAL_EX                                              'test'
         11        DO_FCALL                                      0          
         12      > THROW                                         0          $5
   13    13    >   TYPE_CHECK                                    2          !1
         14      > JMPZ                                                     ~7, ->19
   14    15    >   FETCH_OBJ_W                                      $8      'roots'
         16        ASSIGN_DIM                                               $8, !0
         17        OP_DATA                                                  <array>
   15    18      > RETURN                                                   null
   17    19    >   INIT_FCALL                                               'var_dump'
         20        SEND_VAR                                                 !1
         21        DO_ICALL                                                 
   19    22        INIT_METHOD_CALL                                         'findNode'
         23        SEND_VAR_EX                                              !1
         24        CHECK_FUNC_ARG                                           
         25        FETCH_OBJ_FUNC_ARG                               $11     'roots'
         26        SEND_FUNC_ARG                                            $11
         27        DO_FCALL                                      0  $12     
         28        ASSIGN                                                   !2, $12
   20    29        ISSET_ISEMPTY_CV                                 ~14     !2
         30        BOOL_NOT                                         ~15     ~14
         31      > JMPZ                                                     ~15, ->39
   21    32    >   NEW                                              $16     'InvalidArgumentException'
         33        ROPE_INIT                                     3  ~18     'Parent+node%3A+'
         34        ROPE_ADD                                      1  ~18     ~18, !1
         35        ROPE_END                                      2  ~17     ~18, '+not+found.'
         36        SEND_VAL_EX                                              ~17
         37        DO_FCALL                                      0          
         38      > THROW                                         0          $16
   23    39    >   ASSIGN_DIM                                               !2, !0
         40        OP_DATA                                                  <array>
   24    41      > RETURN                                                   null

End of function addcategory

Function getchildren:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 15
Branch analysis from position: 11
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/daERR
function name:  getChildren
number of ops:  20
compiled vars:  !0 = $parent, !1 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
   28     1        INIT_METHOD_CALL                                         'findNode'
          2        SEND_VAR_EX                                              !0
          3        CHECK_FUNC_ARG                                           
          4        FETCH_OBJ_FUNC_ARG                               $2      'roots'
          5        SEND_FUNC_ARG                                            $2
          6        DO_FCALL                                      0  $3      
          7        ASSIGN                                                   !1, $3
   29     8        ISSET_ISEMPTY_CV                                 ~5      !1
          9        BOOL_NOT                                         ~6      ~5
         10      > JMPZ                                                     ~6, ->15
   30    11    >   NEW                                              $7      'InvalidArgumentException'
         12        SEND_VAL_EX                                              'test2'
         13        DO_FCALL                                      0          
         14      > THROW                                         0          $7
   32    15    >   INIT_FCALL                                               'array_keys'
         16        SEND_VAR                                                 !1
         17        DO_ICALL                                         $9      
         18      > RETURN                                                   $9
   33    19*     > RETURN                                                   null

End of function getchildren

Function findnode:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 13
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 77) Position 1 = 14, Position 2 = 22
Branch analysis from position: 14
2 jumps found. (Code = 78) Position 1 = 15, Position 2 = 22
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
filename:       /in/daERR
function name:  findNode
number of ops:  24
compiled vars:  !0 = $node, !1 = $root, !2 = $n
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   36     2        ECHO                                                     'hej'
   37     3        INIT_FCALL                                               'in_array'
          4        SEND_VAR                                                 !0
          5        INIT_FCALL                                               'array_keys'
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $3      
          8        SEND_VAR                                                 $3
          9        DO_ICALL                                         $4      
         10      > JMPZ                                                     $4, ->13
   38    11    >   FETCH_DIM_R                                      ~5      !1, !0
         12      > RETURN                                                   ~5
   40    13    > > FE_RESET_R                                       $6      !1, ->22
         14    > > FE_FETCH_R                                               $6, !2, ->22
   41    15    >   INIT_METHOD_CALL                                         'findNode'
         16        SEND_VAR_EX                                              !0
         17        SEND_VAR_EX                                              !2
         18        DO_FCALL                                      0  $7      
         19        FE_FREE                                                  $6
         20      > RETURN                                                   $7
   40    21*       JMP                                                      ->14
         22    >   FE_FREE                                                  $6
   43    23      > RETURN                                                   null

End of function findnode

Function categoryexists:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 22
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 22
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 16
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
filename:       /in/daERR
function name:  categoryExists
number of ops:  25
compiled vars:  !0 = $nodes, !1 = $category, !2 = $exists, !3 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   46     2        ECHO                                                     !1
   47     3        ASSIGN                                                   !2, <false>
   48     4      > FE_RESET_R                                       $5      !0, ->22
          5    > > FE_FETCH_R                                               $5, !3, ->22
   49     6    >   INIT_FCALL                                               'in_array'
          7        SEND_VAR                                                 !1
          8        INIT_METHOD_CALL                                         'getChildren'
          9        SEND_VAR_EX                                              !3
         10        DO_FCALL                                      0  $6      
         11        SEND_VAR                                                 $6
         12        DO_ICALL                                         $7      
         13      > JMPZ                                                     $7, ->16
   50    14    >   FE_FREE                                                  $5
         15      > RETURN                                                   <true>
   52    16    >   INIT_METHOD_CALL                                         'categoryExists'
         17        SEND_VAR_EX                                              !3
         18        SEND_VAR_EX                                              !1
         19        DO_FCALL                                      0  $8      
         20        ASSIGN                                                   !2, $8
   48    21      > JMP                                                      ->5
         22    >   FE_FREE                                                  $5
   54    23      > RETURN                                                   !2
   55    24*     > RETURN                                                   null

End of function categoryexists

End of class CategoryTree.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.41 ms | 1408 KiB | 19 Q