-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCoversClassPresentRule.php
More file actions
148 lines (117 loc) · 4.22 KB
/
CoversClassPresentRule.php
File metadata and controls
148 lines (117 loc) · 4.22 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
declare(strict_types=1);
namespace BrainbitsPhpStan;
use Nette\Utils\Strings;
use PhpParser\Comment\Doc;
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 function array_key_exists;
use function preg_match;
use function preg_split;
use function sha1;
use function sprintf;
// phpcs:disable SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
/** @implements Rule<Class_> */
final class CoversClassPresentRule implements Rule
{
private const string TEST_CLASS_ENDING_STRING = 'Test';
/** @var string */
private $unitTestNamespaceContainsString;
/** @var array<string, bool> */
private $alreadyParsedDocComments = [];
public function __construct(string $unitTestNamespaceContainsString)
{
$this->unitTestNamespaceContainsString = $unitTestNamespaceContainsString;
}
public function getNodeType(): string
{
return Class_::class;
}
/**
* @param Class_ $node
*
* @return array<RuleError>
*/
public function processNode(Node $node, Scope $scope): array
{
$messages = [];
$isUnitTest = (bool) $node->extends
&& $this->isUnitTest((string) $scope->getNamespace(), (string) $node->name, $this->unitTestNamespaceContainsString);
$hasCovers = $this->processNodeAnnotation($node, $scope) || $this->processNodeAttribute($node, $scope);
if ($isUnitTest && !$hasCovers) {
$messages[] = RuleErrorBuilder::message('No @covers or #[CoversClass] found in test.')
->build();
}
return $messages;
}
public function processNodeAnnotation(Class_ $node, Scope $scope): bool
{
$lines = $this->getAnnotationLines($node, $scope);
foreach ($lines as $lineContent) {
$lineHasCovers = (bool) preg_match('/^(?:\s*\*\s*@(?:covers|coversDefaultClass)\h+)\\\\?(?<className>\w[^:\s]*)(?:::\S+)?\s*$/u', $lineContent, $matches);
if ($lineHasCovers) {
return true;
}
$lineHasCovers = (bool) preg_match('/^(?:\s*\/\*\*\s*@(?:covers|coversDefaultClass)\h+)\\\\?(?<className>\w[^:\s]*)(?:::\S+)?\s*\*\/\s*$/u', $lineContent, $matches);
if ($lineHasCovers) {
return true;
}
}
return false;
}
public function processNodeAttribute(Class_ $node, Scope $scope): bool
{
if (!$node->attrGroups) {
return false;
}
foreach ($node->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $name => $attr) {
if ((string) $attr->name === 'PHPUnit\Framework\Attributes\CoversClass') {
return true;
}
if ((string) $attr->name === 'PHPUnit\Framework\Attributes\CoversFunction') {
return true;
}
if ((string) $attr->name === 'PHPUnit\Framework\Attributes\CoversNothing') {
return true;
}
}
}
return false;
}
/** @return array<string> */
private function getAnnotationLines(Node $node, Scope $scope): array
{
$docComment = $node->getDocComment();
if (!$docComment instanceof Doc) {
return [];
}
$hash = sha1(
sprintf(
'%s:%s:%s:%s',
$scope->getFile(),
$docComment->getStartLine(),
$docComment->getStartFilePos(),
$docComment->getText(),
),
);
if (array_key_exists($hash, $this->alreadyParsedDocComments)) {
return [];
}
$this->alreadyParsedDocComments[$hash] = true;
$lines = [];
foreach ((array) preg_split('/\R/u', $docComment->getText()) as $line) {
$lines[] = (string) $line;
}
return $lines;
}
private function isUnitTest(string $namespace, string $className, string $unitTestNamespacePattern): bool
{
return Strings::contains($namespace, $unitTestNamespacePattern)
&& Strings::endsWith($className, self::TEST_CLASS_ENDING_STRING);
}
}