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:
1 | <?php declare(strict_types=1); |
||
27 | class ClassFinder implements IClassFinder |
||
28 | { |
||
29 | /** |
||
30 | * The composer class loader as returned from `vendor/autoload.php`. |
||
31 | * |
||
32 | * @var ClassLoader |
||
33 | */ |
||
34 | protected $composer; |
||
35 | |||
36 | /** |
||
37 | * This will be filled with fully qualified class names as they are found |
||
38 | * by searching through the various class maps provided by composer. |
||
39 | * |
||
40 | * @var array|string[] |
||
41 | */ |
||
42 | protected $foundClasses = []; |
||
43 | |||
44 | /** |
||
45 | * The namespace to filter by will be stored here. |
||
46 | * |
||
47 | * > NOTE: This must be set, otherwise we could have a |
||
48 | * very large data set to search through. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $namespace; |
||
53 | |||
54 | /** |
||
55 | * The interface name to filter by will be stored here. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $implements; |
||
60 | |||
61 | /** |
||
62 | * The parent class to filter by will be stored here. |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $extends; |
||
67 | |||
68 | /** |
||
69 | * An optional custom filter method can be set. |
||
70 | * Otherwise we will use the `defaultFilter` method in this class. |
||
71 | * |
||
72 | * @var Closure |
||
73 | */ |
||
74 | protected $filter; |
||
75 | |||
76 | /** |
||
77 | * Constructor. |
||
78 | * |
||
79 | * @param ClassLoader $composer We rely on the information provided by the |
||
80 | * composer class maps in order to find classes |
||
81 | * for you. |
||
82 | */ |
||
83 | public function __construct(ClassLoader $composer) |
||
87 | |||
88 | public function namespace(string $namespace): IClassFinder |
||
92 | |||
93 | public function implements(string $interface): IClassFinder |
||
97 | |||
98 | public function extends(string $parent): IClassFinder |
||
102 | |||
103 | public function filterBy(Closure $filter): IClassFinder |
||
116 | |||
117 | public function search(): array |
||
136 | |||
137 | public function getIterator(): Traversable |
||
141 | |||
142 | public function count(): int |
||
146 | |||
147 | /** |
||
148 | * Searches the composer class map. |
||
149 | * |
||
150 | * Results are added to the `$foundClasses` array. |
||
151 | * |
||
152 | * @return void |
||
153 | */ |
||
154 | protected function searchClassMap() |
||
164 | |||
165 | /** |
||
166 | * Searches the composer PSR-x class maps. |
||
167 | * |
||
168 | * Results are added to the `$foundClasses` array. |
||
169 | * |
||
170 | * @return void |
||
171 | */ |
||
172 | protected function searchPsrMaps() |
||
217 | |||
218 | /** |
||
219 | * Runs a filter over the array of `$foundCLasses`. |
||
220 | * |
||
221 | * Also ensures the classes are real by creating a ReflectionClass instance. |
||
222 | * By default we use the `defaultFilter` method. |
||
223 | * |
||
224 | * @return void |
||
225 | */ |
||
226 | protected function runFilter() |
||
254 | |||
255 | /** |
||
256 | * The default filter run by `runFilter()`. |
||
257 | * |
||
258 | * Further filters by interface or parent class and also filters |
||
259 | * out actual Interfaces, Abstract Classes and Traits. |
||
260 | * |
||
261 | * @param ReflectionClass $rClass |
||
262 | * @return bool |
||
263 | */ |
||
264 | protected function defaultFilter(ReflectionClass $rClass) |
||
278 | } |