Skip to content

Commit 8d42830

Browse files
Check #[CoversClass], #[CoversTrait], #[CoversMethod] and #[CoversFunction] attributes
1 parent 7a756d3 commit 8d42830

3 files changed

Lines changed: 194 additions & 0 deletions

File tree

‎src/Rules/PHPUnit/ClassCoversExistsRule.php‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
namespace PHPStan\Rules\PHPUnit;
44

55
use PhpParser\Node;
6+
use PhpParser\Node\Name;
67
use PHPStan\Analyser\Scope;
78
use PHPStan\Node\InClassNode;
9+
use PHPStan\Reflection\ClassReflection;
810
use PHPStan\Reflection\ReflectionProvider;
11+
use PHPStan\Rules\IdentifierRuleError;
912
use PHPStan\Rules\Rule;
1013
use PHPStan\Rules\RuleErrorBuilder;
1114
use PHPUnit\Framework\TestCase;
1215
use function array_merge;
1316
use function array_shift;
1417
use function count;
18+
use function is_string;
1519
use function sprintf;
1620

1721
/**
@@ -85,7 +89,114 @@ public function processNode(Node $node, Scope $scope): array
8589
);
8690
}
8791

92+
return array_merge($errors, $this->processCoversAttributes($classReflection));
93+
}
94+
95+
/**
96+
* Checks the #[CoversClass], #[CoversTrait], #[CoversMethod] and #[CoversFunction] attributes.
97+
*
98+
* @return list<IdentifierRuleError>
99+
*/
100+
private function processCoversAttributes(ClassReflection $classReflection): array
101+
{
102+
$betterReflection = $classReflection->getNativeReflection()->getBetterReflection();
103+
$errors = [];
104+
105+
foreach ($betterReflection->getAttributesByName('PHPUnit\Framework\Attributes\CoversClass') as $attribute) {
106+
$className = $this->getStringArgument($attribute->getArguments(), 0, 'className');
107+
if ($className === null) {
108+
continue;
109+
}
110+
111+
if (!$this->reflectionProvider->hasClass($className)) {
112+
$errors[] = RuleErrorBuilder::message(sprintf(
113+
'#[CoversClass] value %s references an invalid class.',
114+
$className,
115+
))->identifier('phpunit.coversClass')->build();
116+
continue;
117+
}
118+
119+
if (!$this->reflectionProvider->getClass($className)->isInterface()) {
120+
continue;
121+
}
122+
123+
$errors[] = RuleErrorBuilder::message(sprintf(
124+
'#[CoversClass] value %s references an interface.',
125+
$className,
126+
))->identifier('phpunit.coversInterface')->build();
127+
}
128+
129+
foreach ($betterReflection->getAttributesByName('PHPUnit\Framework\Attributes\CoversTrait') as $attribute) {
130+
$traitName = $this->getStringArgument($attribute->getArguments(), 0, 'traitName');
131+
if ($traitName === null) {
132+
continue;
133+
}
134+
135+
if ($this->reflectionProvider->hasClass($traitName) && $this->reflectionProvider->getClass($traitName)->isTrait()) {
136+
continue;
137+
}
138+
139+
$errors[] = RuleErrorBuilder::message(sprintf(
140+
'#[CoversTrait] value %s references an invalid trait.',
141+
$traitName,
142+
))->identifier('phpunit.coversTrait')->build();
143+
}
144+
145+
foreach ($betterReflection->getAttributesByName('PHPUnit\Framework\Attributes\CoversMethod') as $attribute) {
146+
$arguments = $attribute->getArguments();
147+
$className = $this->getStringArgument($arguments, 0, 'className');
148+
$methodName = $this->getStringArgument($arguments, 1, 'methodName');
149+
if ($className === null || $methodName === null) {
150+
continue;
151+
}
152+
153+
if (!$this->reflectionProvider->hasClass($className)) {
154+
$errors[] = RuleErrorBuilder::message(sprintf(
155+
'#[CoversMethod] value %s::%s references an invalid class.',
156+
$className,
157+
$methodName,
158+
))->identifier('phpunit.coversMethod')->build();
159+
continue;
160+
}
161+
162+
if ($this->reflectionProvider->getClass($className)->hasMethod($methodName)) {
163+
continue;
164+
}
165+
166+
$errors[] = RuleErrorBuilder::message(sprintf(
167+
'#[CoversMethod] value %s::%s references an invalid method.',
168+
$className,
169+
$methodName,
170+
))->identifier('phpunit.coversMethod')->build();
171+
}
172+
173+
foreach ($betterReflection->getAttributesByName('PHPUnit\Framework\Attributes\CoversFunction') as $attribute) {
174+
$functionName = $this->getStringArgument($attribute->getArguments(), 0, 'functionName');
175+
if ($functionName === null) {
176+
continue;
177+
}
178+
179+
if ($this->reflectionProvider->hasFunction(new Name($functionName), null)) {
180+
continue;
181+
}
182+
183+
$errors[] = RuleErrorBuilder::message(sprintf(
184+
'#[CoversFunction] value %s references an invalid function.',
185+
$functionName,
186+
))->identifier('phpunit.coversFunction')->build();
187+
}
188+
88189
return $errors;
89190
}
90191

192+
/**
193+
* @param array<int|string, mixed> $arguments
194+
*/
195+
private function getStringArgument(array $arguments, int $position, string $name): ?string
196+
{
197+
$value = $arguments[$position] ?? $arguments[$name] ?? null;
198+
199+
return is_string($value) && $value !== '' ? $value : null;
200+
}
201+
91202
}

‎tests/Rules/PHPUnit/ClassCoversExistsRuleTest.php‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,39 @@ public function testRule(): void
5252
]);
5353
}
5454

55+
/**
56+
* @requires PHP 8.0
57+
*/
58+
public function testAttributes(): void
59+
{
60+
$this->analyse([__DIR__ . '/data/class-coverage-attributes.php'], [
61+
[
62+
'#[CoversClass] value Not\\A\\Klass references an invalid class.',
63+
37,
64+
],
65+
[
66+
'#[CoversClass] value DateTimeInterface references an interface.',
67+
37,
68+
],
69+
[
70+
'#[CoversTrait] value ClassCoverageAttributes\\Covered references an invalid trait.',
71+
37,
72+
],
73+
[
74+
'#[CoversMethod] value ClassCoverageAttributes\\Covered::missing references an invalid method.',
75+
37,
76+
],
77+
[
78+
'#[CoversMethod] value Not\\A\\Klass::covered references an invalid class.',
79+
37,
80+
],
81+
[
82+
'#[CoversFunction] value ClassCoverageAttributes\\missingFunction references an invalid function.',
83+
37,
84+
],
85+
]);
86+
}
87+
5588
/**
5689
* @return string[]
5790
*/
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ClassCoverageAttributes;
4+
5+
use PHPUnit\Framework\Attributes\CoversClass;
6+
use PHPUnit\Framework\Attributes\CoversFunction;
7+
use PHPUnit\Framework\Attributes\CoversMethod;
8+
use PHPUnit\Framework\Attributes\CoversTrait;
9+
10+
class Covered
11+
{
12+
13+
public function covered(): void
14+
{
15+
}
16+
17+
}
18+
19+
trait CoveredTrait
20+
{
21+
}
22+
23+
function coveredFunction(): void
24+
{
25+
}
26+
27+
#[CoversClass(Covered::class)]
28+
#[CoversClass(className: Covered::class)]
29+
#[CoversTrait(CoveredTrait::class)]
30+
#[CoversMethod(Covered::class, 'covered')]
31+
#[CoversFunction('ClassCoverageAttributes\coveredFunction')]
32+
#[CoversFunction('str_replace')]
33+
class ValidCoversAttributes extends \PHPUnit\Framework\TestCase
34+
{
35+
}
36+
37+
#[CoversClass(\Not\A\Klass::class)]
38+
#[CoversClass(\DateTimeInterface::class)]
39+
#[CoversTrait(Covered::class)]
40+
#[CoversMethod(Covered::class, 'missing')]
41+
#[CoversMethod(\Not\A\Klass::class, 'covered')]
42+
#[CoversFunction('ClassCoverageAttributes\missingFunction')]
43+
class InvalidCoversAttributes extends \PHPUnit\Framework\TestCase
44+
{
45+
}
46+
47+
#[CoversClass(\Not\A\Klass::class)]
48+
class NotATestCase
49+
{
50+
}

0 commit comments

Comments
 (0)