|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace TheCodingMachine\PHPStan\Rules\TypeHints; |
| 5 | + |
| 6 | + |
| 7 | +use BetterReflection\Reflection\ReflectionParameter; |
| 8 | +use BetterReflection\Util\FindReflectionOnLine; |
| 9 | +use phpDocumentor\Reflection\Type; |
| 10 | +use phpDocumentor\Reflection\Types\Array_; |
| 11 | +use phpDocumentor\Reflection\Types\Boolean; |
| 12 | +use phpDocumentor\Reflection\Types\Callable_; |
| 13 | +use phpDocumentor\Reflection\Types\Float_; |
| 14 | +use phpDocumentor\Reflection\Types\Integer; |
| 15 | +use phpDocumentor\Reflection\Types\Mixed; |
| 16 | +use phpDocumentor\Reflection\Types\Null_; |
| 17 | +use phpDocumentor\Reflection\Types\Object_; |
| 18 | +use phpDocumentor\Reflection\Types\Scalar; |
| 19 | +use phpDocumentor\Reflection\Types\String_; |
| 20 | +use PhpParser\Node; |
| 21 | +use PhpParser\Node\Stmt\Catch_; |
| 22 | +use PHPStan\Analyser\Scope; |
| 23 | +use PHPStan\Broker\Broker; |
| 24 | +use PHPStan\Rules\Rule; |
| 25 | + |
| 26 | +class AbstractMissingTypeHintRule implements Rule |
| 27 | +{ |
| 28 | + |
| 29 | + /** |
| 30 | + * @var Broker |
| 31 | + */ |
| 32 | + private $broker; |
| 33 | + |
| 34 | + public function __construct(Broker $broker) |
| 35 | + { |
| 36 | + |
| 37 | + $this->broker = $broker; |
| 38 | + } |
| 39 | + |
| 40 | + public function getNodeType(): string |
| 41 | + { |
| 42 | + // FIXME: does this encompass the Method_? |
| 43 | + return Node\Stmt\Function_::class; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @param \PhpParser\Node\Stmt\Function_ $node |
| 48 | + * @param \PHPStan\Analyser\Scope $scope |
| 49 | + * @return string[] |
| 50 | + */ |
| 51 | + public function processNode(Node $node, Scope $scope): array |
| 52 | + { |
| 53 | + // TODO: improve performance by caching better reflection results. |
| 54 | + $finder = new FindReflectionOnLine(); |
| 55 | + |
| 56 | + $reflection = $finder($scope->getFile(), $node->getLine()); |
| 57 | + |
| 58 | + $errors = []; |
| 59 | + |
| 60 | + foreach ($reflection->getParameters() as $parameter) { |
| 61 | + $result = $this->analyzeParameter($parameter); |
| 62 | + |
| 63 | + if ($result !== null) { |
| 64 | + $errors[] = $result; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return $errors; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Analyzes a parameter and returns the error string if xomething goes wrong or null if everything is ok. |
| 73 | + * |
| 74 | + * @param ReflectionParameter $parameter |
| 75 | + * @return null|string |
| 76 | + */ |
| 77 | + private function analyzeParameter(ReflectionParameter $parameter): ?string |
| 78 | + { |
| 79 | + $phpTypeHint = $parameter->getTypeHint(); |
| 80 | + $docBlockTypeHints = $parameter->getDocBlockTypes(); |
| 81 | + |
| 82 | + // If there is a type-hint, we have nothing to say unless it is an array. |
| 83 | + if ($phpTypeHint !== null) { |
| 84 | + return $this->analyzeParameterWithTypehint($parameter, $phpTypeHint, $docBlockTypeHints); |
| 85 | + } else { |
| 86 | + return $this->analyzeParameterWithoutTypehint($parameter, $docBlockTypeHints); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param Type[] $docBlockTypeHints |
| 92 | + */ |
| 93 | + private function analyzeParameterWithTypehint(ReflectionParameter $parameter, Type $phpTypeHint, array $docBlockTypeHints): ?string |
| 94 | + { |
| 95 | + $docblockWithoutNullable = $this->typesWithoutNullable($docBlockTypeHints); |
| 96 | + |
| 97 | + if (!$phpTypeHint instanceof Array_) { |
| 98 | + // Let's detect mismatches between docblock and PHP typehint |
| 99 | + foreach ($docblockWithoutNullable as $docblockTypehint) { |
| 100 | + if (get_class($docblockTypehint) !== get_class($phpTypeHint)) { |
| 101 | + return sprintf('Parameter $%s type is type-hinted to "%s" but the @param annotation says it is a "%s". Please fix the @param annotation.', $parameter->getName(), (string) $phpTypeHint, (string) $docblockTypehint); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + if (empty($docblockWithoutNullable)) { |
| 109 | + return sprintf('Parameter $%s type is "array". Please provide a @param annotation to further speciy the type of the array. For instance: @param int[] $%s', $parameter->getName(), $parameter->getName()); |
| 110 | + } else { |
| 111 | + foreach ($docblockWithoutNullable as $docblockTypehint) { |
| 112 | + if (!$docblockTypehint instanceof Array_) { |
| 113 | + return sprintf('Mismatching type-hints for parameter %s. PHP type hint is "array" and docblock type hint is %s.', $parameter->getName(), (string) $docblockTypehint); |
| 114 | + } |
| 115 | + |
| 116 | + if ($docblockTypehint->getValueType() instanceof Mixed) { |
| 117 | + return sprintf('Parameter $%s type is "array". Please provide a more specific @param annotation. For instance: @param int[] $%s', $parameter->getName(), $parameter->getName()); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return null; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @param Type[] $docBlockTypeHints |
| 127 | + * @return null|string |
| 128 | + */ |
| 129 | + private function analyzeParameterWithoutTypehint(ReflectionParameter $parameter, array $docBlockTypeHints): ?string |
| 130 | + { |
| 131 | + if (empty($docBlockTypeHints)) { |
| 132 | + return sprintf('Parameter $%s has no type-hint and no @param annotation.', $parameter->getName()); |
| 133 | + } |
| 134 | + |
| 135 | + $nativeTypehint = $this->isNativelyTypehintable($docBlockTypeHints); |
| 136 | + |
| 137 | + if ($nativeTypehint !== null) { |
| 138 | + return sprintf('Parameter $%s can be type-hinted to "%s".', $parameter->getName(), $nativeTypehint); |
| 139 | + } |
| 140 | + |
| 141 | + // TODO: cancel if the method is implemented from an interface or overrides another method |
| 142 | + // TODO: return types |
| 143 | + |
| 144 | + return null; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * @param Type[] $docBlockTypeHints |
| 149 | + * @return string|null |
| 150 | + */ |
| 151 | + private function isNativelyTypehintable(array $docBlockTypeHints): ?string |
| 152 | + { |
| 153 | + if (count($docBlockTypeHints) > 2) { |
| 154 | + return null; |
| 155 | + } |
| 156 | + $isNullable = $this->isNullable($docBlockTypeHints); |
| 157 | + if (count($docBlockTypeHints) === 2 && !$isNullable) { |
| 158 | + return null; |
| 159 | + } |
| 160 | + |
| 161 | + $types = $this->typesWithoutNullable($docBlockTypeHints); |
| 162 | + // At this point, there is at most one element here |
| 163 | + if (empty($types)) { |
| 164 | + return null; |
| 165 | + } |
| 166 | + |
| 167 | + $type = $types[0]; |
| 168 | + |
| 169 | + if ($this->isNativeType($type)) { |
| 170 | + return ($isNullable?'?':'').((string)$type); |
| 171 | + } |
| 172 | + |
| 173 | + if ($type instanceof Array_) { |
| 174 | + return ($isNullable?'?':'').'array'; |
| 175 | + } |
| 176 | + |
| 177 | + // TODO: more definitions to add here |
| 178 | + // Manage interface/classes |
| 179 | + // Manage array of things => (cast to array) |
| 180 | + |
| 181 | + if ($type instanceof Object_) { |
| 182 | + return ($isNullable?'?':'').((string)$type); |
| 183 | + } |
| 184 | + |
| 185 | + return null; |
| 186 | + } |
| 187 | + |
| 188 | + private function isNativeType(Type $type): bool |
| 189 | + { |
| 190 | + if ($type instanceof String_ |
| 191 | + || $type instanceof Integer |
| 192 | + || $type instanceof Boolean |
| 193 | + || $type instanceof Float_ |
| 194 | + || $type instanceof Scalar |
| 195 | + || $type instanceof Callable_ |
| 196 | + || ((string) $type) === 'iterable' |
| 197 | + ) { |
| 198 | + return true; |
| 199 | + } |
| 200 | + return false; |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * @param Type[] $docBlockTypeHints |
| 205 | + * @return bool |
| 206 | + */ |
| 207 | + private function isNullable(array $docBlockTypeHints): bool |
| 208 | + { |
| 209 | + foreach ($docBlockTypeHints as $docBlockTypeHint) { |
| 210 | + if ($docBlockTypeHint instanceof Null_) { |
| 211 | + return true; |
| 212 | + } |
| 213 | + } |
| 214 | + return false; |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * Removes "null" from the list of types. |
| 219 | + * |
| 220 | + * @param Type[] $docBlockTypeHints |
| 221 | + * @return array |
| 222 | + */ |
| 223 | + private function typesWithoutNullable(array $docBlockTypeHints): array |
| 224 | + { |
| 225 | + return array_filter($docBlockTypeHints, function($item) { |
| 226 | + return !$item instanceof Null_; |
| 227 | + }); |
| 228 | + } |
| 229 | +} |
0 commit comments