From 9e1b9de6d260461f6e99b6a8f2dbb0bbb98b579c Mon Sep 17 00:00:00 2001 From: Brad <28307684+mad-briller@users.noreply.github.com> Date: Fri, 24 Mar 2023 16:09:26 +0000 Subject: [PATCH] Warn when trying to cover an interface. --- src/Rules/PHPUnit/CoversHelper.php | 8 +++++++- tests/Rules/PHPUnit/ClassCoversExistsRuleTest.php | 4 ++++ tests/Rules/PHPUnit/data/class-coverage.php | 7 +++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Rules/PHPUnit/CoversHelper.php b/src/Rules/PHPUnit/CoversHelper.php index 66d6edf..f17a206 100644 --- a/src/Rules/PHPUnit/CoversHelper.php +++ b/src/Rules/PHPUnit/CoversHelper.php @@ -95,6 +95,13 @@ public function processCovers( if ($this->reflectionProvider->hasClass($className)) { $class = $this->reflectionProvider->getClass($className); + if ($class->isInterface()) { + $errors[] = RuleErrorBuilder::message(sprintf( + '@covers value %s references an interface.', + $fullName + ))->build(); + } + if (isset($method) && $method !== '' && !$class->hasMethod($method)) { $errors[] = RuleErrorBuilder::message(sprintf( '@covers value %s references an invalid method.', @@ -105,7 +112,6 @@ public function processCovers( 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.', diff --git a/tests/Rules/PHPUnit/ClassCoversExistsRuleTest.php b/tests/Rules/PHPUnit/ClassCoversExistsRuleTest.php index 32806ed..69a7de2 100644 --- a/tests/Rules/PHPUnit/ClassCoversExistsRuleTest.php +++ b/tests/Rules/PHPUnit/ClassCoversExistsRuleTest.php @@ -45,6 +45,10 @@ public function testRule(): void 50, 'The @covers annotation requires a fully qualified name.', ], + [ + '@covers value \DateTimeInterface references an interface.', + 64, + ], ]); } diff --git a/tests/Rules/PHPUnit/data/class-coverage.php b/tests/Rules/PHPUnit/data/class-coverage.php index 2d4e0ef..c231f77 100644 --- a/tests/Rules/PHPUnit/data/class-coverage.php +++ b/tests/Rules/PHPUnit/data/class-coverage.php @@ -57,3 +57,10 @@ class CoversNotFullyQualified extends \PHPUnit\Framework\TestCase class CoversGlobalFunction extends \PHPUnit\Framework\TestCase { } + +/** + * @covers \DateTimeInterface + */ +class CoversInterface extends \PHPUnit\Framework\TestCase +{ +}