From d36d2ae77e27223be60cb6fc60579554cb655e96 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 14 Oct 2023 07:17:20 +0200 Subject: [PATCH] Commenting/TestsHaveCoversTag: handle PHP 8.2 readonly classes While probably rare, the sniff should still find the docblock for a test class marked as `readonly` without breaking on the `readonly` keyword. Fixed now. Includes tests. --- .../Commenting/TestsHaveCoversTagSniff.php | 8 +++----- .../Commenting/TestsHaveCoversTagUnitTest.1.inc | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/Yoast/Sniffs/Commenting/TestsHaveCoversTagSniff.php b/Yoast/Sniffs/Commenting/TestsHaveCoversTagSniff.php index 2858bb0d..8355a5d7 100644 --- a/Yoast/Sniffs/Commenting/TestsHaveCoversTagSniff.php +++ b/Yoast/Sniffs/Commenting/TestsHaveCoversTagSniff.php @@ -5,6 +5,7 @@ use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; use PHP_CodeSniffer\Util\Tokens; +use PHPCSUtils\Tokens\Collections; use PHPCSUtils\Utils\FunctionDeclarations; use PHPCSUtils\Utils\ObjectDeclarations; use PHPCSUtils\Utils\Scopes; @@ -78,11 +79,8 @@ private function process_class( File $phpcsFile, $stackPtr ) { } // @todo: Once PHPCSUtils 1.2.0 (?) is out, replace with call to new findCommentAboveOOStructure() method. - $ignore = [ - \T_WHITESPACE => \T_WHITESPACE, - \T_ABSTRACT => \T_ABSTRACT, - \T_FINAL => \T_FINAL, - ]; + $ignore = Collections::classModifierKeywords(); + $ignore[ \T_WHITESPACE ] = \T_WHITESPACE; $commentEnd = $stackPtr; for ( $commentEnd = ( $stackPtr - 1 ); $commentEnd >= 0; $commentEnd-- ) { diff --git a/Yoast/Tests/Commenting/TestsHaveCoversTagUnitTest.1.inc b/Yoast/Tests/Commenting/TestsHaveCoversTagUnitTest.1.inc index 590bd3dc..0947d7d0 100644 --- a/Yoast/Tests/Commenting/TestsHaveCoversTagUnitTest.1.inc +++ b/Yoast/Tests/Commenting/TestsHaveCoversTagUnitTest.1.inc @@ -178,3 +178,20 @@ class DoesNotHaveOurTargetTagsTest { */ function testMe() {} +/** + * Docblocks above PHP 8.2 readonly test classes should be recognized correctly. + * + * @covers \Some\Class::methodName + */ +readonly class ReadonlyTest { + public function testMe() {} +} + +/** + * Docblocks above PHP 8.2 readonly test classes should be recognized correctly. + * + * @covers \Some\Class::methodName + */ +final readonly class FinalReadonlyTest { + public function testMe() {} +}