Skip to content

Commit 7f9538c

Browse files
committed
Adjust InvalidPhpDocTagValueRule and InvalidPHPStanDocTagRule for property hooks
1 parent b775f8d commit 7f9538c

File tree

6 files changed

+72
-4
lines changed

6 files changed

+72
-4
lines changed

src/Rules/PhpDoc/InvalidPHPStanDocTagRule.php

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

55
use PhpParser\Node;
6+
use PhpParser\NodeAbstract;
67
use PHPStan\Analyser\Scope;
78
use PHPStan\Node\VirtualNode;
89
use PHPStan\PhpDocParser\Lexer\Lexer;
@@ -15,7 +16,7 @@
1516
use function str_starts_with;
1617

1718
/**
18-
* @implements Rule<Node\Stmt>
19+
* @implements Rule<NodeAbstract>
1920
*/
2021
final class InvalidPHPStanDocTagRule implements Rule
2122
{
@@ -69,7 +70,7 @@ public function __construct(
6970

7071
public function getNodeType(): string
7172
{
72-
return Node\Stmt::class;
73+
return NodeAbstract::class;
7374
}
7475

7576
public function processNode(Node $node, Scope $scope): array
@@ -78,6 +79,9 @@ public function processNode(Node $node, Scope $scope): array
7879
if ($node instanceof VirtualNode) {
7980
return [];
8081
}
82+
if (!$node instanceof Node\Stmt && !$node instanceof Node\PropertyHook) {
83+
return [];
84+
}
8185
if ($node instanceof Node\Stmt\Expression) {
8286
if (!$node->expr instanceof Node\Expr\Assign && !$node->expr instanceof Node\Expr\AssignRef) {
8387
return [];

src/Rules/PhpDoc/InvalidPhpDocTagValueRule.php

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

55
use PhpParser\Node;
6+
use PhpParser\NodeAbstract;
67
use PHPStan\Analyser\Scope;
78
use PHPStan\Node\VirtualNode;
89
use PHPStan\PhpDocParser\Ast\PhpDoc\InvalidTagValueNode;
@@ -17,7 +18,7 @@
1718
use function str_starts_with;
1819

1920
/**
20-
* @implements Rule<Node\Stmt>
21+
* @implements Rule<NodeAbstract>
2122
*/
2223
final class InvalidPhpDocTagValueRule implements Rule
2324
{
@@ -31,7 +32,7 @@ public function __construct(
3132

3233
public function getNodeType(): string
3334
{
34-
return Node\Stmt::class;
35+
return NodeAbstract::class;
3536
}
3637

3738
public function processNode(Node $node, Scope $scope): array
@@ -40,6 +41,9 @@ public function processNode(Node $node, Scope $scope): array
4041
if ($node instanceof VirtualNode) {
4142
return [];
4243
}
44+
if (!$node instanceof Node\Stmt && !$node instanceof Node\PropertyHook) {
45+
return [];
46+
}
4347
if ($node instanceof Node\Stmt\Expression) {
4448
if (!$node->expr instanceof Node\Expr\Assign && !$node->expr instanceof Node\Expr\AssignRef) {
4549
return [];

tests/PHPStan/Rules/PhpDoc/InvalidPHPStanDocTagRuleTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\PhpDocParser\Parser\PhpDocParser;
77
use PHPStan\Rules\Rule;
88
use PHPStan\Testing\RuleTestCase;
9+
use const PHP_VERSION_ID;
910

1011
/**
1112
* @extends RuleTestCase<InvalidPHPStanDocTagRule>
@@ -52,4 +53,18 @@ public function testBug8697(): void
5253
$this->analyse([__DIR__ . '/data/bug-8697.php'], []);
5354
}
5455

56+
public function testPropertyHooks(): void
57+
{
58+
if (PHP_VERSION_ID < 80400) {
59+
$this->markTestSkipped('Test requires PHP 8.4.');
60+
}
61+
62+
$this->analyse([__DIR__ . '/data/invalid-phpstan-tag-property-hooks.php'], [
63+
[
64+
'Unknown PHPDoc tag: @phpstan-what',
65+
9,
66+
],
67+
]);
68+
}
69+
5570
}

tests/PHPStan/Rules/PhpDoc/InvalidPhpDocTagValueRuleTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\PhpDocParser\Parser\PhpDocParser;
77
use PHPStan\Rules\Rule;
88
use PHPStan\Testing\RuleTestCase;
9+
use const PHP_VERSION_ID;
910

1011
/**
1112
* @extends RuleTestCase<InvalidPhpDocTagValueRule>
@@ -144,4 +145,18 @@ public function testBug6692(): void
144145
]);
145146
}
146147

148+
public function testPropertyHooks(): void
149+
{
150+
if (PHP_VERSION_ID < 80400) {
151+
$this->markTestSkipped('Test requires PHP 8.4.');
152+
}
153+
154+
$this->analyse([__DIR__ . '/data/invalid-phpdoc-property-hooks.php'], [
155+
[
156+
'PHPDoc tag @return has invalid value (Test(): Unexpected token "(", expected TOKEN_HORIZONTAL_WS at offset 16 on line 1',
157+
9,
158+
],
159+
]);
160+
}
161+
147162
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php // lint >= 8.4
2+
3+
namespace InvalidPhpDocPropertyHooks;
4+
5+
class Foo
6+
{
7+
8+
public int $i {
9+
/** @return Test( */
10+
get {
11+
12+
}
13+
}
14+
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php // lint >= 8.4
2+
3+
namespace InvalidPHPStanTagPropertyHooks;
4+
5+
class Foo
6+
{
7+
8+
public int $i {
9+
/** @phpstan-what what */
10+
get {
11+
12+
}
13+
}
14+
15+
}

0 commit comments

Comments
 (0)