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 |
||
22 | class ReflectionParameter extends BaseReflectionParameter |
||
23 | { |
||
24 | use InternalPropertiesEmulationTrait; |
||
25 | |||
26 | /** |
||
27 | * Reflection function or method |
||
28 | * |
||
29 | * @var \ReflectionFunctionAbstract |
||
30 | */ |
||
31 | private $declaringFunction; |
||
32 | |||
33 | /** |
||
34 | * Stores the default value for node (if present) |
||
35 | * |
||
36 | * @var mixed |
||
37 | */ |
||
38 | private $defaultValue = null; |
||
39 | |||
40 | /** |
||
41 | * Whether or not default value is constant |
||
42 | * |
||
43 | * @var bool |
||
44 | */ |
||
45 | private $isDefaultValueConstant = false; |
||
46 | |||
47 | /** |
||
48 | * Name of the constant of default value |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | private $defaultValueConstantName; |
||
53 | |||
54 | /** |
||
55 | * Index of parameter in the list |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | private $parameterIndex = 0; |
||
60 | |||
61 | /** |
||
62 | * Concrete parameter node |
||
63 | * |
||
64 | * @var Param |
||
65 | */ |
||
66 | private $parameterNode; |
||
67 | |||
68 | /** |
||
69 | * Initializes a reflection for the property |
||
70 | * |
||
71 | * @param string|array $unusedFunctionName Name of the function/method |
||
72 | * @param string $parameterName Name of the parameter to reflect |
||
73 | * @param Param $parameterNode Parameter definition node |
||
74 | * @param int $parameterIndex Index of parameter |
||
75 | * @param \ReflectionFunctionAbstract $declaringFunction |
||
76 | */ |
||
77 | 11 | public function __construct( |
|
105 | |||
106 | /** |
||
107 | * Emulating original behaviour of reflection |
||
108 | */ |
||
109 | 1 | public function ___debugInfo() |
|
115 | |||
116 | /** |
||
117 | * Returns string representation of this parameter. |
||
118 | * |
||
119 | * @return string |
||
120 | */ |
||
121 | 3 | public function __toString() |
|
122 | { |
||
123 | 3 | $parameterType = $this->parameterNode->type; |
|
124 | 3 | if (is_object($parameterType)) { |
|
125 | 1 | $parameterType = $parameterType->toString(); |
|
126 | } |
||
127 | 3 | $isNullableParam = !empty($parameterType) && $this->allowsNull(); |
|
128 | 3 | $isOptional = $this->isOptional(); |
|
129 | 3 | $defaultValue = ''; |
|
130 | 3 | if ($isOptional) { |
|
131 | 2 | $defaultValue = $this->getDefaultValue(); |
|
132 | 2 | if (is_string($defaultValue) && strlen($defaultValue) > 15) { |
|
133 | $defaultValue = substr($defaultValue, 0, 15) . '...'; |
||
134 | } |
||
135 | /* @see https://3v4l.org/DJOEb for behaviour changes */ |
||
136 | 2 | if (is_double($defaultValue) && fmod($defaultValue, 1.0) === 0.0) { |
|
137 | 2 | $defaultValue = (int) $defaultValue; |
|
138 | } |
||
139 | 2 | $defaultValue = str_replace('\\\\', '\\', var_export($defaultValue, true)); |
|
140 | } |
||
141 | |||
142 | 3 | return sprintf( |
|
143 | 3 | 'Parameter #%d [ %s %s%s%s%s$%s%s ]', |
|
144 | 3 | $this->parameterIndex, |
|
145 | 3 | ($this->isVariadic() || $isOptional) ? '<optional>' : '<required>', |
|
146 | 3 | $parameterType ? ltrim($parameterType, '\\') . ' ' : '', |
|
147 | 3 | $isNullableParam ? 'or NULL ' : '', |
|
148 | 3 | $this->isVariadic() ? '...' : '', |
|
149 | 3 | $this->isPassedByReference() ? '&' : '', |
|
150 | 3 | $this->getName(), |
|
151 | 3 | $isOptional ? (' = ' . $defaultValue) : '' |
|
152 | ); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * {@inheritDoc} |
||
157 | */ |
||
158 | 2 | public function allowsNull() |
|
167 | |||
168 | /** |
||
169 | * {@inheritDoc} |
||
170 | */ |
||
171 | 1 | public function canBePassedByValue() |
|
175 | |||
176 | /** |
||
177 | * @inheritDoc |
||
178 | */ |
||
179 | 2 | public function getClass() |
|
180 | { |
||
181 | 2 | $parameterType = $this->parameterNode->type; |
|
182 | 2 | if ($parameterType instanceof Name) { |
|
183 | 2 | if (!$parameterType instanceof Name\FullyQualified) { |
|
184 | 1 | $parameterTypeName = $parameterType->toString(); |
|
185 | |||
186 | 1 | if ('self' === $parameterTypeName) { |
|
187 | 1 | return $this->getDeclaringClass(); |
|
188 | } |
||
189 | |||
190 | 1 | if ('parent' === $parameterTypeName) { |
|
191 | 1 | return $this->getDeclaringClass()->getParentClass(); |
|
192 | } |
||
193 | |||
194 | throw new ReflectionException("Can not resolve a class name for parameter"); |
||
195 | } |
||
196 | 1 | $className = $parameterType->toString(); |
|
197 | 1 | $classOrInterfaceExists = class_exists($className, false) || interface_exists($className, false); |
|
198 | |||
199 | 1 | return $classOrInterfaceExists ? new \ReflectionClass($className) : new ReflectionClass($className); |
|
200 | } |
||
201 | |||
202 | 1 | return null; |
|
203 | } |
||
204 | |||
205 | /** |
||
206 | * {@inheritDoc} |
||
207 | */ |
||
208 | 3 | public function getDeclaringClass() |
|
216 | |||
217 | /** |
||
218 | * {@inheritDoc} |
||
219 | */ |
||
220 | 3 | public function getDeclaringFunction() |
|
224 | |||
225 | /** |
||
226 | * {@inheritDoc} |
||
227 | */ |
||
228 | 5 | public function getDefaultValue() |
|
236 | |||
237 | /** |
||
238 | * {@inheritDoc} |
||
239 | */ |
||
240 | 2 | public function getDefaultValueConstantName() |
|
248 | |||
249 | /** |
||
250 | * @inheritDoc |
||
251 | */ |
||
252 | 4 | public function getName() |
|
256 | |||
257 | /** |
||
258 | * {@inheritDoc} |
||
259 | */ |
||
260 | 1 | public function getPosition() |
|
264 | |||
265 | /** |
||
266 | * @inheritDoc |
||
267 | */ |
||
268 | 1 | public function getType() |
|
283 | |||
284 | /** |
||
285 | * @inheritDoc |
||
286 | */ |
||
287 | 2 | public function hasType() |
|
293 | |||
294 | /** |
||
295 | * @inheritDoc |
||
296 | */ |
||
297 | 1 | public function isArray() |
|
301 | |||
302 | /** |
||
303 | * @inheritDoc |
||
304 | */ |
||
305 | 1 | public function isCallable() |
|
309 | |||
310 | /** |
||
311 | * @inheritDoc |
||
312 | */ |
||
313 | 11 | public function isDefaultValueAvailable() |
|
317 | |||
318 | /** |
||
319 | * {@inheritDoc} |
||
320 | */ |
||
321 | 1 | public function isDefaultValueConstant() |
|
325 | |||
326 | /** |
||
327 | * {@inheritDoc} |
||
328 | */ |
||
329 | 3 | public function isOptional() |
|
333 | |||
334 | /** |
||
335 | * @inheritDoc |
||
336 | */ |
||
337 | 3 | public function isPassedByReference() |
|
341 | |||
342 | /** |
||
343 | * @inheritDoc |
||
344 | */ |
||
345 | 3 | public function isVariadic() |
|
349 | |||
350 | /** |
||
351 | * Returns if all following parameters have a default value definition. |
||
352 | * |
||
353 | * @return bool |
||
354 | * @throws ReflectionException If could not fetch declaring function reflection |
||
355 | */ |
||
356 | 3 | protected function haveSiblingsDefalutValues() |
|
373 | } |
||
374 |