| Conditions | 14 |
| Paths | 6 |
| Total Lines | 73 |
| Code Lines | 50 |
| 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 namespace EvolutionCMS\Support; |
||
| 67 | public function drawSub($parentid, $level) |
||
| 68 | { |
||
| 69 | $modx = evolutionCMS(); |
||
| 70 | |||
| 71 | $output = ''; |
||
| 72 | |||
| 73 | if (isset($this->menu[$parentid])) { |
||
| 74 | |||
| 75 | $ph = array(); |
||
| 76 | $countChild = 0; |
||
| 77 | $itemTpl = ' |
||
| 78 | <li id="[+id+]" class="[+li_class+]"><a href="[+href+]" alt="[+alt+]" target="[+target+]" onclick="[+onclick+]"[+a_class+] [+LinkAttr+]>[+itemName+]</a>[+DrawSub+]</li>'; |
||
| 79 | $outerTpl = '<ul id="[+id+]" class="[+class+]">[+output+]</ul>'; |
||
| 80 | foreach ($this->menu[$parentid] as $key => $value) { |
||
| 81 | if ($value[6] !== '') { |
||
| 82 | $permissions = explode(',', $value[6]); |
||
| 83 | foreach ($permissions as $val) { |
||
| 84 | if (!$modx->hasPermission($val)) { |
||
| 85 | continue; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | $countChild++; |
||
| 91 | $id = $value[0]; |
||
| 92 | $ph['id'] = $id; |
||
| 93 | $ph['li_class'] = $this->getItemClass($id) . $value[10]; |
||
| 94 | $ph['href'] = $value[3]; |
||
| 95 | $ph['alt'] = $value[4]; |
||
| 96 | $ph['target'] = $value[7]; |
||
| 97 | $ph['onclick'] = $value[5]; |
||
| 98 | $ph['a_class'] = $this->getLinkClass($id); |
||
| 99 | $ph['LinkAttr'] = $this->getLinkAttr($id); |
||
| 100 | $ph['itemName'] = $value[2] . $this->getItemName($id); |
||
| 101 | |||
| 102 | $ph['DrawSub'] = ''; |
||
| 103 | |||
| 104 | if (isset($this->menu[$id])) { |
||
| 105 | $level++; |
||
| 106 | $ph['DrawSub'] = $this->drawSub($id, $level); |
||
| 107 | $level--; |
||
| 108 | // Optional buttons |
||
| 109 | } else { |
||
| 110 | if (isset($value[11]) && !empty($value[11])) { |
||
| 111 | $optionalButton = ''; |
||
| 112 | if (is_array($value[11])) { |
||
| 113 | foreach ($value[11] as $opt) { |
||
| 114 | $optionalButton .= sprintf('<%s href="%s" class="%s" onclick="%s" title="%s">%s</%s>', |
||
| 115 | $opt[0], $opt[1], $opt[2], $opt[3], $opt[4], $opt[5], $opt[0]); |
||
| 116 | } |
||
| 117 | } else { |
||
| 118 | $opt = $value[11]; |
||
| 119 | $optionalButton = sprintf('<%s href="%s" class="%s" onclick="%s" title="%s">%s</%s>', |
||
| 120 | $opt[0], $opt[1], $opt[2], $opt[3], $opt[4], $opt[5], $opt[0]); |
||
| 121 | } |
||
| 122 | $ph['DrawSub'] = $optionalButton; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | $output .= $modx->parseText($itemTpl, $ph); |
||
| 127 | } |
||
| 128 | |||
| 129 | $ph = array(); |
||
| 130 | if ($countChild > 0) { |
||
| 131 | $ph['id'] = $level == 0 ? $this->defaults['outerClass'] : ''; |
||
| 132 | $ph['class'] = $level == 0 ? $this->defaults['outerClass'] : $this->defaults['innerClass']; |
||
| 133 | $ph['output'] = $output; |
||
| 134 | $output = $modx->parseText($outerTpl, $ph); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | return $output; |
||
| 139 | } |
||
| 140 | |||
| 193 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.