1 | <?php |
||
9 | class SidebarComposer |
||
10 | { |
||
11 | protected $modelFactory; |
||
12 | |||
13 | public function __construct(ModelAbstractorFactory $modelFactory) |
||
17 | |||
18 | public function compose($view) |
||
26 | |||
27 | /** |
||
28 | * @return array |
||
29 | */ |
||
30 | private function getSidebarItems() |
||
31 | { |
||
32 | $modelsGroups = config('anavel-crud.modelsGroups'); |
||
33 | $models = config('anavel-crud.models'); |
||
34 | $menuItems = []; |
||
35 | |||
36 | if (!is_null($modelsGroups)) { |
||
37 | foreach ($modelsGroups as $group => $items) { |
||
|
|||
38 | $menuItems[$group]['isActive'] = false; |
||
39 | $menuItems[$group]['name'] = transcrud($group); |
||
40 | $menuItems[$group]['items'] = []; |
||
41 | foreach ($items as $itemName) { |
||
42 | try { |
||
43 | $item = $this->getModelItem($itemName); |
||
44 | $menuItems[$group]['items'][] = $item; |
||
45 | if (!$menuItems[$group]['isActive'] && $item['isActive']) { |
||
46 | $menuItems[$group]['isActive'] = true; |
||
47 | } |
||
48 | if (isset($models[$itemName])) { |
||
49 | unset($models[$itemName]); |
||
50 | } |
||
51 | } catch (\Exception $e) { |
||
52 | continue; |
||
53 | } |
||
54 | } |
||
55 | // Remove empty groups (resulting, most probably, of different permissions) |
||
56 | if (count($menuItems[$group]['items']) < 1) { |
||
57 | unset($menuItems[$group]); |
||
58 | } |
||
59 | } |
||
60 | } |
||
61 | |||
62 | foreach ($models as $modelName => $model) { |
||
63 | try { |
||
64 | $item = $this->getModelItem($modelName); |
||
65 | } catch (\Exception $e) { |
||
66 | continue; |
||
67 | } |
||
68 | $menuItems[$modelName]['isActive'] = $item['isActive']; |
||
69 | $menuItems[$modelName]['name'] = $item['name']; |
||
70 | $menuItems[$modelName]['items'][] = $item; |
||
71 | } |
||
72 | |||
73 | //Sort alphabetically de menu items |
||
74 | usort($menuItems, function ($itemA, $itemB) { |
||
75 | return strcmp($itemA['name'], $itemB['name']); |
||
76 | }); |
||
77 | |||
78 | return $menuItems; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param $modelName |
||
83 | * |
||
84 | * @throws \Exception |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | private function getModelItem($modelName) |
||
110 | } |
||
111 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.