Conditions | 16 |
Paths | 1 |
Total Lines | 67 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
35 | public static function addItems($menu, Collection $items) |
||
36 | { |
||
37 | $items->sort(function ($entry1, $entry2) { |
||
38 | $weight1 = $entry1['weight']?? 10; |
||
39 | $weight2 = $entry2['weight']?? 10; |
||
40 | |||
41 | return $weight1 <=> $weight2; |
||
42 | })->map(function($item, $caption) use ($menu) { |
||
43 | if (! ($item['access']?? true)) return; |
||
44 | |||
45 | if (!isset($item['link']) && !is_array($item)) { |
||
46 | $item = [ |
||
47 | 'link' => $item |
||
48 | ]; |
||
49 | } |
||
50 | |||
51 | $item['caption'] = $item['caption']?? $caption; |
||
52 | |||
53 | if (is_array($item['caption'])) { |
||
54 | $item['caption'] = [$caption] + $item['caption']; |
||
55 | } |
||
56 | |||
57 | if ($subitems = $item['menu']?? []) { |
||
58 | $submenu = $menu->addMenu($item['caption']); |
||
59 | |||
60 | self::addItems($submenu, collect($subitems)); |
||
61 | } |
||
62 | elseif ($subitems = $item['group']?? []) { |
||
63 | $subgroup = $menu->addGroup($item['caption']); |
||
64 | |||
65 | if (($item['toggle']?? false) && !$menu->in_dropdown) { |
||
66 | $subgroup->addClass('toggle-group'); |
||
67 | $subgroup->add(['Icon', 'dropdown'], 'Icon')->removeClass('item'); |
||
68 | } |
||
69 | |||
70 | self::addItems($subgroup, collect($subitems)); |
||
71 | } |
||
72 | elseif ($subitems = $item['accordion']?? []) { |
||
73 | $accordion = $menu->add(['Accordion']); |
||
74 | |||
75 | $section = $accordion->addSection($item['caption']); |
||
76 | |||
77 | foreach ($subitems as $subitem) { |
||
78 | $subitem = $section->add(['Item', 'Test', 'ui' => 'item'])->setElement('a'); |
||
79 | |||
80 | $action = null; |
||
81 | $link = $item['link']?? ''; |
||
82 | if (is_string($link) || is_array($link)) { |
||
83 | $action = $section->url($link); |
||
84 | } |
||
85 | |||
86 | if (is_string($link)) { |
||
87 | $subitem->setAttr('href', $link); |
||
88 | } |
||
89 | |||
90 | if ($action instanceof jsExpressionable) { |
||
91 | $subitem->js('click', $link); |
||
92 | } |
||
93 | } |
||
94 | // self::addItems($accordion, collect($subitems)); |
||
95 | } |
||
96 | else { |
||
97 | if (is_a($menu, Accordion::class)) { |
||
98 | $menu->add(['View', 'Test']); |
||
99 | } |
||
100 | else { |
||
101 | $menu->addItem($item['caption'], $item['link']?? ''); |
||
102 | } |
||
107 | } |