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 | 24 | public function __construct( |
|
| 78 | $unusedFunctionName, |
||
| 79 | $parameterName, |
||
| 80 | Param $parameterNode = null, |
||
| 81 | $parameterIndex = 0, |
||
| 82 | \ReflectionFunctionAbstract $declaringFunction = null |
||
| 83 | ) { |
||
| 84 | // Let's unset original read-only property to have a control over it via __get |
||
| 85 | 24 | unset($this->name); |
|
| 86 | |||
| 87 | 24 | $this->parameterNode = $parameterNode; |
|
| 88 | 24 | $this->parameterIndex = $parameterIndex; |
|
| 89 | 24 | $this->declaringFunction = $declaringFunction; |
|
| 90 | |||
| 91 | 24 | if ($this->isDefaultValueAvailable()) { |
|
| 92 | 18 | if ($declaringFunction instanceof \ReflectionMethod) { |
|
| 93 | 8 | $context = $declaringFunction->getDeclaringClass(); |
|
| 94 | } else { |
||
| 95 | 10 | $context = $declaringFunction; |
|
| 96 | }; |
||
| 97 | |||
| 98 | 18 | $expressionSolver = new NodeExpressionResolver($context); |
|
| 99 | 18 | $expressionSolver->process($this->parameterNode->default); |
|
| 100 | 18 | $this->defaultValue = $expressionSolver->getValue(); |
|
| 101 | 18 | $this->isDefaultValueConstant = $expressionSolver->isConstant(); |
|
| 102 | 18 | $this->defaultValueConstantName = $expressionSolver->getConstantName(); |
|
| 103 | } |
||
| 104 | 24 | } |
|
| 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 | 13 | public function __toString() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * {@inheritDoc} |
||
| 156 | */ |
||
| 157 | 14 | public function allowsNull() |
|
| 166 | |||
| 167 | /** |
||
| 168 | * {@inheritDoc} |
||
| 169 | */ |
||
| 170 | 3 | public function canBePassedByValue() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @inheritDoc |
||
| 177 | */ |
||
| 178 | 2 | public function getClass() |
|
| 203 | |||
| 204 | /** |
||
| 205 | * {@inheritDoc} |
||
| 206 | */ |
||
| 207 | 3 | public function getDeclaringClass() |
|
| 215 | |||
| 216 | /** |
||
| 217 | * {@inheritDoc} |
||
| 218 | */ |
||
| 219 | 12 | public function getDeclaringFunction() |
|
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritDoc} |
||
| 226 | */ |
||
| 227 | 11 | public function getDefaultValue() |
|
| 235 | |||
| 236 | /** |
||
| 237 | * {@inheritDoc} |
||
| 238 | */ |
||
| 239 | 6 | public function getDefaultValueConstantName() |
|
| 247 | |||
| 248 | /** |
||
| 249 | * @inheritDoc |
||
| 250 | */ |
||
| 251 | 14 | public function getName() |
|
| 255 | |||
| 256 | /** |
||
| 257 | * {@inheritDoc} |
||
| 258 | */ |
||
| 259 | 3 | public function getPosition() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @inheritDoc |
||
| 266 | */ |
||
| 267 | 14 | public function getType() |
|
| 282 | |||
| 283 | /** |
||
| 284 | * @inheritDoc |
||
| 285 | */ |
||
| 286 | 4 | public function hasType() |
|
| 292 | |||
| 293 | /** |
||
| 294 | * @inheritDoc |
||
| 295 | */ |
||
| 296 | 3 | public function isArray() |
|
| 300 | |||
| 301 | /** |
||
| 302 | * @inheritDoc |
||
| 303 | */ |
||
| 304 | 3 | public function isCallable() |
|
| 308 | |||
| 309 | /** |
||
| 310 | * @inheritDoc |
||
| 311 | */ |
||
| 312 | 32 | public function isDefaultValueAvailable() |
|
| 316 | |||
| 317 | /** |
||
| 318 | * {@inheritDoc} |
||
| 319 | */ |
||
| 320 | 5 | public function isDefaultValueConstant() |
|
| 324 | |||
| 325 | /** |
||
| 326 | * {@inheritDoc} |
||
| 327 | */ |
||
| 328 | 22 | public function isOptional() |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @inheritDoc |
||
| 335 | */ |
||
| 336 | 13 | public function isPassedByReference() |
|
| 340 | |||
| 341 | /** |
||
| 342 | * @inheritDoc |
||
| 343 | */ |
||
| 344 | 31 | public function isVariadic() |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Returns if all following parameters have a default value definition. |
||
| 351 | * |
||
| 352 | * @return bool |
||
| 353 | * @throws ReflectionException If could not fetch declaring function reflection |
||
| 354 | */ |
||
| 355 | 12 | protected function haveSiblingsDefalutValues() |
|
| 372 | } |
||
| 373 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: