Conditions | 22 |
Paths | 265 |
Total Lines | 98 |
Lines | 8 |
Ratio | 8.16 % |
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 |
||
48 | public function renderNavigation($activeCategory = 0) |
||
49 | { |
||
50 | global $sids, $PMF_LANG; |
||
51 | |||
52 | $open = 0; |
||
53 | $output = ''; |
||
54 | $numCategories = $this->Category->height(); |
||
55 | $numFaqs = $this->Category->getNumberOfRecordsOfCategory(); |
||
56 | |||
57 | if ($numCategories > 0) { |
||
58 | for ($y = 0; $y < $numCategories; $y = $this->Category->getNextLineTree($y)) { |
||
59 | list($hasChild, $name, $categoryId, $description, $active) = $this->Category->getLineDisplay($y); |
||
60 | |||
61 | if (!$active) { |
||
62 | continue; |
||
63 | } |
||
64 | |||
65 | if ($activeCategory == $categoryId) { |
||
66 | $isActive = true; |
||
67 | } else { |
||
68 | $isActive = false; |
||
69 | } |
||
70 | |||
71 | $level = $this->Category->treeTab[$y]['level']; |
||
72 | $leveldiff = $open - $level; |
||
73 | |||
74 | if ($this->_config->get('records.hideEmptyCategories') && !isset($numFaqs[$categoryId]) && |
||
75 | '-' === $hasChild) { |
||
76 | continue; |
||
77 | } |
||
78 | |||
79 | View Code Duplication | if ($leveldiff > 1) { |
|
80 | $output .= '</li>'; |
||
81 | for ($i = $leveldiff; $i > 1; --$i) { |
||
82 | $output .= sprintf("\n%s</ul>\n%s</li>\n", |
||
83 | str_repeat("\t", $level + $i + 1), |
||
84 | str_repeat("\t", $level + $i)); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | if ($level < $open) { |
||
89 | if (($level - $open) == -1) { |
||
90 | $output .= '</li>'; |
||
91 | } |
||
92 | $output .= "\n".str_repeat("\t", $level + 2)."</ul>\n".str_repeat("\t", $level + 1)."</li>\n"; |
||
93 | } elseif ($level == $open && $y != 0) { |
||
94 | $output .= "</li>\n"; |
||
95 | } |
||
96 | |||
97 | if ($level > $open) { |
||
98 | $output .= sprintf( |
||
99 | "\n%s<ul class=\"nav nav-list\">\n%s<li%s>", |
||
100 | str_repeat("\t", $level + 1), |
||
101 | str_repeat("\t", $level + 1), |
||
102 | $isActive ? ' class="active"' : '' |
||
103 | ); |
||
104 | } else { |
||
105 | $output .= sprintf( |
||
106 | '%s<li%s>', |
||
107 | str_repeat("\t", $level + 1), |
||
108 | $isActive ? ' class="active"' : '' |
||
109 | ); |
||
110 | } |
||
111 | |||
112 | if (isset($this->Category->treeTab[$y]['symbol']) && $this->Category->treeTab[$y]['symbol'] == 'plus') { |
||
113 | $output .= $this->Category->addCategoryLink( |
||
114 | $sids, $categoryId, $name, $description, true, $isActive |
||
115 | ); |
||
116 | } else { |
||
117 | if ($this->Category->treeTab[$y]['symbol'] == 'minus') { |
||
118 | $name = ($this->Category->treeTab[$y]['parent_id'] === 0) |
||
119 | ? |
||
120 | $name |
||
121 | : |
||
122 | $this->Category->categoryName[$this->Category->treeTab[$y]['id']]['name']; |
||
123 | $output .= $this->Category->addCategoryLink( |
||
124 | $sids, $categoryId, $name, $description, false, $isActive |
||
125 | ); |
||
126 | } else { |
||
127 | $output .= $this->Category->addCategoryLink( |
||
128 | $sids, $categoryId, $name, $description, false, $isActive |
||
129 | ); |
||
130 | } |
||
131 | } |
||
132 | $open = $level; |
||
133 | } |
||
134 | if ($open > 0) { |
||
135 | $output .= str_repeat("</li>\n\t</ul>\n\t", $open); |
||
136 | } |
||
137 | $output .= '</li>'; |
||
138 | |||
139 | return $output; |
||
140 | } else { |
||
141 | $output = '<li><a href="#">'.$PMF_LANG['no_cats'].'</a></li>'; |
||
142 | } |
||
143 | |||
144 | return $output; |
||
145 | } |
||
146 | |||
318 |
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
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key 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.