Complex classes like ReflectionFunctionLikeTrait 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 ReflectionFunctionLikeTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | trait ReflectionFunctionLikeTrait |
||
30 | { |
||
31 | use InitializationTrait; |
||
32 | |||
33 | /** |
||
34 | * @var FunctionLike |
||
35 | */ |
||
36 | protected $functionLikeNode; |
||
37 | |||
38 | /** |
||
39 | * Namespace name |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $namespaceName = ''; |
||
44 | |||
45 | /** |
||
46 | * @var array|ReflectionParameter[] |
||
47 | */ |
||
48 | protected $parameters; |
||
49 | |||
50 | /** |
||
51 | * {@inheritDoc} |
||
52 | */ |
||
53 | 65 | public function getClosureScopeClass() |
|
59 | |||
60 | /** |
||
61 | * {@inheritDoc} |
||
62 | */ |
||
63 | 65 | public function getClosureThis() |
|
69 | |||
70 | 129 | public function getDocComment() |
|
76 | |||
77 | 129 | public function getEndLine() |
|
81 | |||
82 | 65 | public function getExtension() |
|
86 | |||
87 | 65 | public function getExtensionName() |
|
91 | |||
92 | 71 | public function getFileName() |
|
96 | |||
97 | /** |
||
98 | * {@inheritDoc} |
||
99 | */ |
||
100 | 2028 | public function getName() |
|
110 | |||
111 | /** |
||
112 | * {@inheritDoc} |
||
113 | */ |
||
114 | 71 | public function getNamespaceName() |
|
118 | |||
119 | /** |
||
120 | * Get the number of parameters that a function defines, both optional and required. |
||
121 | * |
||
122 | * @link http://php.net/manual/en/reflectionfunctionabstract.getnumberofparameters.php |
||
123 | * |
||
124 | * @return int |
||
125 | */ |
||
126 | 115 | public function getNumberOfParameters() |
|
130 | |||
131 | /** |
||
132 | * Get the number of required parameters that a function defines. |
||
133 | * |
||
134 | * @link http://php.net/manual/en/reflectionfunctionabstract.getnumberofrequiredparameters.php |
||
135 | * |
||
136 | * @return int |
||
137 | */ |
||
138 | 65 | public function getNumberOfRequiredParameters() |
|
149 | |||
150 | /** |
||
151 | * {@inheritDoc} |
||
152 | */ |
||
153 | 207 | public function getParameters() |
|
174 | |||
175 | /** |
||
176 | * Gets the specified return type of a function |
||
177 | * |
||
178 | * @return \ReflectionType |
||
179 | * |
||
180 | * @link http://php.net/manual/en/reflectionfunctionabstract.getreturntype.php |
||
181 | */ |
||
182 | 65 | public function getReturnType() |
|
204 | |||
205 | /** |
||
206 | * {@inheritDoc} |
||
207 | */ |
||
208 | 65 | public function getShortName() |
|
216 | |||
217 | 129 | public function getStartLine() |
|
221 | |||
222 | /** |
||
223 | * {@inheritDoc} |
||
224 | */ |
||
225 | 65 | public function getStaticVariables() |
|
245 | |||
246 | /** |
||
247 | * Checks if the function has a specified return type |
||
248 | * |
||
249 | * @return bool |
||
250 | * |
||
251 | * @link http://php.net/manual/en/reflectionfunctionabstract.hasreturntype.php |
||
252 | */ |
||
253 | 66 | public function hasReturnType() |
|
259 | |||
260 | /** |
||
261 | * {@inheritDoc} |
||
262 | */ |
||
263 | 65 | public function inNamespace() |
|
267 | |||
268 | /** |
||
269 | * {@inheritDoc} |
||
270 | */ |
||
271 | 64 | public function isClosure() |
|
275 | |||
276 | /** |
||
277 | * {@inheritDoc} |
||
278 | */ |
||
279 | 64 | public function isDeprecated() |
|
284 | |||
285 | /** |
||
286 | * {@inheritDoc} |
||
287 | */ |
||
288 | 64 | public function isGenerator() |
|
308 | |||
309 | /** |
||
310 | * {@inheritDoc} |
||
311 | */ |
||
312 | 64 | public function isInternal() |
|
317 | |||
318 | /** |
||
319 | * {@inheritDoc} |
||
320 | */ |
||
321 | 64 | public function isUserDefined() |
|
326 | |||
327 | /** |
||
328 | * {@inheritDoc} |
||
329 | */ |
||
330 | 64 | public function isVariadic() |
|
340 | |||
341 | /** |
||
342 | * {@inheritDoc} |
||
343 | */ |
||
344 | 65 | public function returnsReference() |
|
348 | } |
||
349 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.