Skip to content

Commit

Permalink
PHP 8.2 | PSR2/ClassDeclaration: allow for readonly classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfnl committed May 18, 2023
1 parent af87457 commit 3441ef9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function processOpen(File $phpcsFile, $stackPtr)
$blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar));
$spaces = strlen($blankSpace);

if (in_array($tokens[($stackPtr - 2)]['code'], [T_ABSTRACT, T_FINAL], true) === true
if (in_array($tokens[($stackPtr - 2)]['code'], [T_ABSTRACT, T_FINAL, T_READONLY], true) === true
&& $spaces !== 1
) {
$prevContent = strtolower($tokens[($stackPtr - 2)]['content']);
Expand All @@ -89,6 +89,7 @@ public function processOpen(File $phpcsFile, $stackPtr)
}
} else if ($tokens[($stackPtr - 2)]['code'] === T_ABSTRACT
|| $tokens[($stackPtr - 2)]['code'] === T_FINAL
|| $tokens[($stackPtr - 2)]['code'] === T_READONLY
) {
$prevContent = strtolower($tokens[($stackPtr - 2)]['content']);
$error = 'Expected 1 space between %s and %s keywords; newline found';
Expand Down
9 changes: 9 additions & 0 deletions src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,12 @@ C8

foo(new class {
});

readonly
class Test
{
}

readonly class Test
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,11 @@ class C8

foo(new class {
});

readonly class Test
{
}

readonly class Test
{
}
2 changes: 2 additions & 0 deletions src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public function getErrorList()
216 => 1,
231 => 2,
235 => 1,
244 => 1,
248 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit 3441ef9

Please sign in to comment.