-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathNoMissnamedDocTagRule.php
More file actions
103 lines (85 loc) · 3.03 KB
/
NoMissnamedDocTagRule.php
File metadata and controls
103 lines (85 loc) · 3.03 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
<?php
declare(strict_types=1);
namespace Symplify\PHPStanRules\Rules;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use Symplify\PHPStanRules\Enum\RuleIdentifier;
/**
* @implements Rule<Class_>
*
* @see \Symplify\PHPStanRules\Tests\Rules\NoMissnamedDocTagRule\NoMissnamedDocTagRuleTest
*/
final class NoMissnamedDocTagRule implements Rule
{
/**
* @api used in tests
*/
public const CONSTANT_ERROR_MESSAGE = 'Constant doc comment tag must be @var, "%s" given';
/**
* @api used in tests
*/
public const PROPERTY_ERROR_MESSAGE = 'Property doc comment tag must be @var, "%s" given';
/**
* @api used in tests
*/
public const METHOD_ERROR_MESSAGE = 'Method doc comment tag must be @param or @return, "%s" given';
public function getNodeType(): string
{
return Class_::class;
}
/**
* @param Class_ $node
* @return array<RuleError>
*/
public function processNode(Node $node, Scope $scope): array
{
$ruleErrors = [];
foreach ($node->getMethods() as $classMethod) {
// match "@return" and "@param" tags
if ($classMethod->getDocComment() === null) {
continue;
}
$matches = Strings::match($classMethod->getDocComment()->getText(), '#(@var)\b#mi');
if ($matches === null) {
continue;
}
$ruleErrors[] = RuleErrorBuilder::message(sprintf(self::METHOD_ERROR_MESSAGE, $matches[1]))
->identifier(RuleIdentifier::NO_MISSNAMED_DOC_TAG)
->line($classMethod->getStartLine())
->build();
}
foreach ($node->getProperties() as $property) {
// match "@return" and "@param" tags
if ($property->getDocComment() === null) {
continue;
}
$matches = Strings::match($property->getDocComment()->getText(), '#(@param|@return)\b#mi');
if ($matches === null) {
continue;
}
$ruleErrors[] = RuleErrorBuilder::message(sprintf(self::PROPERTY_ERROR_MESSAGE, $matches[1]))
->identifier(RuleIdentifier::NO_MISSNAMED_DOC_TAG)
->line($property->getStartLine())
->build();
}
foreach ($node->getConstants() as $classConst) {
if ($classConst->getDocComment() === null) {
continue;
}
$matches = Strings::match($classConst->getDocComment()->getText(), '#(@param|@return)\b#mi');
if ($matches === null) {
continue;
}
$ruleErrors[] = RuleErrorBuilder::message(sprintf(self::CONSTANT_ERROR_MESSAGE, $matches[1]))
->identifier(RuleIdentifier::NO_MISSNAMED_DOC_TAG)
->line($classConst->getStartLine())
->build();
}
return $ruleErrors;
}
}