forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvalidPHPStanDocTagRule.php
More file actions
127 lines (115 loc) · 3.08 KB
/
InvalidPHPStanDocTagRule.php
File metadata and controls
127 lines (115 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php declare(strict_types = 1);
namespace PHPStan\Rules\PhpDoc;
use PhpParser\Node;
use PhpParser\NodeAbstract;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Node\VirtualNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function in_array;
use function sprintf;
use function str_starts_with;
/**
* @implements Rule<NodeAbstract>
*/
#[RegisteredRule(level: 2)]
final class InvalidPHPStanDocTagRule implements Rule
{
private const POSSIBLE_PHPSTAN_TAGS = [
'@phpstan-param',
'@phpstan-param-out',
'@phpstan-var',
'@phpstan-extends',
'@phpstan-implements',
'@phpstan-use',
'@phpstan-template',
'@phpstan-template-contravariant',
'@phpstan-template-covariant',
'@phpstan-return',
'@phpstan-throws',
'@phpstan-ignore',
'@phpstan-ignore-next-line',
'@phpstan-ignore-line',
'@phpstan-method',
'@phpstan-pure',
'@phpstan-impure',
'@phpstan-immutable',
'@phpstan-type',
'@phpstan-import-type',
'@phpstan-property',
'@phpstan-property-read',
'@phpstan-property-write',
'@phpstan-consistent-constructor',
'@phpstan-assert',
'@phpstan-assert-if-true',
'@phpstan-assert-if-false',
'@phpstan-self-out',
'@phpstan-this-out',
'@phpstan-allow-private-mutation',
'@phpstan-readonly',
'@phpstan-readonly-allow-private-mutation',
'@phpstan-require-extends',
'@phpstan-require-implements',
'@phpstan-sealed',
'@phpstan-param-immediately-invoked-callable',
'@phpstan-param-later-invoked-callable',
'@phpstan-param-closure-this',
'@phpstan-all-methods-pure',
'@phpstan-all-methods-impure',
];
public function __construct(
private Lexer $phpDocLexer,
private PhpDocParser $phpDocParser,
)
{
}
public function getNodeType(): string
{
return NodeAbstract::class;
}
public function processNode(Node $node, Scope $scope): array
{
// mirrored with InvalidPhpDocTagValueRule
if ($node instanceof VirtualNode) {
return [];
}
if (!$node instanceof Node\Stmt && !$node instanceof Node\PropertyHook) {
return [];
}
if ($node instanceof Node\Stmt\Expression) {
if (
!$node->expr instanceof Node\Expr\Assign
&& !$node->expr instanceof Node\Expr\AssignRef
&& !$node->expr instanceof Node\Expr\AssignOp
) {
return [];
}
}
$docComment = $node->getDocComment();
if ($docComment === null) {
return [];
}
$phpDocString = $docComment->getText();
$tokens = new TokenIterator($this->phpDocLexer->tokenize($phpDocString));
$phpDocNode = $this->phpDocParser->parse($tokens);
$errors = [];
foreach ($phpDocNode->getTags() as $phpDocTag) {
if (!str_starts_with($phpDocTag->name, '@phpstan-')
|| in_array($phpDocTag->name, self::POSSIBLE_PHPSTAN_TAGS, true)
) {
continue;
}
$errors[] = RuleErrorBuilder::message(sprintf(
'Unknown PHPDoc tag: %s',
$phpDocTag->name,
))
->line(PhpDocLineHelper::detectLine($node, $phpDocTag))
->identifier('phpDoc.phpstanTag')->build();
}
return $errors;
}
}