Skip to content

Commit

Permalink
PHP 8.2 | Squiz/ClassComment: allow for readonly classes
Browse files Browse the repository at this point in the history
Includes improving the accuracy of the tokens to skip over.

Includes unit test.

Ref:
* https://wiki.php.net/rfc/readonly_classes
  • Loading branch information
jrfnl committed May 18, 2023
1 parent 358c476 commit 81f322e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
9 changes: 6 additions & 3 deletions src/Standards/Squiz/Sniffs/Commenting/ClassCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;

class ClassCommentSniff implements Sniff
{
Expand Down Expand Up @@ -49,8 +48,12 @@ public function register()
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$find = Tokens::$methodPrefixes;
$find[T_WHITESPACE] = T_WHITESPACE;
$find = [
T_ABSTRACT => T_ABSTRACT,
T_FINAL => T_FINAL,
T_READONLY => T_READONLY,
T_WHITESPACE => T_WHITESPACE,
];

$previousContent = null;
for ($commentEnd = ($stackPtr - 1); $commentEnd >= 0; $commentEnd--) {
Expand Down
12 changes: 12 additions & 0 deletions src/Standards/Squiz/Tests/Commenting/ClassCommentUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,15 @@ class Space_At_end
class AllGood
{
}

/**
* Docblock
*/
abstract readonly class AbstractReadonlyWithDocblock {}

/*
* Docblock
*/
readonly class ReadonlyWrongStyle {}

readonly final class ReadonlyFinalWithoutDocblock {}
10 changes: 6 additions & 4 deletions src/Standards/Squiz/Tests/Commenting/ClassCommentUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ class ClassCommentUnitTest extends AbstractSniffUnitTest
public function getErrorList()
{
return [
2 => 1,
15 => 1,
31 => 1,
54 => 1,
2 => 1,
15 => 1,
31 => 1,
54 => 1,
143 => 1,
145 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit 81f322e

Please sign in to comment.