Complex classes like ReflectionUtility 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 ReflectionUtility, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class ReflectionUtility |
||
22 | { |
||
23 | /** |
||
24 | * Create a new class reflection. Do not use the makeInstance or objectManager |
||
25 | * because the reflection API is also used in front of the caching framework. |
||
26 | * |
||
27 | * @param string $className |
||
28 | * |
||
29 | * @return ClassReflection |
||
30 | */ |
||
31 | public static function createReflectionClass($className) |
||
35 | |||
36 | /** |
||
37 | * Check if the given class is instantiable. |
||
38 | * |
||
39 | * @param string $className |
||
40 | * |
||
41 | * @return bool |
||
42 | */ |
||
43 | public static function isInstantiable($className):bool |
||
53 | |||
54 | /** |
||
55 | * Get the name of the parent class. |
||
56 | * |
||
57 | * @param string $className |
||
58 | * |
||
59 | * @return string |
||
60 | */ |
||
61 | public static function getParentClassName($className) |
||
71 | |||
72 | /** |
||
73 | * Get all properties that are tagged with the given tag. |
||
74 | * |
||
75 | * @param string $className |
||
76 | * @param string $tag |
||
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | public static function getPropertiesTaggedWith($className, $tag) |
||
93 | |||
94 | /** |
||
95 | * Get all properties that are tagged with the given tag. |
||
96 | * |
||
97 | * @param string $className |
||
98 | * @param string $tag |
||
99 | * |
||
100 | * @return array |
||
101 | */ |
||
102 | public static function getPropertyNamesTaggedWith($className, $tag):array |
||
114 | |||
115 | /** |
||
116 | * Get all public methods of the given class. |
||
117 | * |
||
118 | * @param string $className |
||
119 | * |
||
120 | * @return MethodReflection[] |
||
121 | */ |
||
122 | public static function getPublicMethods($className) |
||
127 | |||
128 | /** |
||
129 | * Get first class tag information. |
||
130 | * The trimmed value if the tag exists and FALSE if the tag do not exists. |
||
131 | * |
||
132 | * @param string $className |
||
133 | * @param string $tag |
||
134 | * |
||
135 | * @return string|bool |
||
136 | */ |
||
137 | public static function getFirstTagValue(string $className, string $tag) |
||
156 | |||
157 | /** |
||
158 | * Get the tag configuration from this method and respect multiple line and space configuration. |
||
159 | * |
||
160 | * @param MethodReflection|ClassReflection $reflectionObject |
||
161 | * @param array $tagNames |
||
162 | * |
||
163 | * @return array |
||
164 | */ |
||
165 | public static function getTagConfiguration($reflectionObject, array $tagNames): array |
||
184 | |||
185 | |||
186 | /** |
||
187 | * Get the tag configuration from this method and respect multiple line and space configuration. |
||
188 | * |
||
189 | * @param string $className |
||
190 | * @param array $tagNames |
||
191 | * |
||
192 | * @return array |
||
193 | */ |
||
194 | public static function getTagConfigurationForMethod($className, $methodName, array $tagNames): array |
||
215 | |||
216 | |||
217 | /** |
||
218 | * Get the tag configuration from this method and respect multiple line and space configuration. |
||
219 | * |
||
220 | * @param MethodReflection|ClassReflection $reflectionObject |
||
|
|||
221 | * @param array $tagNames |
||
222 | * |
||
223 | * @return array |
||
224 | */ |
||
225 | public static function getTagConfigurationForClass($className, array $tagNames): array |
||
246 | /** |
||
247 | * Get the tag configuration from this method and respect multiple line and space configuration. |
||
248 | * |
||
249 | * @param MethodReflection|ClassReflection $reflectionObject |
||
250 | * @param array $tagNames |
||
251 | * |
||
252 | * @return array |
||
253 | */ |
||
254 | public static function getTagConfigurationForProperty($className, $property, array $tagNames): array |
||
275 | |||
276 | |||
277 | |||
278 | /** |
||
279 | * Get public method names |
||
280 | * |
||
281 | * @param string $className |
||
282 | * @return array |
||
283 | */ |
||
284 | public static function getPropertyNames(string $className): array |
||
289 | |||
290 | /** |
||
291 | * Get public method names |
||
292 | * |
||
293 | * @param string $className |
||
294 | * @return array |
||
295 | */ |
||
296 | public static function getPublicMethodNames(string $className): array |
||
317 | |||
318 | /** |
||
319 | * Get properties of the given class, that are als declared in the given class. |
||
320 | * |
||
321 | * @param string $className |
||
322 | * |
||
323 | * @return array |
||
324 | */ |
||
325 | public static function getDeclaringProperties($className) |
||
335 | |||
336 | /** |
||
337 | * Is 9 or higher |
||
338 | * |
||
339 | * @return bool |
||
340 | */ |
||
341 | public static function is9orHigher(): bool |
||
345 | } |
||
346 |
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.