Skip to content

Commit 3441ef9

Browse files
committed
PHP 8.2 | PSR2/ClassDeclaration: allow for readonly classes
Includes unit test. Ref: * https://wiki.php.net/rfc/readonly_classes
1 parent af87457 commit 3441ef9

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function processOpen(File $phpcsFile, $stackPtr)
7171
$blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar));
7272
$spaces = strlen($blankSpace);
7373

74-
if (in_array($tokens[($stackPtr - 2)]['code'], [T_ABSTRACT, T_FINAL], true) === true
74+
if (in_array($tokens[($stackPtr - 2)]['code'], [T_ABSTRACT, T_FINAL, T_READONLY], true) === true
7575
&& $spaces !== 1
7676
) {
7777
$prevContent = strtolower($tokens[($stackPtr - 2)]['content']);
@@ -89,6 +89,7 @@ public function processOpen(File $phpcsFile, $stackPtr)
8989
}
9090
} else if ($tokens[($stackPtr - 2)]['code'] === T_ABSTRACT
9191
|| $tokens[($stackPtr - 2)]['code'] === T_FINAL
92+
|| $tokens[($stackPtr - 2)]['code'] === T_READONLY
9293
) {
9394
$prevContent = strtolower($tokens[($stackPtr - 2)]['content']);
9495
$error = 'Expected 1 space between %s and %s keywords; newline found';

src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,12 @@ C8
239239

240240
foo(new class {
241241
});
242+
243+
readonly
244+
class Test
245+
{
246+
}
247+
248+
readonly class Test
249+
{
250+
}

src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,11 @@ class C8
232232

233233
foo(new class {
234234
});
235+
236+
readonly class Test
237+
{
238+
}
239+
240+
readonly class Test
241+
{
242+
}

src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public function getErrorList()
6363
216 => 1,
6464
231 => 2,
6565
235 => 1,
66+
244 => 1,
67+
248 => 1,
6668
];
6769

6870
}//end getErrorList()

0 commit comments

Comments
 (0)