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 ReflectionClassLike 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 ReflectionClassLike, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | abstract class ReflectionClassLike extends Reflection |
||
6 | { |
||
7 | use Parts\AliasTrait; |
||
8 | use Parts\DocCommentTrait; |
||
9 | use Parts\IndexableTrait; |
||
10 | |||
11 | protected $methods = []; |
||
12 | |||
13 | /** |
||
14 | * Checks if the class is a subclass of the specified class or implements a specified interface. |
||
15 | * |
||
16 | * @param string $className |
||
17 | * |
||
18 | * @return bool |
||
19 | */ |
||
20 | public function isSubclassOf($className) |
||
24 | |||
25 | /** |
||
26 | * Gets an array of methods, including inherited ones. |
||
27 | * |
||
28 | * @param int $filter Any combination of ReflectionMethod::IS_STATIC, ReflectionMethod::IS_PUBLIC, ReflectionMethod::IS_PROTECTED, ReflectionMethod::IS_PRIVATE, ReflectionMethod::IS_ABSTRACT, ReflectionMethod::IS_FINAL. |
||
29 | * |
||
30 | * @return ReflectionMethod[] |
||
31 | */ |
||
32 | 390 | public function getMethods($filter = null) |
|
68 | |||
69 | /** |
||
70 | * Gets an array of methods, without inherited ones. |
||
71 | * |
||
72 | * @param int $filter Any combination of ReflectionMethod::IS_STATIC, ReflectionMethod::IS_PUBLIC, ReflectionMethod::IS_PROTECTED, ReflectionMethod::IS_PRIVATE, ReflectionMethod::IS_ABSTRACT, ReflectionMethod::IS_FINAL. |
||
73 | * |
||
74 | * @return \Benoth\StaticReflection\Reflection\ReflectionMethod[] |
||
75 | */ |
||
76 | 408 | public function getSelfMethods($filter = null) |
|
85 | |||
86 | /** |
||
87 | * Gets a ReflectionMethod for a class method. |
||
88 | * |
||
89 | * @param string $methodSearchedName The method name to reflect |
||
90 | * |
||
91 | * @throws \ReflectionException If the method does not exist |
||
92 | * |
||
93 | * @return \Benoth\StaticReflection\Reflection\ReflectionMethod |
||
94 | */ |
||
95 | 198 | public function getMethod($methodSearchedName) |
|
105 | |||
106 | /** |
||
107 | * Gets the constructor of the reflected class. |
||
108 | * |
||
109 | * @return \Benoth\StaticReflection\Reflection\ReflectionMethod |
||
110 | */ |
||
111 | 15 | public function getConstructor() |
|
119 | |||
120 | /** |
||
121 | * Checks if a method is defined. |
||
122 | * |
||
123 | * @param string $methodSearchedName Name of the method being checked for |
||
124 | * |
||
125 | * @return bool |
||
126 | */ |
||
127 | 48 | public function hasMethod($methodSearchedName) |
|
137 | |||
138 | /** |
||
139 | * Checks if current entity implements an interface. |
||
140 | * |
||
141 | * @param string $interfaceSearchedName The interface name |
||
142 | * |
||
143 | * @return bool |
||
144 | */ |
||
145 | 15 | public function implementsInterface($interfaceSearchedName) |
|
156 | |||
157 | /** |
||
158 | * Add a method to the reflected class. |
||
159 | * |
||
160 | * @param ReflectionMethod $method |
||
161 | */ |
||
162 | 1062 | public function addMethod(ReflectionMethod $method) |
|
170 | |||
171 | /** |
||
172 | * Gets a ReflectionExtension object for the extension which defined the class. |
||
173 | * |
||
174 | * @return null|\ReflectionExtension |
||
175 | */ |
||
176 | 15 | public function getExtension() |
|
180 | |||
181 | /** |
||
182 | * Gets the name of the extension which defined the class. |
||
183 | * |
||
184 | * @return bool |
||
185 | */ |
||
186 | 15 | public function getExtensionName() |
|
190 | |||
191 | /** |
||
192 | * Checks if class is defined internally by an extension, or the core. |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | 15 | public function isInternal() |
|
200 | |||
201 | /** |
||
202 | * Checks if class is user defined. |
||
203 | * |
||
204 | * @return bool |
||
205 | */ |
||
206 | 15 | public function isUserDefined() |
|
210 | |||
211 | /** |
||
212 | * Checks if the entity is a class. |
||
213 | * |
||
214 | * @return bool |
||
215 | */ |
||
216 | 15 | public function isClass() |
|
220 | |||
221 | /** |
||
222 | * Checks if the entity is an interface. |
||
223 | * |
||
224 | * @return bool |
||
225 | */ |
||
226 | 15 | public function isInterface() |
|
230 | |||
231 | /** |
||
232 | * Checks if the entity is a trait. |
||
233 | * |
||
234 | * @return bool |
||
235 | */ |
||
236 | 15 | public function isTrait() |
|
240 | |||
241 | /** |
||
242 | * Filter an array of methods. |
||
243 | * |
||
244 | * @param \Benoth\StaticReflection\Reflection\ReflectionMethod[] $methods |
||
245 | * @param int $filter Any combination of ReflectionMethod::IS_STATIC, ReflectionMethod::IS_PUBLIC, ReflectionMethod::IS_PROTECTED, ReflectionMethod::IS_PRIVATE, ReflectionMethod::IS_ABSTRACT, ReflectionMethod::IS_FINAL. |
||
246 | * |
||
247 | * @return \Benoth\StaticReflection\Reflection\ReflectionMethod[] |
||
248 | */ |
||
249 | 408 | protected function filterMethods(array $methods, $filter) |
|
273 | } |
||
274 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.