Conditions | 19 |
Paths | 137 |
Total Lines | 96 |
Lines | 11 |
Ratio | 11.46 % |
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 |
||
152 | public function renderCategoryDropDown() |
||
153 | { |
||
154 | global $sids, $PMF_LANG; |
||
155 | |||
156 | $open = 0; |
||
157 | $output = ''; |
||
158 | $numCategories = $this->Category->height(); |
||
159 | |||
160 | $this->Category->expandAll(); |
||
161 | |||
162 | if ($numCategories > 0) { |
||
163 | for ($y = 0; $y < $this->Category->height(); $y = $this->Category->getNextLineTree($y)) { |
||
164 | list($hasChild, $categoryName, $parent, $description, $active) = $this->Category->getLineDisplay($y); |
||
165 | |||
166 | if (!$active) { |
||
167 | continue; |
||
168 | } |
||
169 | |||
170 | $level = $this->Category->treeTab[$y]['level']; |
||
171 | $leveldiff = $open - $level; |
||
172 | $numChilds = $this->Category->treeTab[$y]['numChilds']; |
||
173 | |||
174 | if (!isset($number[$parent])) { |
||
175 | $number[$parent] = 0; |
||
|
|||
176 | } |
||
177 | |||
178 | View Code Duplication | if ($this->_config->get('records.hideEmptyCategories') && 0 === $number[$parent] && '-' === $hasChild) { |
|
179 | continue; |
||
180 | } |
||
181 | |||
182 | View Code Duplication | if ($leveldiff > 1) { |
|
183 | $output .= '</li>'; |
||
184 | for ($i = $leveldiff; $i > 1; --$i) { |
||
185 | $output .= sprintf("\n%s</ul>\n%s</li>\n", |
||
186 | str_repeat("\t", $level + $i + 1), |
||
187 | str_repeat("\t", $level + $i)); |
||
188 | } |
||
189 | } |
||
190 | |||
191 | if ($level < $open) { |
||
192 | if (($level - $open) == -1) { |
||
193 | $output .= '</li>'; |
||
194 | } |
||
195 | $output .= sprintf("\n%s</ul>\n%s</li>\n", |
||
196 | str_repeat("\t", $level + 2), |
||
197 | str_repeat("\t", $level + 1)); |
||
198 | } elseif ($level == $open && $y != 0) { |
||
199 | $output .= "</li>\n"; |
||
200 | } |
||
201 | |||
202 | if ($level > $open) { |
||
203 | $output .= sprintf( |
||
204 | "\n%s<ul class=\"dropdown-menu\">\n%s", |
||
205 | str_repeat("\t", $level + 1), |
||
206 | str_repeat("\t", $level + 1) |
||
207 | ); |
||
208 | if ($numChilds > 0) { |
||
209 | $output .= '<li class="dropdown-submenu">'; |
||
210 | } else { |
||
211 | $output .= '<li>'; |
||
212 | } |
||
213 | } else { |
||
214 | $output .= str_repeat("\t", $level + 1); |
||
215 | if ($numChilds > 0) { |
||
216 | $output .= '<li class="dropdown-submenu">'; |
||
217 | } else { |
||
218 | $output .= '<li>'; |
||
219 | } |
||
220 | } |
||
221 | |||
222 | $url = sprintf( |
||
223 | '%s?%saction=show&cat=%d', |
||
224 | Link::getSystemRelativeUri(), |
||
225 | $sids, |
||
226 | $parent |
||
227 | ); |
||
228 | $oLink = new Link($url, $this->_config); |
||
229 | $oLink->itemTitle = $categoryName; |
||
230 | $oLink->text = $categoryName; |
||
231 | $oLink->tooltip = $description; |
||
232 | |||
233 | $output .= $oLink->toHtmlAnchor(); |
||
234 | $open = $level; |
||
235 | } |
||
236 | |||
237 | if (isset($level) && $level > 0) { |
||
238 | $output .= str_repeat("</li>\n\t</ul>\n\t", $level); |
||
239 | } |
||
240 | |||
241 | return $output; |
||
242 | } else { |
||
243 | $output = '<li><a href="#">'.$PMF_LANG['no_cats'].'</a></li>'; |
||
244 | } |
||
245 | |||
246 | return $output; |
||
247 | } |
||
248 | |||
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.