From 61caa3a0c0bb9a34365a6cbb07333bba90e6e868 Mon Sep 17 00:00:00 2001 From: Thomas Baccelli Date: Thu, 16 May 2024 08:15:06 +0200 Subject: [PATCH] fix: `php_unit_test_class_requires_covers` Attribute detection when class is final (#8016) --- src/Fixer/PhpUnit/PhpUnitTestClassRequiresCoversFixer.php | 4 ++++ .../PhpUnit/PhpUnitTestClassRequiresCoversFixerTest.php | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/src/Fixer/PhpUnit/PhpUnitTestClassRequiresCoversFixer.php b/src/Fixer/PhpUnit/PhpUnitTestClassRequiresCoversFixer.php index b95abb75f7c..8f3e80adeb9 100644 --- a/src/Fixer/PhpUnit/PhpUnitTestClassRequiresCoversFixer.php +++ b/src/Fixer/PhpUnit/PhpUnitTestClassRequiresCoversFixer.php @@ -68,6 +68,10 @@ protected function applyPhpUnitClassFix(Tokens $tokens, int $startIndex, int $en return; // don't add `@covers` annotation for abstract base classes } + if (isset($modifiers['final'])) { + $classIndex = $tokens->getPrevMeaningfulToken($classIndex); + } + $this->ensureIsDocBlockWithAnnotation( $tokens, $classIndex, diff --git a/tests/Fixer/PhpUnit/PhpUnitTestClassRequiresCoversFixerTest.php b/tests/Fixer/PhpUnit/PhpUnitTestClassRequiresCoversFixerTest.php index 59d44dd0f93..7a733a0e5c2 100644 --- a/tests/Fixer/PhpUnit/PhpUnitTestClassRequiresCoversFixerTest.php +++ b/tests/Fixer/PhpUnit/PhpUnitTestClassRequiresCoversFixerTest.php @@ -370,6 +370,14 @@ class FooTest extends \PHPUnit_Framework_TestCase {} class FooTest extends \PHPUnit_Framework_TestCase {} PHP, ]; + + yield 'with attribute on final class' => [ + <<<'PHP' +