Skip to content

Commit

Permalink
Fixed false positive when covering a global function.
Browse files Browse the repository at this point in the history
  • Loading branch information
mad-briller authored and ondrejmirtes committed Feb 28, 2023
1 parent 4a19a3c commit 34ee324
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/Rules/PHPUnit/CoversHelper.php
Expand Up @@ -71,6 +71,13 @@ public function processCovers(
{
$errors = [];
$covers = (string) $phpDocTag->value;

if ($covers === '') {
$errors[] = RuleErrorBuilder::message('@covers value does not specify anything.')->build();

return $errors;
}

$isMethod = strpos($covers, '::') !== false;
$fullName = $covers;

Expand All @@ -94,17 +101,12 @@ public function processCovers(
$fullName
))->build();
}
} else {
if ($covers === '') {
$errors[] = RuleErrorBuilder::message('@covers value does not specify anything.')->build();

return $errors;
}

if (!isset($method) && $this->reflectionProvider->hasFunction(new Name($covers, []), null)) {
return $errors;
}
} elseif (isset($method) && $this->reflectionProvider->hasFunction(new Name($method, []), null)) {
return $errors;
} elseif (!isset($method) && $this->reflectionProvider->hasFunction(new Name($className, []), null)) {
return $errors;

} else {
$error = RuleErrorBuilder::message(sprintf(
'@covers value %s references an invalid %s.',
$fullName,
Expand Down
7 changes: 7 additions & 0 deletions tests/Rules/PHPUnit/data/class-coverage.php
Expand Up @@ -50,3 +50,10 @@ class CoversNothing extends \PHPUnit\Framework\TestCase
class CoversNotFullyQualified extends \PHPUnit\Framework\TestCase
{
}

/**
* @covers ::str_replace
*/
class CoversGlobalFunction extends \PHPUnit\Framework\TestCase
{
}

1 comment on commit 34ee324

@dionnys
Copy link

@dionnys dionnys commented on 34ee324 Apr 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Please sign in to comment.