Conditions | 17 |
Paths | 4 |
Total Lines | 81 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
24 | public function create() |
||
25 | { |
||
26 | // Keep default options in an array and merge with incoming options that can override the defaults. |
||
27 | $default = array( |
||
28 | 'id' => null, |
||
29 | 'class' => null, |
||
30 | 'wrapper' => 'nav', |
||
31 | 'create_url' => function ($url) { |
||
32 | return $url; |
||
33 | }, |
||
34 | ); |
||
35 | $menu = array_replace_recursive($default, $this->config); |
||
36 | |||
37 | // Create the ul li menu from the array, use an anonomous recursive function that returns an |
||
38 | // array of values. |
||
39 | $createMenu = function ($items, $callback) use (&$createMenu, $menu) { |
||
40 | |||
41 | $html = null; |
||
42 | $hasItemIsSelected = false; |
||
43 | |||
44 | foreach ($items as $item) { |
||
45 | |||
46 | // has submenu, call recursivly and keep track on if the submenu has a selected item in it. |
||
47 | $submenu = null; |
||
48 | $selectedParent = null; |
||
49 | |||
50 | if (isset($item['submenu'])) { |
||
51 | list($submenu, $selectedParent) = $createMenu($item['submenu']['items'], $callback); |
||
52 | $selectedParent = $selectedParent |
||
53 | ? "selected-parent " |
||
54 | : null; |
||
55 | } |
||
56 | |||
57 | // Check if the current menuitem is selected |
||
58 | $selected = $callback($item['url']) |
||
59 | ? "selected " |
||
60 | : null; |
||
61 | |||
62 | // Check if the menuitem is a parent of current page, /controller for /controller/action |
||
63 | $isParent = null; |
||
64 | if (isset($item['mark-if-parent-of']) && $item['mark-if-parent-of'] == true) { |
||
65 | $isParent = $menu['is_parent']($item['mark-if-parent-of']) |
||
66 | ? "is-parent " |
||
67 | : null; |
||
68 | } |
||
69 | |||
70 | // Is there a class set for this item, then use it |
||
71 | $class = isset($item['class']) && ! is_null($item['class']) |
||
72 | ? $item['class'] |
||
73 | : null; |
||
74 | |||
75 | // Prepare the class-attribute, if used |
||
76 | $class = ($selected || $selectedParent || $isParent || $class) |
||
77 | ? " class='{$selected}{$selectedParent}{$isParent}{$class}' " |
||
78 | : null; |
||
79 | |||
80 | // Add the menu item |
||
81 | $url = $menu['create_url']($item['url']); |
||
82 | $html .= "\n<li{$class}><a href='{$url}' title='{$item['title']}'>{$item['text']}</a>{$submenu}</li>\n"; |
||
83 | |||
84 | // To remember there is selected children when going up the menu hierarchy |
||
85 | if ($selected) { |
||
86 | $hasItemIsSelected = true; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | // Return the menu |
||
91 | return array("\n<ul>$html</ul>\n", $hasItemIsSelected); |
||
92 | }; |
||
93 | |||
94 | // Call the anonomous function to create the menu, and submenues if any. |
||
95 | list($html) = $createMenu($menu['items'], $menu['callback']); |
||
96 | |||
97 | |||
98 | // Set the id & class element, only if it exists in the menu-array |
||
99 | $id = isset($menu['id']) ? " id='{$menu['id']}'" : null; |
||
100 | $class = isset($menu['class']) ? " class='{$menu['class']}'" : null; |
||
101 | $wrapper = $menu['wrapper']; |
||
102 | |||
103 | return "\n<{$wrapper}{$id}{$class}>{$html}</{$wrapper}>\n"; |
||
104 | } |
||
105 | } |
||
106 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: