Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c86e460

Browse files
mad-brillerondrejmirtes
authored andcommitted
Fix covers rule for functions.
1 parent e431a6c commit c86e460

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

‎src/Rules/PHPUnit/CoversHelper.php‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\Rules\PHPUnit;
44

55
use PhpParser\Node;
6+
use PhpParser\Node\Name;
67
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
78
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
89
use PHPStan\Reflection\ReflectionProvider;
@@ -70,8 +71,9 @@ public function processCovers(
7071
{
7172
$errors = [];
7273
$covers = (string) $phpDocTag->value;
74+
$isMethod = strpos($covers, '::') !== false;
7375

74-
if (strpos($covers, '::') !== false) {
76+
if ($isMethod) {
7577
[$className, $method] = explode('::', $covers);
7678
} else {
7779
$className = $covers;
@@ -83,16 +85,22 @@ public function processCovers(
8385

8486
if ($this->reflectionProvider->hasClass($className)) {
8587
$class = $this->reflectionProvider->getClass($className);
88+
8689
if (isset($method) && $method !== '' && !$class->hasMethod($method)) {
8790
$errors[] = RuleErrorBuilder::message(sprintf(
8891
'@covers value %s references an invalid method.',
8992
$covers
9093
))->build();
9194
}
9295
} else {
96+
if (!isset($method) && $this->reflectionProvider->hasFunction(new Name($covers, []), null)) {
97+
return $errors;
98+
}
99+
93100
$errors[] = RuleErrorBuilder::message(sprintf(
94-
'@covers value %s references an invalid class.',
95-
$covers
101+
'@covers value %s references an invalid %s.',
102+
$covers,
103+
$isMethod ? 'method' : 'class or function'
96104
))->build();
97105
}
98106
return $errors;

‎tests/Rules/PHPUnit/ClassMethodCoversExistsRuleTest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ public function testRule(): void
2626
{
2727
$this->analyse([__DIR__ . '/data/method-coverage.php'], [
2828
[
29-
'@covers value ::ignoreThis references an invalid class.',
29+
'@covers value ::ignoreThis references an invalid method.',
3030
14,
3131
],
3232
[
3333
'@covers value \PHPUnit\Framework\TestCase::assertNotReal references an invalid method.',
3434
28,
3535
],
3636
[
37-
'@covers value \Not\A\Class::foo references an invalid class.',
37+
'@covers value \Not\A\Class::foo references an invalid method.',
3838
35,
3939
],
4040
[

‎tests/Rules/PHPUnit/data/class-coverage.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,15 @@ class CoversShouldExistTestCase2 extends \PHPUnit\Framework\TestCase
2323
class MultipleCoversDefaultClass extends \PHPUnit\Framework\TestCase
2424
{
2525
}
26+
27+
/**
28+
* @covers \ClassCoverage\testable
29+
*/
30+
class CoversFunction extends \PHPUnit\Framework\TestCase
31+
{
32+
}
33+
34+
function testable(): void
35+
{
36+
37+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /