Conditions | 12 |
Paths | 18 |
Total Lines | 102 |
Code Lines | 62 |
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 |
||
227 | protected function getTreeListHtml(array $fileList, string $hrefPrefix = ''): string |
||
228 | { |
||
229 | /* |
||
230 | * In this method, all directories have a trailing DIRECTORY_SEPARATOR. |
||
231 | * This is important so that $curDir doesn't become empty if we go |
||
232 | * up to the root directory ('/' on linux) |
||
233 | */ |
||
234 | $curDir = IOHelper::getCommonPathPrefix(\array_keys($fileList)); |
||
235 | $preLen = \strlen($curDir); |
||
236 | |||
237 | $indentStep = 4; |
||
238 | $indent = $indentStep; |
||
239 | $ret = '<ul>'.PHP_EOL; |
||
240 | |||
241 | foreach ($fileList as $name => $file) { |
||
242 | $dir = \dirname($name).DIRECTORY_SEPARATOR; |
||
243 | |||
244 | // Go back until the file is somewhere below curDir |
||
245 | while (!\str_starts_with($dir, $curDir)) { |
||
246 | // chop off one subDir from $curDir |
||
247 | $curDir = \substr( |
||
248 | $curDir, |
||
249 | 0, |
||
250 | \strrpos($curDir, DIRECTORY_SEPARATOR, -2) + 1 |
||
251 | ); |
||
252 | $ret .= \str_pad(' ', $indent); |
||
253 | $ret .= '</ul>'.PHP_EOL; |
||
254 | $indent -= $indentStep; |
||
255 | $ret .= \str_pad(' ', $indent); |
||
256 | $ret .= '</li>'.PHP_EOL; |
||
257 | } |
||
258 | |||
259 | if ($dir !== $curDir) { |
||
260 | // File is in a subDir of current directory |
||
261 | // relDir has no leading or trailing slash. |
||
262 | $relDir = \substr($dir, \strlen($curDir), -1); |
||
263 | $relDirs = \explode(DIRECTORY_SEPARATOR, $relDir); |
||
264 | |||
265 | foreach ($relDirs as $dirName) { |
||
266 | $curDir .= $dirName.DIRECTORY_SEPARATOR; |
||
267 | // Check how many errors/warnings are in this dir. |
||
268 | //TODO: Optimize this. Counts get recalculated for subDirs. |
||
269 | $errors = 0; |
||
270 | $warnings = 0; |
||
271 | |||
272 | foreach (\array_keys($fileList) as $fName) { |
||
273 | if (\strncmp($fName, $curDir, \strlen($curDir)) !== 0) { |
||
274 | continue; |
||
275 | } |
||
276 | |||
277 | $errors += $fileList[$fName]->getErrorCount(); |
||
278 | $warnings += $fileList[$fName]->getWarningCount(); |
||
279 | } |
||
280 | |||
281 | $count = ''; |
||
282 | |||
283 | if (0 !== $errors || 0 !== $warnings) { |
||
284 | $count .= '(<span class="errorCount">'; |
||
285 | $count .= $errors; |
||
286 | $count .= '</span>|<span class="warningCount">'; |
||
287 | $count .= $warnings.'</span>)'; |
||
288 | } |
||
289 | |||
290 | $ret .= \str_pad(' ', $indent); |
||
291 | $ret .= "<li><a class='treeDir'>{$dirName} {$count}</a>".PHP_EOL; |
||
292 | $indent += $indentStep; |
||
293 | $ret .= \str_pad(' ', $indent); |
||
294 | $ret .= '<ul>'.PHP_EOL; |
||
295 | } |
||
296 | } |
||
297 | |||
298 | $name = \str_replace('\\', '/', $name); |
||
299 | $shortName = \substr($name, $preLen); |
||
300 | $fileName = \basename($name); |
||
301 | $count = ''; |
||
302 | |||
303 | if (0 !== $file->getErrorCount() || 0 !== $file->getWarningCount()) { |
||
304 | $count .= '(<span class="errorCount">'; |
||
305 | $count .= $file->getErrorCount(); |
||
306 | $count .= '</span>|<span class="warningCount">'; |
||
307 | $count .= $file->getWarningCount(); |
||
308 | $count .= '</span>)'; |
||
309 | } |
||
310 | |||
311 | $ret .= \str_pad(' ', $indent); |
||
312 | $ret .= '<li class="php"><a class="fileLink" href="'; |
||
313 | $ret .= $hrefPrefix.$shortName.'.html">'; |
||
314 | $ret .= "{$fileName} {$count}</a></li>".PHP_EOL; |
||
315 | } |
||
316 | |||
317 | while ($indent > $indentStep) { |
||
318 | $indent -= $indentStep; |
||
319 | $ret .= \str_pad(' ', $indent); |
||
320 | $ret .= '</ul>'.PHP_EOL; |
||
321 | $indent -= $indentStep; |
||
322 | $ret .= \str_pad(' ', $indent); |
||
323 | $ret .= '</li>'.PHP_EOL; |
||
324 | } |
||
325 | |||
326 | $ret .= '</ul>'.PHP_EOL; |
||
327 | |||
328 | return $ret; |
||
329 | } |
||
355 |