Conditions | 19 |
Paths | 8 |
Total Lines | 101 |
Code Lines | 60 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
75 | public function createMenuWithSubMenus($config) |
||
76 | { |
||
77 | $default = [ |
||
78 | "id" => null, |
||
79 | "class" => null, |
||
80 | "wrapper" => "nav", |
||
81 | ]; |
||
82 | $menu = array_replace_recursive($default, $config); |
||
83 | //$menu = array_replace_recursive($menu, $menus[$menuName]); |
||
84 | |||
85 | // Create the ul li menu from the array, use an anonomous recursive |
||
86 | // function that returns an array of values. |
||
87 | $createMenu = function ( |
||
88 | $items, |
||
89 | $ulId = null, |
||
90 | $ulClass = null |
||
91 | ) use ( |
||
92 | &$createMenu |
||
93 | ) { |
||
94 | |||
95 | $html = null; |
||
96 | $hasItemIsSelected = false; |
||
97 | |||
98 | foreach ($items as $item) { |
||
99 | // has submenu, call recursivly and keep track on if the submenu has a selected item in it. |
||
100 | $subMenu = null; |
||
101 | $subMenuButton = null; |
||
102 | $subMenuClass = null; |
||
103 | $selectedParent = null; |
||
104 | |||
105 | if (isset($item["submenu"])) { |
||
106 | list($subMenu, $selectedParent) = $createMenu($item["submenu"]["items"]); |
||
107 | $selectedParent = $selectedParent |
||
108 | ? "selected-parent " |
||
109 | : null; |
||
110 | $subMenuButton = "<a class=\"rm-submenu-button\" href=\"#\"></a>"; |
||
111 | $subMenuClass = "rm-submenu"; |
||
112 | } |
||
113 | |||
114 | // Check if the current menuitem is selected |
||
115 | if (!isset($item["url"])) { |
||
116 | var_dump($item); |
||
117 | } |
||
118 | $selected = $this->check($item["url"]) |
||
119 | ? "selected " |
||
120 | : null; |
||
121 | |||
122 | // Check if the menuitem is a parent of current page, /controller for /controller/action |
||
123 | $isParent = null; |
||
124 | if (isset($item["mark-if-parent"]) && $item["mark-if-parent"] == true) { |
||
125 | $isParent = $this->isParent($item["url"]) |
||
126 | ? "selected-parent " |
||
127 | : null; |
||
128 | } |
||
129 | |||
130 | // Is there a class set for this item, then use it |
||
131 | $class = isset($item["class"]) && ! is_null($item["class"]) |
||
132 | ? $item["class"] |
||
133 | : null; |
||
134 | |||
135 | // Prepare the class-attribute, if used |
||
136 | $class = ($selected || $selectedParent || $isParent || $class) |
||
137 | ? " class=\"{$selected}{$selectedParent}{$isParent}{$class}{$subMenuClass}\" " |
||
138 | : null; |
||
139 | |||
140 | // Add the menu item |
||
141 | // $url = $menu["create_url"]($item["url"]); |
||
142 | $url = $this->url($item["url"]); |
||
143 | $html .= "\n<li{$class}>{$subMenuButton}<a href='{$url}' title='{$item['title']}'>{$item['text']}</a>{$subMenu}</li>\n"; |
||
144 | |||
145 | // To remember there is selected children when going up the menu hierarchy |
||
146 | if ($selected) { |
||
147 | $hasItemIsSelected = true; |
||
148 | } |
||
149 | } |
||
150 | |||
151 | // Return the menu |
||
152 | return array("\n<ul$ulId$ulClass>$html</ul>\n", $hasItemIsSelected); |
||
153 | }; |
||
154 | |||
155 | // Call the anonomous function to create the menu, and submenues if any. |
||
156 | $id = isset($menu["id"]) |
||
157 | ? " id=\"{$menu["id"]}\"" |
||
158 | : null; |
||
159 | $class = isset($menu["class"]) |
||
160 | ? " class=\"{$menu["class"]}\"" |
||
161 | : null; |
||
162 | |||
163 | list($html) = $createMenu( |
||
164 | $menu["items"], |
||
165 | $id, |
||
166 | $class |
||
167 | ); |
||
168 | |||
169 | // Set the id & class element, only if it exists in the menu-array |
||
170 | $wrapper = $menu["wrapper"]; |
||
171 | if ($wrapper) { |
||
172 | $html = "<{$wrapper}>{$html}</{$wrapper}>"; |
||
173 | } |
||
174 | |||
175 | return "\n{$html}\n"; |
||
176 | } |
||
178 |