Complex classes like ReflectionParameter 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 ReflectionParameter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class ReflectionParameter extends BaseReflectionParameter |
||
24 | { |
||
25 | use InternalPropertiesEmulationTrait; |
||
26 | |||
27 | /** |
||
28 | * Reflection function or method |
||
29 | * |
||
30 | * @var \ReflectionFunctionAbstract |
||
31 | */ |
||
32 | private $declaringFunction; |
||
33 | |||
34 | /** |
||
35 | * Stores the default value for node (if present) |
||
36 | * |
||
37 | * @var mixed |
||
38 | */ |
||
39 | private $defaultValue = null; |
||
40 | |||
41 | /** |
||
42 | * Whether or not default value is constant |
||
43 | * |
||
44 | * @var bool |
||
45 | */ |
||
46 | private $isDefaultValueConstant = false; |
||
47 | |||
48 | /** |
||
49 | * Name of the constant of default value |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | private $defaultValueConstantName; |
||
54 | |||
55 | /** |
||
56 | * Index of parameter in the list |
||
57 | * |
||
58 | * @var int |
||
59 | */ |
||
60 | private $parameterIndex = 0; |
||
61 | |||
62 | /** |
||
63 | * Concrete parameter node |
||
64 | * |
||
65 | * @var Param |
||
66 | */ |
||
67 | private $parameterNode; |
||
68 | |||
69 | /** |
||
70 | * Initializes a reflection for the property |
||
71 | * |
||
72 | * @param string|array $unusedFunctionName Name of the function/method |
||
73 | * @param string $parameterName Name of the parameter to reflect |
||
74 | * @param Param $parameterNode Parameter definition node |
||
75 | * @param int $parameterIndex Index of parameter |
||
76 | * @param \ReflectionFunctionAbstract $declaringFunction |
||
77 | */ |
||
78 | 31 | public function __construct( |
|
106 | |||
107 | /** |
||
108 | * Emulating original behaviour of reflection |
||
109 | */ |
||
110 | 1 | public function ___debugInfo() |
|
116 | |||
117 | /** |
||
118 | * Returns string representation of this parameter. |
||
119 | * |
||
120 | * @return string |
||
121 | */ |
||
122 | 20 | public function __toString() |
|
153 | |||
154 | /** |
||
155 | * {@inheritDoc} |
||
156 | */ |
||
157 | 21 | public function allowsNull() |
|
171 | |||
172 | /** |
||
173 | * {@inheritDoc} |
||
174 | */ |
||
175 | 3 | public function canBePassedByValue() |
|
179 | |||
180 | /** |
||
181 | * @inheritDoc |
||
182 | */ |
||
183 | 2 | public function getClass() |
|
208 | |||
209 | /** |
||
210 | * {@inheritDoc} |
||
211 | */ |
||
212 | 3 | public function getDeclaringClass() |
|
220 | |||
221 | /** |
||
222 | * {@inheritDoc} |
||
223 | */ |
||
224 | 14 | public function getDeclaringFunction() |
|
228 | |||
229 | /** |
||
230 | * {@inheritDoc} |
||
231 | */ |
||
232 | 12 | public function getDefaultValue() |
|
240 | |||
241 | /** |
||
242 | * {@inheritDoc} |
||
243 | */ |
||
244 | 6 | public function getDefaultValueConstantName() |
|
252 | |||
253 | /** |
||
254 | * @inheritDoc |
||
255 | */ |
||
256 | 21 | public function getName() |
|
260 | |||
261 | /** |
||
262 | * {@inheritDoc} |
||
263 | */ |
||
264 | 3 | public function getPosition() |
|
268 | |||
269 | /** |
||
270 | * @inheritDoc |
||
271 | */ |
||
272 | 21 | public function getType() |
|
291 | |||
292 | /** |
||
293 | * @inheritDoc |
||
294 | */ |
||
295 | 4 | public function hasType() |
|
301 | |||
302 | /** |
||
303 | * @inheritDoc |
||
304 | */ |
||
305 | 3 | public function isArray() |
|
309 | |||
310 | /** |
||
311 | * @inheritDoc |
||
312 | */ |
||
313 | 3 | public function isCallable() |
|
317 | |||
318 | /** |
||
319 | * @inheritDoc |
||
320 | */ |
||
321 | 45 | public function isDefaultValueAvailable() |
|
325 | |||
326 | /** |
||
327 | * {@inheritDoc} |
||
328 | */ |
||
329 | 5 | public function isDefaultValueConstant() |
|
333 | |||
334 | /** |
||
335 | * {@inheritDoc} |
||
336 | */ |
||
337 | 36 | public function isOptional() |
|
341 | |||
342 | /** |
||
343 | * @inheritDoc |
||
344 | */ |
||
345 | 20 | public function isPassedByReference() |
|
349 | |||
350 | /** |
||
351 | * @inheritDoc |
||
352 | */ |
||
353 | 52 | public function isVariadic() |
|
357 | |||
358 | /** |
||
359 | * Returns if all following parameters have a default value definition. |
||
360 | * |
||
361 | * @return bool |
||
362 | * @throws ReflectionException If could not fetch declaring function reflection |
||
363 | */ |
||
364 | 14 | protected function haveSiblingsDefalutValues() |
|
381 | } |
||
382 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.