-
Notifications
You must be signed in to change notification settings - Fork 3
/
EnforceKebabCaseArtisanCommandsRule.php
118 lines (96 loc) · 3.38 KB
/
EnforceKebabCaseArtisanCommandsRule.php
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
<?php
namespace Worksome\CodingStyle\PHPStan\Laravel;
use Illuminate\Console\Command;
use Illuminate\Console\Parser;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
/**
* @implements Rule<Class_>
*/
class EnforceKebabCaseArtisanCommandsRule implements Rule
{
public function __construct(private array $excludedCommandClasses = [])
{
}
public function getNodeType(): string
{
return Class_::class;
}
/**
* @param Class_ $node
*/
public function processNode(Node $node, Scope $scope): array
{
if ($node->namespacedName === null) {
return [];
}
$className = $node->namespacedName->toString();
if (! $node->extends || ! str_contains($node->extends->toString(), Command::class)) {
return [];
}
if (in_array($className, $this->excludedCommandClasses)) {
return [];
}
if (($property = $node->getProperty('signature')) === null) {
return [];
}
return $this->getCommandSignatureErrors($className, $property);
}
public function getCommandSignatureErrors(string $className, Property $signature): array
{
$value = (string) $signature->props[0]->default->value;
$errors = [];
$command = Parser::parse((string) $value);
foreach ($command as $segment) {
if ($segment === []) {
continue;
}
if (is_string($segment)) {
$errors = $this->getCommandNameErrors($segment, $className, $signature, $errors);
}
if (is_array($segment)) {
$errors = $this->getOptionOrArgumentErrors($segment, $className, $signature, $errors);
}
}
return $errors;
}
public function getCommandNameErrors(string $segment, string $className, Property $signature, array $errors): array
{
if (! preg_match('/^[a-z0-9\-:]+$/', $segment)) {
$errors[] = RuleErrorBuilder::message(
"Command \"{$className}\" is not using kebab-case for the command name in its signature."
)
->line($signature->getLine())
->identifier('worksome.laravel.kebabCaseArtisanCommands')
->build();
}
return $errors;
}
public function getOptionOrArgumentErrors(
array $segment,
string $className,
Property $signature,
array $errors,
): array {
foreach ($segment as $optionOrArgument) {
$isArgument = $optionOrArgument instanceof InputArgument;
$isOption = $optionOrArgument instanceof InputOption;
if (! $isArgument && ! $isOption) {
continue;
}
$type = $isArgument ? 'argument' : 'option';
if (! preg_match('/^[a-z0-9\-]+$/', $optionOrArgument->getName())) {
$errors[] = RuleErrorBuilder::message(
"Command \"{$className}\" is not using kebab-case for the \"{$optionOrArgument->getName()}\" {$type} in its signature."
)->line($signature->getLine())->build();
}
}
return $errors;
}
}