Skip to content

Commit b775f8d

Browse files
committed
Adjust InvalidThrowsPhpDocValueRule for property hooks
1 parent 31cfe22 commit b775f8d

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

src/Rules/PhpDoc/InvalidThrowsPhpDocValueRule.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace PHPStan\Rules\PhpDoc;
44

55
use PhpParser\Node;
6+
use PhpParser\NodeAbstract;
67
use PHPStan\Analyser\Scope;
8+
use PHPStan\Node\InPropertyHookNode;
79
use PHPStan\Rules\Rule;
810
use PHPStan\Rules\RuleErrorBuilder;
911
use PHPStan\Type\FileTypeMapper;
@@ -16,7 +18,7 @@
1618
use function sprintf;
1719

1820
/**
19-
* @implements Rule<Node\Stmt>
21+
* @implements Rule<NodeAbstract>
2022
*/
2123
final class InvalidThrowsPhpDocValueRule implements Rule
2224
{
@@ -27,13 +29,17 @@ public function __construct(private FileTypeMapper $fileTypeMapper)
2729

2830
public function getNodeType(): string
2931
{
30-
return Node\Stmt::class;
32+
return NodeAbstract::class;
3133
}
3234

3335
public function processNode(Node $node, Scope $scope): array
3436
{
35-
if ($node instanceof Node\Stmt\Function_ || $node instanceof Node\Stmt\ClassMethod) {
36-
return []; // is handled by virtual nodes
37+
if ($node instanceof Node\Stmt) {
38+
if ($node instanceof Node\Stmt\Function_ || $node instanceof Node\Stmt\ClassMethod) {
39+
return []; // is handled by virtual nodes
40+
}
41+
} elseif (!$node instanceof InPropertyHookNode) {
42+
return [];
3743
}
3844

3945
$docComment = $node->getDocComment();

tests/PHPStan/Rules/PhpDoc/InvalidThrowsPhpDocValueRuleTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPStan\Testing\RuleTestCase;
1010
use PHPStan\Type\FileTypeMapper;
1111
use PHPStan\Type\VerbosityLevel;
12+
use const PHP_VERSION_ID;
1213

1314
/**
1415
* @extends RuleTestCase<InvalidThrowsPhpDocValueRule>
@@ -137,4 +138,18 @@ public function testMergeInheritedPhpDocs(
137138
$this->assertSame($expectedType, $throwsType->describe(VerbosityLevel::precise()));
138139
}
139140

141+
public function testPropertyHooks(): void
142+
{
143+
if (PHP_VERSION_ID < 80400) {
144+
$this->markTestSkipped('Test requires PHP 8.4.');
145+
}
146+
147+
$this->analyse([__DIR__ . '/data/invalid-throws-property-hook.php'], [
148+
[
149+
'PHPDoc tag @throws with type DateTimeImmutable is not subtype of Throwable',
150+
17,
151+
],
152+
]);
153+
}
154+
140155
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php // lint >= 8.4
2+
3+
namespace InvalidThrowsPropertyHook;
4+
5+
class Foo
6+
{
7+
8+
public int $i {
9+
/** @throws \InvalidArgumentException */
10+
get {
11+
return 1;
12+
}
13+
}
14+
15+
public int $j {
16+
/** @throws \DateTimeImmutable */
17+
get {
18+
return 1;
19+
}
20+
}
21+
22+
}

0 commit comments

Comments
 (0)