forked from phpstan/phpstan-phpunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassNamingRule.php
111 lines (91 loc) · 2.42 KB
/
ClassNamingRule.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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\PHPUnit;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPUnit\Framework\TestCase;
use function sprintf;
use function strlen;
use function substr_compare;
/**
* @implements Rule<Node\Stmt\Class_>
*/
class ClassNamingRule implements Rule
{
private ReflectionProvider $reflectionProvider;
public function __construct(ReflectionProvider $reflectionProvider)
{
$this->reflectionProvider = $reflectionProvider;
}
public function getNodeType(): string
{
return Node\Stmt\Class_::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (!isset($node->namespacedName)) {
return [];
}
$class = $this->reflectionProvider->getClass($node->namespacedName->toString());
if (!$class->isSubclassOf(TestCase::class)) {
return [];
}
$errors = [];
if ($class->isAbstract()) {
$this->requireSuffix(
$errors,
$class->getName(),
'TestCase',
'Abstract test case class, \'%s\', should be named ending in \'%s\'.',
);
return $errors;
}
$this->requireSuffix(
$errors,
$class->getName(),
'Test',
'Concrete test class, \'%s\', should be named ending in \'%s\'.',
);
if (!$class->isFinal()) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Concrete test class, \'%s\', should be declared final.',
$class->getName(),
))->identifier('phpunit.naming')->build();
}
return $errors;
}
/**
* @param list<IdentifierRuleError> $errors
* @param class-string $className
* @param non-empty-string $suffix
*/
private function requireSuffix(array &$errors, string $className, string $suffix, string $messageFormat): void
{
if ($this->hasSuffix($className, $suffix)) {
return;
}
$errors[] = RuleErrorBuilder::message(sprintf(
$messageFormat,
$className,
$suffix,
))->identifier('phpunit.naming')->build();
}
/**
* Checks if class name has the given suffix.
*
* Comparison is case insensitive.
*
* @param class-string $className
* @param non-empty-string $suffix
*/
private function hasSuffix(string $className, string $suffix): bool
{
$classNameLen = strlen($className);
$suffixLen = strlen($suffix);
return $suffixLen < $classNameLen
&& substr_compare($className, $suffix, -$suffixLen, $suffixLen, true) === 0;
}
}