Skip to content

Commit

Permalink
Commenting/TestsHaveCoversTag: handle PHP 8.2 readonly classes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrfnl committed Nov 4, 2023
1 parent 6b213b6 commit 39f8b89
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
8 changes: 3 additions & 5 deletions Yoast/Sniffs/Commenting/TestsHaveCoversTagSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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-- ) {
Expand Down
17 changes: 17 additions & 0 deletions Yoast/Tests/Commenting/TestsHaveCoversTagUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
}

0 comments on commit 39f8b89

Please sign in to comment.