|
4 | 4 |
|
5 | 5 | use PhpParser\Node; |
6 | 6 | use PHPStan\Analyser\Scope; |
7 | | -use PHPStan\Node\InClassMethodNode; |
8 | | -use PHPStan\Node\InFunctionNode; |
9 | | -use PHPStan\Node\VirtualNode; |
10 | | -use PHPStan\Rules\RuleError; |
11 | 7 | use PHPStan\Rules\RuleErrorBuilder; |
| 8 | +use PHPStan\Type\FileTypeMapper; |
12 | 9 | use PHPStan\Type\ObjectType; |
13 | | -use PHPStan\Type\Type; |
14 | 10 | use PHPStan\Type\VerbosityLevel; |
15 | 11 | use PHPStan\Type\VoidType; |
16 | 12 |
|
17 | 13 | /** |
18 | | - * @implements \PHPStan\Rules\Rule<VirtualNode> |
| 14 | + * @implements \PHPStan\Rules\Rule<\PhpParser\Node\FunctionLike> |
19 | 15 | */ |
20 | 16 | class InvalidThrowsPhpDocValueRule implements \PHPStan\Rules\Rule |
21 | 17 | { |
22 | 18 |
|
| 19 | + /** @var FileTypeMapper */ |
| 20 | + private $fileTypeMapper; |
| 21 | + |
| 22 | + public function __construct(FileTypeMapper $fileTypeMapper) |
| 23 | + { |
| 24 | + $this->fileTypeMapper = $fileTypeMapper; |
| 25 | + } |
| 26 | + |
23 | 27 | public function getNodeType(): string |
24 | 28 | { |
25 | | - return VirtualNode::class; |
| 29 | + return \PhpParser\Node\FunctionLike::class; |
26 | 30 | } |
27 | 31 |
|
28 | 32 | public function processNode(Node $node, Scope $scope): array |
29 | 33 | { |
30 | | - if (!$node instanceof InFunctionNode && !$node instanceof InClassMethodNode) { |
| 34 | + $docComment = $node->getDocComment(); |
| 35 | + if ($docComment === null) { |
31 | 36 | return []; |
32 | 37 | } |
33 | 38 |
|
34 | | - if ($scope->getFunction() === null) { |
35 | | - throw new \PHPStan\ShouldNotHappenException(); |
| 39 | + $functionName = null; |
| 40 | + if ($node instanceof Node\Stmt\ClassMethod) { |
| 41 | + $functionName = $node->name->name; |
| 42 | + } elseif ($node instanceof Node\Stmt\Function_) { |
| 43 | + $functionName = trim($scope->getNamespace() . '\\' . $node->name->name, '\\'); |
36 | 44 | } |
37 | 45 |
|
38 | | - $throwType = $scope->getFunction()->getThrowType(); |
| 46 | + $resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc( |
| 47 | + $scope->getFile(), |
| 48 | + $scope->isInClass() ? $scope->getClassReflection()->getName() : null, |
| 49 | + $scope->isInTrait() ? $scope->getTraitReflection()->getName() : null, |
| 50 | + $functionName, |
| 51 | + $docComment->getText() |
| 52 | + ); |
39 | 53 |
|
40 | | - return $this->check($throwType); |
41 | | - } |
42 | | - |
43 | | - /** |
44 | | - * @param Type|null $phpDocThrowType |
45 | | - * @return array<int, RuleError> errors |
46 | | - */ |
47 | | - private function check(?Type $phpDocThrowType): array |
48 | | - { |
49 | | - if ($phpDocThrowType === null) { |
| 54 | + if ($resolvedPhpDoc->getThrowsTag() === null) { |
50 | 55 | return []; |
51 | 56 | } |
52 | 57 |
|
53 | | - if ((new VoidType())->isSuperTypeOf($phpDocThrowType)->yes()) { |
| 58 | + $phpDocThrowsType = $resolvedPhpDoc->getThrowsTag()->getType(); |
| 59 | + if ((new VoidType())->isSuperTypeOf($phpDocThrowsType)->yes()) { |
54 | 60 | return []; |
55 | 61 | } |
56 | 62 |
|
57 | | - $isThrowsSuperType = (new ObjectType(\Throwable::class))->isSuperTypeOf($phpDocThrowType); |
| 63 | + $isThrowsSuperType = (new ObjectType(\Throwable::class))->isSuperTypeOf($phpDocThrowsType); |
58 | 64 | if ($isThrowsSuperType->yes()) { |
59 | 65 | return []; |
60 | 66 | } |
61 | 67 |
|
62 | 68 | return [ |
63 | 69 | RuleErrorBuilder::message(sprintf( |
64 | 70 | 'PHPDoc tag @throws with type %s is not subtype of Throwable', |
65 | | - $phpDocThrowType->describe(VerbosityLevel::typeOnly()) |
| 71 | + $phpDocThrowsType->describe(VerbosityLevel::typeOnly()) |
66 | 72 | ))->build(), |
67 | 73 | ]; |
68 | 74 | } |
|
0 commit comments