-
Notifications
You must be signed in to change notification settings - Fork 3
/
NamespaceBasedSuffixRule.php
60 lines (50 loc) · 1.39 KB
/
NamespaceBasedSuffixRule.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
<?php
namespace Worksome\CodingStyle\PHPStan;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
/**
* @implements Rule<Class_>
*/
class NamespaceBasedSuffixRule implements Rule
{
/**
* @param array<string, string> $namespaceAndSuffix
*/
public function __construct(
private array $namespaceAndSuffix,
) {
}
public function getNodeType(): string
{
return Class_::class;
}
/**
* @param Class_ $node
*/
public function processNode(Node $node, Scope $scope): array
{
$fullyQualifiedNameSpace = ($node->namespacedName ?? null)?->toString();
if ($fullyQualifiedNameSpace === null) {
return [];
}
foreach ($this->namespaceAndSuffix as $nameSpace => $suffix) {
if (! str_starts_with($fullyQualifiedNameSpace, $nameSpace)) {
continue;
}
if (str_ends_with($fullyQualifiedNameSpace, $suffix)) {
continue;
}
return [
RuleErrorBuilder::message(
"Classes in namespace $nameSpace should always be suffixed with $suffix."
)
->identifier('worksome.namespaceBasedSuffix')
->build(),
];
}
return [];
}
}