Complex classes like AbstractMissingTypeHintRule 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 AbstractMissingTypeHintRule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | abstract class AbstractMissingTypeHintRule implements Rule |
||
| 33 | { |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var Broker |
||
| 37 | */ |
||
| 38 | private $broker; |
||
| 39 | |||
| 40 | public function __construct(Broker $broker) |
||
| 41 | { |
||
| 42 | $this->broker = $broker; |
||
| 43 | } |
||
| 44 | |||
| 45 | abstract public function getNodeType(): string; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param ReflectionMethod|ReflectionFunction $reflection |
||
| 49 | * @return string |
||
| 50 | */ |
||
| 51 | abstract public function getContext($reflection): string; |
||
| 52 | |||
| 53 | abstract public function isReturnIgnored(Node $node): bool; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param \PhpParser\Node\Stmt\Function_|\PhpParser\Node\Stmt\ClassMethod $node |
||
| 57 | * @param \PHPStan\Analyser\Scope $scope |
||
| 58 | * @return string[] |
||
| 59 | */ |
||
| 60 | public function processNode(Node $node, Scope $scope): array |
||
| 61 | { |
||
| 62 | // TODO: improve performance by caching better reflection results. |
||
| 63 | $finder = (new BetterReflection())->findReflectionsOnLine(); |
||
| 64 | |||
| 65 | if ($node->getLine() < 0) { |
||
| 66 | // Fixes some problems with methods in anonymous class (the line number is poorly reported). |
||
| 67 | return []; |
||
| 68 | } |
||
| 69 | |||
| 70 | $reflection = $finder($scope->getFile(), $node->getLine()); |
||
| 71 | |||
| 72 | // If the method implements/extends another method, we have no choice on the signature so let's bypass this check. |
||
| 73 | if ($reflection instanceof ReflectionMethod && $this->isInherited($reflection)) { |
||
| 74 | return []; |
||
| 75 | } |
||
| 76 | |||
| 77 | $errors = []; |
||
| 78 | |||
| 79 | if ($reflection === null) { |
||
| 80 | throw new \RuntimeException('Could not find item at '.$scope->getFile().':'.$node->getLine()); |
||
| 81 | } |
||
| 82 | |||
| 83 | foreach ($reflection->getParameters() as $parameter) { |
||
| 84 | $result = $this->analyzeParameter($parameter); |
||
| 85 | |||
| 86 | if ($result !== null) { |
||
| 87 | $errors[] = $result; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | if (!$this->isReturnIgnored($node)) { |
||
| 92 | $returnTypeError = $this->analyzeReturnType($reflection); |
||
| 93 | if ($returnTypeError !== null) { |
||
| 94 | $errors[] = $returnTypeError; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | return $errors; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Analyzes a parameter and returns the error string if xomething goes wrong or null if everything is ok. |
||
| 103 | * |
||
| 104 | * @param ReflectionParameter $parameter |
||
| 105 | * @return null|string |
||
| 106 | */ |
||
| 107 | private function analyzeParameter(ReflectionParameter $parameter): ?string |
||
| 108 | { |
||
| 109 | $typeResolver = new \phpDocumentor\Reflection\TypeResolver(); |
||
| 110 | |||
| 111 | $phpTypeHint = $parameter->getType(); |
||
| 112 | try { |
||
| 113 | $docBlockTypeHints = $parameter->getDocBlockTypes(); |
||
| 114 | } catch (\InvalidArgumentException $e) { |
||
| 115 | return sprintf('%s, for parameter $%s, invalid docblock @param encountered. %s', |
||
| 116 | $this->getContext($parameter), |
||
|
|
|||
| 117 | $parameter->getName(), |
||
| 118 | $e->getMessage() |
||
| 119 | ); |
||
| 120 | } |
||
| 121 | |||
| 122 | // If there is a type-hint, we have nothing to say unless it is an array. |
||
| 123 | if ($phpTypeHint !== null) { |
||
| 124 | $phpdocTypeHint = $typeResolver->resolve((string) $phpTypeHint); |
||
| 125 | if ($parameter->isVariadic()) { |
||
| 126 | $phpdocTypeHint = new Array_($phpdocTypeHint); |
||
| 127 | } |
||
| 128 | |||
| 129 | return $this->analyzeWithTypehint($parameter, $phpdocTypeHint, $docBlockTypeHints); |
||
| 130 | } else { |
||
| 131 | return $this->analyzeWithoutTypehint($parameter, $docBlockTypeHints); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param ReflectionFunction|ReflectionMethod $function |
||
| 137 | * @return null|string |
||
| 138 | */ |
||
| 139 | private function analyzeReturnType($function): ?string |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param ReflectionParameter|ReflectionMethod|ReflectionFunction $context |
||
| 159 | * @param Type $phpTypeHint |
||
| 160 | * @param Type[] $docBlockTypeHints |
||
| 161 | * @return null|string |
||
| 162 | */ |
||
| 163 | private function analyzeWithTypehint($context, Type $phpTypeHint, array $docBlockTypeHints): ?string |
||
| 212 | |||
| 213 | private function isTypeIterable(Type $phpTypeHint) : bool |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param ReflectionParameter|ReflectionMethod|ReflectionFunction $context |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | private function findExplicitMixedArray($context) : bool |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param ReflectionParameter|ReflectionMethod|ReflectionFunction $context |
||
| 253 | * @param Type[] $docBlockTypeHints |
||
| 254 | * @return null|string |
||
| 255 | */ |
||
| 256 | private function analyzeWithoutTypehint($context, array $docBlockTypeHints): ?string |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param Type[] $docBlockTypeHints |
||
| 281 | * @return string|null |
||
| 282 | */ |
||
| 283 | private function isNativelyTypehintable(array $docBlockTypeHints): ?string |
||
| 319 | |||
| 320 | private function isNativeType(Type $type): bool |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param Type[] $docBlockTypeHints |
||
| 337 | * @return bool |
||
| 338 | */ |
||
| 339 | private function isNullable(array $docBlockTypeHints): bool |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Removes "null" from the list of types. |
||
| 351 | * |
||
| 352 | * @param Type[] $docBlockTypeHints |
||
| 353 | * @return Type[] |
||
| 354 | */ |
||
| 355 | private function typesWithoutNullable(array $docBlockTypeHints): array |
||
| 361 | |||
| 362 | private function isInherited(ReflectionMethod $method, ReflectionClass $class = null): bool |
||
| 384 | } |
||
| 385 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: