3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Menu { const MENU_CACHE_KEY = 'menu_cache'; /** @var MenuItem[] */ protected $rootNodes = array(); protected $childNodes = array(); /** @var ICache */ protected $cache; /** * @param ICache $cache */ public function setCache(ICache $cache) { $this->cache = $cache; } public function hasCache() { return $this->cache instanceof ICache; } /** * @param MenuItem $menuItem */ public function add(MenuItem $menuItem) { if ($menuItem->isParent()) { $this->addRootNode($menuItem); } else { $this->addChildNode($menuItem); } } /** * @param MenuItem $menuItem */ private function addRootNode(MenuItem $menuItem) { array_push($this->rootNodes, $menuItem); } /** * @param MenuItem $menuItem */ private function addChildNode(MenuItem $menuItem) { $this->ensureChildNodeExists($menuItem->parentId); array_push($this->childNodes[$menuItem->parentId], $menuItem); } /** * @param int $parentId */ private function ensureChildNodeExists($parentId) { if (!isset($this->childNodes[$parentId])) { $this->childNodes[$parentId] = array(); } } /** * @return string */ public function render() { if ($this->hasCache()) { $cached = $this->cache->getItem(self::MENU_CACHE_KEY); if (!empty($cached)) { return $cached; } } $html = '<ul>'; foreach ($this->rootNodes as $menuItem) { $html .= '<li>' . $menuItem->render(); if (!empty($this->childNodes[$menuItem->id])) { $html .= '<ul>'; /** @var $childMenuItem MenuItem */ foreach ($this->childNodes[$menuItem->id] as $childMenuItem) { $html .= '<li>' . $childMenuItem->render() . '</li>'; } $html .= '</ul>'; } $html .= '</li>'; } $html .= '</ul>'; if ($this->hasCache()) { $this->cache->setItem(self::MENU_CACHE_KEY, $html); } return $html; } } class MenuItem { public $id; public $parentId; public $label; public $link; /** * @param array|null $params */ public function __construct($params = null) { if (!is_null($params)) { $this->id = (int) $params['id']; $this->label = $params['label']; $this->link = $params['link']; $this->parentId = (int) $params['parent_id']; } } /** * @return bool */ public function isParent() { return $this->parentId === 0; } /** * @return string */ public function render() { return '<a href="' . $this->link . '">' . $this->label . '</a>'; } } interface ICache { /** * @param string $key * * @return string|null */ public function getItem($key); /** * @param string $key * @param string $value */ public function setItem($key, $value); } $dbResults = array( array('id' => 1, 'parent_id' => 0, 'link' => '#', 'label' => 'Home'), array('id' => 2, 'parent_id' => 0, 'link' => '#', 'label' => 'Portfolio'), array('id' => 3, 'parent_id' => 0, 'link' => '#', 'label' => 'Projects'), array('id' => 4, 'parent_id' => 2, 'link' => '#', 'label' => 'Design'), array('id' => 5, 'parent_id' => 2, 'link' => '#', 'label' => 'Dev'), array('id' => 6, 'parent_id' => 2, 'link' => '#', 'label' => 'Foo'), array('id' => 7, 'parent_id' => 3, 'link' => '#', 'label' => 'Bar'), ); $menu = new Menu(); /* $menu->setCache(new RedisCache()); $menu->setCache(new FileCache()); $menu->setCache(new MemcachedCache()); */ foreach ($dbResults as $row) { $menu->add(new MenuItem($row)); } echo $menu->render();
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 = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/N6cli
function name:  (null)
number of ops:  18
compiled vars:  !0 = $dbResults, !1 = $menu, !2 = $row
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  163     0  E >   ASSIGN                                                   !0, <array>
  173     1        NEW                                              $4      'Menu'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $4
  179     4      > FE_RESET_R                                       $7      !0, ->13
          5    > > FE_FETCH_R                                               $7, !2, ->13
  180     6    >   INIT_METHOD_CALL                                         !1, 'add'
          7        NEW                                              $8      'MenuItem'
          8        SEND_VAR_EX                                              !2
          9        DO_FCALL                                      0          
         10        SEND_VAR_NO_REF_EX                                       $8
         11        DO_FCALL                                      0          
  179    12      > JMP                                                      ->5
         13    >   FE_FREE                                                  $7
  183    14        INIT_METHOD_CALL                                         !1, 'render'
         15        DO_FCALL                                      0  $11     
         16        ECHO                                                     $11
         17      > RETURN                                                   1

Class Menu:
Function setcache:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/N6cli
function name:  setCache
number of ops:  4
compiled vars:  !0 = $cache
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
   20     1        ASSIGN_OBJ                                               'cache'
          2        OP_DATA                                                  !0
   21     3      > RETURN                                                   null

End of function setcache

Function hascache:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/N6cli
function name:  hasCache
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   FETCH_OBJ_R                                      ~0      'cache'
          1        INSTANCEOF                                       ~1      ~0, 'ICache'
          2      > RETURN                                                   ~1
   26     3*     > RETURN                                                   null

End of function hascache

Function add:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/N6cli
function name:  add
number of ops:  12
compiled vars:  !0 = $menuItem
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
   33     1        INIT_METHOD_CALL                                         !0, 'isParent'
          2        DO_FCALL                                      0  $1      
          3      > JMPZ                                                     $1, ->8
   34     4    >   INIT_METHOD_CALL                                         'addRootNode'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0          
          7      > JMP                                                      ->11
   36     8    >   INIT_METHOD_CALL                                         'addChildNode'
          9        SEND_VAR_EX                                              !0
         10        DO_FCALL                                      0          
   38    11    > > RETURN                                                   null

End of function add

Function addrootnode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/N6cli
function name:  addRootNode
number of ops:  7
compiled vars:  !0 = $menuItem
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   RECV                                             !0      
   45     1        INIT_FCALL                                               'array_push'
          2        FETCH_OBJ_W                                      $1      'rootNodes'
          3        SEND_REF                                                 $1
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                                 
   46     6      > RETURN                                                   null

End of function addrootnode

Function addchildnode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/N6cli
function name:  addChildNode
number of ops:  14
compiled vars:  !0 = $menuItem
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   RECV                                             !0      
   53     1        INIT_METHOD_CALL                                         'ensureChildNodeExists'
          2        CHECK_FUNC_ARG                                           
          3        FETCH_OBJ_FUNC_ARG                               $1      !0, 'parentId'
          4        SEND_FUNC_ARG                                            $1
          5        DO_FCALL                                      0          
   54     6        INIT_FCALL                                               'array_push'
          7        FETCH_OBJ_R                                      ~4      !0, 'parentId'
          8        FETCH_OBJ_W                                      $3      'childNodes'
          9        FETCH_DIM_W                                      $5      $3, ~4
         10        SEND_REF                                                 $5
         11        SEND_VAR                                                 !0
         12        DO_ICALL                                                 
   55    13      > RETURN                                                   null

End of function addchildnode

Function ensurechildnodeexists:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 8
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/N6cli
function name:  ensureChildNodeExists
number of ops:  9
compiled vars:  !0 = $parentId
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   RECV                                             !0      
   62     1        FETCH_OBJ_IS                                     ~1      'childNodes'
          2        ISSET_ISEMPTY_DIM_OBJ                         0  ~2      ~1, !0
          3        BOOL_NOT                                         ~3      ~2
          4      > JMPZ                                                     ~3, ->8
   63     5    >   FETCH_OBJ_W                                      $4      'childNodes'
          6        ASSIGN_DIM                                               $4, !0
          7        OP_DATA                                                  <array>
   65     8    > > RETURN                                                   null

End of function ensurechildnodeexists

Function render:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 12
Branch analysis from position: 3
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 = 15, Position 2 = 41
Branch analysis from position: 15
2 jumps found. (Code = 78) Position 1 = 16, Position 2 = 41
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 39
Branch analysis from position: 25
2 jumps found. (Code = 77) Position 1 = 30, Position 2 = 37
Branch analysis from position: 30
2 jumps found. (Code = 78) Position 1 = 31, Position 2 = 37
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 37
Branch analysis from position: 39
Branch analysis from position: 41
2 jumps found. (Code = 43) Position 1 = 46, Position 2 = 51
Branch analysis from position: 46
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 51
Branch analysis from position: 41
Branch analysis from position: 12
filename:       /in/N6cli
function name:  render
number of ops:  53
compiled vars:  !0 = $cached, !1 = $html, !2 = $menuItem, !3 = $childMenuItem
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   72     0  E >   INIT_METHOD_CALL                                         'hasCache'
          1        DO_FCALL                                      0  $4      
          2      > JMPZ                                                     $4, ->12
   73     3    >   FETCH_OBJ_R                                      ~5      'cache'
          4        INIT_METHOD_CALL                                         ~5, 'getItem'
          5        SEND_VAL_EX                                              'menu_cache'
          6        DO_FCALL                                      0  $6      
          7        ASSIGN                                                   !0, $6
   74     8        ISSET_ISEMPTY_CV                                 ~8      !0
          9        BOOL_NOT                                         ~9      ~8
         10      > JMPZ                                                     ~9, ->12
   75    11    > > RETURN                                                   !0
   79    12    >   ASSIGN                                                   !1, '%3Cul%3E'
   80    13        FETCH_OBJ_R                                      ~11     'rootNodes'
         14      > FE_RESET_R                                       $12     ~11, ->41
         15    > > FE_FETCH_R                                               $12, !2, ->41
   81    16    >   INIT_METHOD_CALL                                         !2, 'render'
         17        DO_FCALL                                      0  $13     
         18        CONCAT                                           ~14     '%3Cli%3E', $13
         19        ASSIGN_OP                                     8          !1, ~14
   83    20        FETCH_OBJ_R                                      ~17     !2, 'id'
         21        FETCH_OBJ_IS                                     ~16     'childNodes'
         22        ISSET_ISEMPTY_DIM_OBJ                         1  ~18     ~16, ~17
         23        BOOL_NOT                                         ~19     ~18
         24      > JMPZ                                                     ~19, ->39
   85    25    >   ASSIGN_OP                                     8          !1, '%3Cul%3E'
   88    26        FETCH_OBJ_R                                      ~22     !2, 'id'
         27        FETCH_OBJ_R                                      ~21     'childNodes'
         28        FETCH_DIM_R                                      ~23     ~21, ~22
         29      > FE_RESET_R                                       $24     ~23, ->37
         30    > > FE_FETCH_R                                               $24, !3, ->37
   89    31    >   INIT_METHOD_CALL                                         !3, 'render'
         32        DO_FCALL                                      0  $25     
         33        CONCAT                                           ~26     '%3Cli%3E', $25
         34        CONCAT                                           ~27     ~26, '%3C%2Fli%3E'
         35        ASSIGN_OP                                     8          !1, ~27
   88    36      > JMP                                                      ->30
         37    >   FE_FREE                                                  $24
   92    38        ASSIGN_OP                                     8          !1, '%3C%2Ful%3E'
   95    39    >   ASSIGN_OP                                     8          !1, '%3C%2Fli%3E'
   80    40      > JMP                                                      ->15
         41    >   FE_FREE                                                  $12
   97    42        ASSIGN_OP                                     8          !1, '%3C%2Ful%3E'
   99    43        INIT_METHOD_CALL                                         'hasCache'
         44        DO_FCALL                                      0  $32     
         45      > JMPZ                                                     $32, ->51
  100    46    >   FETCH_OBJ_R                                      ~33     'cache'
         47        INIT_METHOD_CALL                                         ~33, 'setItem'
         48        SEND_VAL_EX                                              'menu_cache'
         49        SEND_VAR_EX                                              !1
         50        DO_FCALL                                      0          
  103    51    > > RETURN                                                   !1
  104    52*     > RETURN                                                   null

End of function render

End of class Menu.

Class MenuItem:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 18
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
filename:       /in/N6cli
function name:  __construct
number of ops:  19
compiled vars:  !0 = $params
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  117     0  E >   RECV_INIT                                        !0      null
  119     1        TYPE_CHECK                                    2  ~1      !0
          2        BOOL_NOT                                         ~2      ~1
          3      > JMPZ                                                     ~2, ->18
  120     4    >   FETCH_DIM_R                                      ~4      !0, 'id'
          5        CAST                                          4  ~5      ~4
          6        ASSIGN_OBJ                                               'id'
          7        OP_DATA                                                  ~5
  121     8        FETCH_DIM_R                                      ~7      !0, 'label'
          9        ASSIGN_OBJ                                               'label'
         10        OP_DATA                                                  ~7
  122    11        FETCH_DIM_R                                      ~9      !0, 'link'
         12        ASSIGN_OBJ                                               'link'
         13        OP_DATA                                                  ~9
  123    14        FETCH_DIM_R                                      ~11     !0, 'parent_id'
         15        CAST                                          4  ~12     ~11
         16        ASSIGN_OBJ                                               'parentId'
         17        OP_DATA                                                  ~12
  125    18    > > RETURN                                                   null

End of function __construct

Function isparent:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/N6cli
function name:  isParent
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  132     0  E >   FETCH_OBJ_R                                      ~0      'parentId'
          1        IS_IDENTICAL                                     ~1      ~0, 0
          2      > RETURN                                                   ~1
  133     3*     > RETURN                                                   null

End of function isparent

Function render:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/N6cli
function name:  render
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  140     0  E >   FETCH_OBJ_R                                      ~0      'link'
          1        CONCAT                                           ~1      '%3Ca+href%3D%22', ~0
          2        CONCAT                                           ~2      ~1, '%22%3E'
          3        FETCH_OBJ_R                                      ~3      'label'
          4        CONCAT                                           ~4      ~2, ~3
          5        CONCAT                                           ~5      ~4, '%3C%2Fa%3E'
          6      > RETURN                                                   ~5
  141     7*     > RETURN                                                   null

End of function render

End of class MenuItem.

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

End of function getitem

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

End of function setitem

End of class ICache.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.35 ms | 1420 KiB | 15 Q