Skip to content

Commit 8cfb147

Browse files
committed
Reworked InvalidThrowsPhpDocValueRule without NodeScopeResolver
1 parent 71d5257 commit 8cfb147

3 files changed

Lines changed: 19 additions & 70 deletions

File tree

src/PhpDoc/StubValidator.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,6 @@ private function getRuleRegistry(Container $container): Registry
117117
$functionDefinitionCheck = $container->getByType(FunctionDefinitionCheck::class);
118118
$missingTypehintCheck = $container->getByType(MissingTypehintCheck::class);
119119

120-
/** @var NodeScopeResolver $nodeScopeResolver */
121-
$nodeScopeResolver = $container->getByType(NodeScopeResolver::class);
122-
123120
return new Registry([
124121
// level 0
125122
new ExistingClassesInClassImplementsRule($classCaseSensitivityCheck),
@@ -149,7 +146,7 @@ private function getRuleRegistry(Container $container): Registry
149146
$container->getByType(Lexer::class),
150147
$container->getByType(PhpDocParser::class)
151148
),
152-
new InvalidThrowsPhpDocValueRule($fileTypeMapper, $nodeScopeResolver),
149+
new InvalidThrowsPhpDocValueRule(),
153150

154151
// level 6
155152
new MissingFunctionParameterTypehintRule($reflectionProvider, $missingTypehintCheck),

src/Rules/PhpDoc/InvalidThrowsPhpDocValueRule.php

Lines changed: 17 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,108 +3,66 @@
33
namespace PHPStan\Rules\PhpDoc;
44

55
use PhpParser\Node;
6-
use PHPStan\Analyser\NodeScopeResolver;
76
use PHPStan\Analyser\Scope;
7+
use PHPStan\Node\InClassMethodNode;
8+
use PHPStan\Node\InFunctionNode;
9+
use PHPStan\Node\VirtualNode;
810
use PHPStan\Rules\RuleError;
911
use PHPStan\Rules\RuleErrorBuilder;
10-
use PHPStan\Type\FileTypeMapper;
1112
use PHPStan\Type\ObjectType;
1213
use PHPStan\Type\Type;
1314
use PHPStan\Type\VerbosityLevel;
1415
use PHPStan\Type\VoidType;
1516

1617
/**
17-
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\FunctionLike>
18+
* @implements \PHPStan\Rules\Rule<VirtualNode>
1819
*/
1920
class InvalidThrowsPhpDocValueRule implements \PHPStan\Rules\Rule
2021
{
2122

22-
/** @var FileTypeMapper */
23-
private $fileTypeMapper;
24-
25-
/** @var NodeScopeResolver */
26-
private $nodeScopeResolver;
27-
28-
public function __construct(
29-
FileTypeMapper $fileTypeMapper,
30-
NodeScopeResolver $nodeScopeResolver
31-
)
32-
{
33-
$this->fileTypeMapper = $fileTypeMapper;
34-
$this->nodeScopeResolver = $nodeScopeResolver;
35-
}
36-
3723
public function getNodeType(): string
3824
{
39-
return \PhpParser\Node\FunctionLike::class;
25+
return VirtualNode::class;
4026
}
4127

4228
public function processNode(Node $node, Scope $scope): array
4329
{
44-
$throwsType = $this->getThrowsType($node, $scope);
45-
return $this->check($throwsType);
46-
}
47-
48-
private function getThrowsType(Node $node, Scope $scope): ?Type
49-
{
50-
return $node instanceof Node\Stmt\ClassMethod
51-
? $this->getMethodThrowsType($node, $scope)
52-
: $this->getFunctionThrowsType($node, $scope);
53-
}
54-
55-
private function getFunctionThrowsType(Node $node, Scope $scope): ?Type
56-
{
57-
$docComment = $node->getDocComment();
58-
if ($docComment === null) {
59-
return null;
30+
if (!$node instanceof InFunctionNode && !$node instanceof InClassMethodNode) {
31+
return [];
6032
}
6133

62-
$functionName = null;
63-
if ($node instanceof Node\Stmt\Function_) {
64-
$functionName = trim($scope->getNamespace() . '\\' . $node->name->name, '\\');
34+
if ($scope->getFunction() === null) {
35+
throw new \PHPStan\ShouldNotHappenException();
6536
}
6637

67-
$resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc(
68-
$scope->getFile(),
69-
$scope->isInClass() ? $scope->getClassReflection()->getName() : null,
70-
$scope->isInTrait() ? $scope->getTraitReflection()->getName() : null,
71-
$functionName,
72-
$docComment->getText()
73-
);
38+
$throwType = $scope->getFunction()->getThrowType();
7439

75-
$throwsTag = $resolvedPhpDoc->getThrowsTag();
76-
return $throwsTag === null ? null : $throwsTag->getType();
77-
}
78-
79-
private function getMethodThrowsType(Node\Stmt\ClassMethod $node, Scope $scope): ?Type
80-
{
81-
[, , , $phpDocThrowType] = $this->nodeScopeResolver->getPhpDocs($scope, $node);
82-
return $phpDocThrowType;
40+
return $this->check($throwType);
8341
}
8442

8543
/**
86-
* @param Type|null $phpDocThrowsType
44+
* @param Type|null $phpDocThrowType
8745
* @return array<int, RuleError> errors
8846
*/
89-
private function check(?Type $phpDocThrowsType): array
47+
private function check(?Type $phpDocThrowType): array
9048
{
91-
if ($phpDocThrowsType === null) {
49+
if ($phpDocThrowType === null) {
9250
return [];
9351
}
9452

95-
if ((new VoidType())->isSuperTypeOf($phpDocThrowsType)->yes()) {
53+
if ((new VoidType())->isSuperTypeOf($phpDocThrowType)->yes()) {
9654
return [];
9755
}
9856

99-
$isThrowsSuperType = (new ObjectType(\Throwable::class))->isSuperTypeOf($phpDocThrowsType);
57+
$isThrowsSuperType = (new ObjectType(\Throwable::class))->isSuperTypeOf($phpDocThrowType);
10058
if ($isThrowsSuperType->yes()) {
10159
return [];
10260
}
10361

10462
return [
10563
RuleErrorBuilder::message(sprintf(
10664
'PHPDoc tag @throws with type %s is not subtype of Throwable',
107-
$phpDocThrowsType->describe(VerbosityLevel::typeOnly())
65+
$phpDocThrowType->describe(VerbosityLevel::typeOnly())
10866
))->build(),
10967
];
11068
}

tests/PHPStan/Rules/PhpDoc/InvalidThrowsPhpDocValueRuleTest.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
namespace PHPStan\Rules\PhpDoc;
44

5-
use PHPStan\Analyser\NodeScopeResolver;
6-
use PHPStan\Type\FileTypeMapper;
7-
85
/**
96
* @extends \PHPStan\Testing\RuleTestCase<InvalidThrowsPhpDocValueRule>
107
*/
@@ -13,10 +10,7 @@ class InvalidThrowsPhpDocValueRuleTest extends \PHPStan\Testing\RuleTestCase
1310

1411
protected function getRule(): \PHPStan\Rules\Rule
1512
{
16-
return new InvalidThrowsPhpDocValueRule(
17-
self::getContainer()->getByType(FileTypeMapper::class),
18-
self::getContainer()->getByType(NodeScopeResolver::class)
19-
);
13+
return new InvalidThrowsPhpDocValueRule();
2014
}
2115

2216
public function testRule(): void

0 commit comments

Comments
 (0)