Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Finder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Finder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Finder |
||
16 | { |
||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $defaultExtension = 'php'; |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $paths = []; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $root; |
||
31 | |||
32 | /** |
||
33 | * @var boolean |
||
34 | */ |
||
35 | protected $returnHandlers = false; |
||
36 | |||
37 | /** |
||
38 | * @var null|boolean |
||
39 | */ |
||
40 | protected $nextAsHandlers = null; |
||
41 | |||
42 | /** |
||
43 | * @param array $path |
||
|
|||
44 | * @param string $defaultExtension |
||
45 | * @param string $root |
||
46 | */ |
||
47 | 5 | public function __construct(array $paths = null, $defaultExtension = null, $root = null) |
|
61 | |||
62 | /** |
||
63 | * Wether to return handlers |
||
64 | * |
||
65 | * @param boolean $returnHandlers |
||
66 | */ |
||
67 | 1 | public function returnHandlers($returnHandlers = true) |
|
71 | |||
72 | /** |
||
73 | * Wether to let the next find result return handlers |
||
74 | * |
||
75 | * @param boolean $returnHandlers |
||
76 | */ |
||
77 | 2 | public function asHandlers($returnHandlers = true) |
|
81 | |||
82 | /** |
||
83 | * Wether to let the next find result return a handler |
||
84 | * |
||
85 | * @param boolean $returnHandler |
||
86 | */ |
||
87 | 1 | public function asHandler($returnHandler = true) |
|
91 | |||
92 | /** |
||
93 | * Returns the root |
||
94 | * |
||
95 | * @return string |
||
96 | */ |
||
97 | 1 | public function getRoot() |
|
101 | |||
102 | /** |
||
103 | * Sets a root restriction |
||
104 | * |
||
105 | * @param string $root |
||
106 | */ |
||
107 | 2 | public function setRoot($root) |
|
116 | |||
117 | /** |
||
118 | * Adds paths to look in |
||
119 | * |
||
120 | * @param array $paths |
||
121 | * @param boolean $clearCache |
||
122 | */ |
||
123 | 2 | public function addPaths(array $paths, $clearCache = true, $group = '__DEFAULT__') |
|
127 | |||
128 | /** |
||
129 | * Adds a path |
||
130 | * |
||
131 | * @param string $path |
||
132 | * @param boolean $clearCache |
||
133 | */ |
||
134 | 4 | View Code Duplication | public function addPath($path, $clearCache = true, $group = '__DEFAULT__') |
148 | |||
149 | /** |
||
150 | * Removes paths to look in |
||
151 | * |
||
152 | * @param array $paths |
||
153 | */ |
||
154 | 1 | public function removePaths(array $paths, $clearCache = true, $group = '__DEFAULT__') |
|
158 | |||
159 | /** |
||
160 | * Removes a path |
||
161 | * |
||
162 | * @param string $path |
||
163 | */ |
||
164 | 1 | public function removePath($path, $clearCache = true, $group = '__DEFAULT__') |
|
175 | |||
176 | /** |
||
177 | * Removes path cache |
||
178 | * |
||
179 | * @param string $path |
||
180 | */ |
||
181 | 1 | public function removePathCache($path) |
|
191 | |||
192 | /** |
||
193 | * Normalizes a path |
||
194 | * |
||
195 | * @param string $path |
||
196 | * |
||
197 | * @return string |
||
198 | * |
||
199 | * @throws \Exception |
||
200 | */ |
||
201 | 4 | public function normalizePath($path) |
|
213 | |||
214 | /** |
||
215 | * Returns the paths set up to look in |
||
216 | * |
||
217 | * @return array |
||
218 | */ |
||
219 | 1 | public function getPaths($group = '__DEFAULT__') |
|
228 | |||
229 | /** |
||
230 | * Retrieves the path groups |
||
231 | * |
||
232 | * @return array |
||
233 | */ |
||
234 | 1 | public function getGroups() |
|
238 | |||
239 | /** |
||
240 | * Replaces all the paths |
||
241 | * |
||
242 | * @param array $paths |
||
243 | */ |
||
244 | 1 | View Code Duplication | public function setPaths(array $paths, $clearCache = true, $group = '__DEFAULT__') |
254 | |||
255 | /** |
||
256 | * Finds all files with a given name/subpath. |
||
257 | * |
||
258 | * @param string $name |
||
259 | * @param boolean $reload |
||
260 | * @param boolean $reversed |
||
261 | * @param string $type |
||
262 | */ |
||
263 | 2 | public function findAll($name, $reload = false, $reversed = false, $type = 'all') |
|
320 | |||
321 | /** |
||
322 | * Finds all files with a given name/subpath |
||
323 | * |
||
324 | * @param string $name |
||
325 | * @param boolean $reload |
||
326 | * @param boolean $reversed |
||
327 | */ |
||
328 | 2 | public function findAllFiles($name, $reload = false, $reversed = false) |
|
332 | |||
333 | /** |
||
334 | * Finds all directories with a given name/subpath |
||
335 | * |
||
336 | * @param string $name |
||
337 | * @param boolean $reload |
||
338 | * @param boolean $reversed |
||
339 | */ |
||
340 | 2 | public function findAllDirs($name, $reload = false, $reversed = false) |
|
344 | |||
345 | /** |
||
346 | * Reverse-finds all files and directories with a given name/subpath |
||
347 | * |
||
348 | * @param string $name |
||
349 | * @param boolean $reload |
||
350 | * @param string $type |
||
351 | */ |
||
352 | 1 | public function findAllReversed($name, $reload = false, $type = 'all') |
|
356 | |||
357 | /** |
||
358 | * Reverse-finds all directories with a given name/subpath |
||
359 | * |
||
360 | * @param string $name |
||
361 | * @param boolean $reload |
||
362 | */ |
||
363 | 1 | public function findAllDirsReversed($name, $reload = false) |
|
367 | |||
368 | /** |
||
369 | * Reverse-finds all files with a given name/subpath |
||
370 | * |
||
371 | * @param string $name |
||
372 | * @param boolean $reload |
||
373 | */ |
||
374 | 1 | public function findAllFilesReversed($name, $reload = false) |
|
378 | |||
379 | /** |
||
380 | * Finds one file or directories with a given name/subpath |
||
381 | * |
||
382 | * @param string $name |
||
383 | * @param boolean $reload |
||
384 | * @param boolean $reversed |
||
385 | * @param string $type |
||
386 | */ |
||
387 | 3 | public function find($name, $reload = false, $reversed = false, $type = 'all') |
|
462 | |||
463 | /** |
||
464 | * Finds one file with a given name/subpath |
||
465 | * |
||
466 | * @param string $name |
||
467 | * @param boolean $reload |
||
468 | * @param boolean $reversed |
||
469 | */ |
||
470 | 3 | public function findFile($name, $reload = false, $reversed = false) |
|
474 | |||
475 | /** |
||
476 | * Finds one directories with a given name/subpath |
||
477 | * |
||
478 | * @param string $name |
||
479 | * @param boolean $reload |
||
480 | * @param boolean $reversed |
||
481 | */ |
||
482 | 2 | public function findDir($name, $reload = false, $reversed = false) |
|
486 | |||
487 | /** |
||
488 | * Reverse-finds one file or directory with a given name/subpath |
||
489 | * |
||
490 | * @param string $name |
||
491 | * @param boolean $reload |
||
492 | * @param boolean $reversed |
||
493 | * @param string $type |
||
494 | */ |
||
495 | 1 | public function findReversed($name, $reload = false, $type = 'all') |
|
499 | |||
500 | /** |
||
501 | * Reverse-finds one file with a given name/subpath |
||
502 | * |
||
503 | * @param string $name |
||
504 | * @param boolean $reload |
||
505 | * @param boolean $reversed |
||
506 | */ |
||
507 | 1 | public function findFileReversed($name, $reload = false) |
|
511 | |||
512 | /** |
||
513 | * Reverse-finds one directory with a given name/subpath |
||
514 | * |
||
515 | * @param string $name |
||
516 | * @param boolean $reload |
||
517 | * @param boolean $reversed |
||
518 | */ |
||
519 | 1 | public function findDirReversed($name, $reload = false) |
|
523 | |||
524 | /** |
||
525 | * Retrieves a location from cache |
||
526 | * |
||
527 | * @param string $scope |
||
528 | * @param string $name |
||
529 | * @param boolean $reversed |
||
530 | * |
||
531 | * @return string|array |
||
532 | */ |
||
533 | 3 | public function findCached($scope, $name, $reversed) |
|
542 | |||
543 | /** |
||
544 | * Clears the location cache |
||
545 | */ |
||
546 | 1 | public function clearCache() |
|
550 | |||
551 | /** |
||
552 | * Caches a find result |
||
553 | * |
||
554 | * @param string $scope |
||
555 | * @param string $name |
||
556 | * @param boolean $reversed |
||
557 | * @param array $pathsUsed |
||
558 | */ |
||
559 | 3 | public function cache($scope, $name, $reversed, $result, $pathsUsed = []) |
|
567 | |||
568 | /** |
||
569 | * Generates a cache key |
||
570 | * |
||
571 | * @param string $scope |
||
572 | * @param string $name |
||
573 | * @param boolean $reversed |
||
574 | * |
||
575 | * @return string |
||
576 | */ |
||
577 | 3 | public function makeCacheKey($scope, $name, $reversed) |
|
588 | |||
589 | /** |
||
590 | * Normalizes a file name |
||
591 | * |
||
592 | * @param string $name |
||
593 | * |
||
594 | * @return string |
||
595 | */ |
||
596 | 3 | public function normalizeFileName($name) |
|
605 | |||
606 | /** |
||
607 | * Sets the default extension |
||
608 | * |
||
609 | * @param string $extension |
||
610 | */ |
||
611 | 2 | public function setDefaultExtension($extension) |
|
615 | } |
||
616 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.