3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CategoryTree { public $roots = []; public function addCategory($category, $parent) { if ($this->categoryExists($this->roots, $category)) { throw new InvalidArgumentException("Category: $category already exists."); } if ($parent === null) { $this->roots[$category] = []; return; } $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("Parent node: $parent not found."); } return array_keys($node); } protected 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 = 'test'; if (in_array($category, array_keys($nodes))) { return true; } foreach($nodes as $key => $node) { $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')); echo "\n". $c->categoryExists($c->roots, 'D');
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Rk0hp
function name:  (null)
number of ops:  32
compiled vars:  !0 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E >   NEW                                              $1      'CategoryTree'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   56     3        INIT_METHOD_CALL                                         !0, 'addCategory'
          4        SEND_VAL_EX                                              'A'
          5        SEND_VAL_EX                                              null
          6        DO_FCALL                                      0          
   57     7        INIT_METHOD_CALL                                         !0, 'addCategory'
          8        SEND_VAL_EX                                              'B'
          9        SEND_VAL_EX                                              'A'
         10        DO_FCALL                                      0          
   58    11        INIT_METHOD_CALL                                         !0, 'addCategory'
         12        SEND_VAL_EX                                              'C'
         13        SEND_VAL_EX                                              'A'
         14        DO_FCALL                                      0          
   59    15        INIT_FCALL                                               'implode'
         16        SEND_VAL                                                 '%2C'
         17        INIT_METHOD_CALL                                         !0, 'getChildren'
         18        SEND_VAL_EX                                              'A'
         19        DO_FCALL                                      0  $7      
         20        SEND_VAR                                                 $7
         21        DO_ICALL                                         $8      
         22        ECHO                                                     $8
   60    23        INIT_METHOD_CALL                                         !0, 'categoryExists'
         24        CHECK_FUNC_ARG                                           
         25        FETCH_OBJ_FUNC_ARG                               $9      !0, 'roots'
         26        SEND_FUNC_ARG                                            $9
         27        SEND_VAL_EX                                              'D'
         28        DO_FCALL                                      0  $10     
         29        CONCAT                                           ~11     '%0A', $10
         30        ECHO                                                     ~11
         31      > RETURN                                                   1

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

End of function findnode

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

End of function categoryexists

End of class CategoryTree.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
173.51 ms | 1408 KiB | 19 Q