Skip to content

Commit 10249a2

Browse files
authored
Merge pull request #324 from PHPCSStandards/php-8.3/psr12-classinstantiation-allow-for-readonly-anon-classes
PHP 8.3 | PSR12/ClassInstantiation: allow for readonly anonymous classes
2 parents 675d8f3 + 5fd4059 commit 10249a2

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

src/Standards/PSR12/Sniffs/Classes/ClassInstantiationSniff.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ public function process(File $phpcsFile, $stackPtr)
6464
continue;
6565
}
6666

67-
// Skip over potential attributes for anonymous classes.
67+
// Bow out when this is an anonymous class.
68+
// Anonymous classes are the only situation which would allow for an attribute
69+
// or for the readonly keyword between "new" and the class "name".
6870
if ($tokens[$i]['code'] === T_ATTRIBUTE
69-
&& isset($tokens[$i]['attribute_closer']) === true
71+
|| $tokens[$i]['code'] === T_READONLY
72+
|| $tokens[$i]['code'] === T_ANON_CLASS
7073
) {
71-
$i = $tokens[$i]['attribute_closer'];
72-
continue;
74+
return;
7375
}
7476

7577
if ($tokens[$i]['code'] === T_OPEN_SQUARE_BRACKET
@@ -87,11 +89,6 @@ public function process(File $phpcsFile, $stackPtr)
8789
return;
8890
}
8991

90-
if ($tokens[$classNameEnd]['code'] === T_ANON_CLASS) {
91-
// Ignore anon classes.
92-
return;
93-
}
94-
9592
if ($tokens[$classNameEnd]['code'] === T_OPEN_PARENTHESIS) {
9693
// Using parenthesis.
9794
return;

src/Standards/PSR12/Tests/Classes/ClassInstantiationUnitTest.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ $anonWithAttribute = new #[SomeAttribute('summary')] class {
4545

4646
$foo = new parent();
4747
$foo = new parent;
48+
49+
// PHP 8.3: safeguard that the sniff ignores anonymous classes, even when declared as readonly.
50+
$anon = new readonly class {};
51+
$anon = new #[MyAttribute] readonly class {};

src/Standards/PSR12/Tests/Classes/ClassInstantiationUnitTest.inc.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ $anonWithAttribute = new #[SomeAttribute('summary')] class {
4545

4646
$foo = new parent();
4747
$foo = new parent();
48+
49+
// PHP 8.3: safeguard that the sniff ignores anonymous classes, even when declared as readonly.
50+
$anon = new readonly class {};
51+
$anon = new #[MyAttribute] readonly class {};

0 commit comments

Comments
 (0)