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) || !$this->categoryExists($this->roots, $parent)) { throw new InvalidArgumentException(); } if ($parent === null) { $this->tree[$category] = []; } $node = $findNode($parent, $this->root); if(!isset($node)) { throw new InvalidArgumentException(); } $node[$category] = []; } public function getChildren($parent) { $node = $findNode($parent, $this->root); if(!isset($node)) { throw new InvalidArgumentException(); } return array_keys($node); } public function findNode($node, &$root) { if (in_array($node, array_keys($root))) { return $root[$node]; } foreach ($root as $n) { return $this->findNode($node, $n); } } public function categoryExists($nodes, $category) { $exists = false; foreach($node 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/UEpV2
function name:  (null)
number of ops:  16
compiled vars:  !0 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   NEW                                              $1      'CategoryTree'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   55     3        INIT_METHOD_CALL                                         !0, 'addCategory'
          4        SEND_VAL_EX                                              'A'
          5        SEND_VAL_EX                                              null
          6        DO_FCALL                                      0          
   56     7        INIT_METHOD_CALL                                         !0, 'addCategory'
          8        SEND_VAL_EX                                              'B'
          9        SEND_VAL_EX                                              'A'
         10        DO_FCALL                                      0          
   57    11        INIT_METHOD_CALL                                         !0, 'addCategory'
         12        SEND_VAL_EX                                              'C'
         13        SEND_VAL_EX                                              'A'
         14        DO_FCALL                                      0          
   58    15      > RETURN                                                   1

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

End of function getchildren

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

End of function findnode

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

End of function categoryexists

End of class CategoryTree.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
160.15 ms | 1408 KiB | 17 Q