-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandClassShouldBeHelpCommandHandlerClass.php
More file actions
94 lines (83 loc) · 2.62 KB
/
CommandClassShouldBeHelpCommandHandlerClass.php
File metadata and controls
94 lines (83 loc) · 2.62 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
<?php
declare(strict_types=1);
namespace Simtel\PHPStanRules\Rule;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Analyser\Scope;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
/**
* @implements Rule<Class_>
*/
final class CommandClassShouldBeHelpCommandHandlerClass implements Rule
{
public function __construct(
private readonly PhpDocParser $parser,
private readonly Lexer $phpDocLexer,
) {
}
public function getNodeType(): string
{
return Class_::class;
}
/**
* @param Class_ $node
* @param Scope $scope
*
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
$className = '';
if ($node->name !== null) {
$className = $node->name->name;
}
if (! str_ends_with($className, 'Command')) {
return [];
}
$methods = $node->getMethods();
foreach ($methods as $method) {
if ($method->name->name === '__invoke') {
return [];
}
}
$find = false;
$doc = $node->getDocComment()?->getText() ?? '';
if ($doc === '') {
return [RuleErrorBuilder::message('Command class should be include phpDoc with @see attribute')->build()];
}
$tokens = new TokenIterator($this->phpDocLexer->tokenize($doc));
$text = $this->parser->parse($tokens);
foreach ($text->getTags() as $tag) {
if ($tag->name !== '@see') {
continue;
}
if ($tag->value instanceof GenericTagValueNode) {
$find = true;
$value = $tag->value->value;
if (! str_ends_with($value, 'CommandHandler')) {
return [
RuleErrorBuilder::message(
sprintf(
'PhpDoc command class should be include @see attribute with CommandHandler class name, but include %s',
$value
)
)->build(),
];
}
}
}
if ($find === false) {
return [
RuleErrorBuilder::message(
'PhpDoc command class should be include @see attribute with CommandHandler class name'
)->build(),
];
}
return [];
}
}