Skip to content

Commit 40afa3d

Browse files
committed
Merge branch 'feature/psr12-classinstantiation-bug-fix-attributes-vs-anon-class' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents 5b660ae + 76faf86 commit 40afa3d

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ public function process(File $phpcsFile, $stackPtr)
6363
continue;
6464
}
6565

66+
// Skip over potential attributes for anonymous classes.
67+
if ($tokens[$i]['code'] === T_ATTRIBUTE
68+
&& isset($tokens[$i]['attribute_closer']) === true
69+
) {
70+
$i = $tokens[$i]['attribute_closer'];
71+
continue;
72+
}
73+
6674
if ($tokens[$i]['code'] === T_OPEN_SQUARE_BRACKET
6775
|| $tokens[$i]['code'] === T_OPEN_CURLY_BRACKET
6876
) {
@@ -72,7 +80,7 @@ public function process(File $phpcsFile, $stackPtr)
7280

7381
$classNameEnd = $i;
7482
break;
75-
}
83+
}//end for
7684

7785
if ($classNameEnd === null) {
7886
return;
@@ -88,6 +96,11 @@ public function process(File $phpcsFile, $stackPtr)
8896
return;
8997
}
9098

99+
if ($classNameEnd === $stackPtr) {
100+
// Failed to find the class name.
101+
return;
102+
}
103+
91104
$error = 'Parentheses must be used when instantiating a new class';
92105
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'MissingParentheses');
93106
if ($fix === true) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,9 @@ $a = new ${$varHoldingClassName};
3636
$class = new $obj?->classname();
3737
$class = new $obj?->classname;
3838
$class = new ${$obj?->classname};
39+
40+
// Issue 3456.
41+
// Anon classes should be skipped, even when there is an attribute between the new and the class keywords.
42+
$anonWithAttribute = new #[SomeAttribute('summary')] class {
43+
public const SOME_STUFF = 'foo';
44+
};

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,9 @@ $a = new ${$varHoldingClassName}();
3636
$class = new $obj?->classname();
3737
$class = new $obj?->classname();
3838
$class = new ${$obj?->classname}();
39+
40+
// Issue 3456.
41+
// Anon classes should be skipped, even when there is an attribute between the new and the class keywords.
42+
$anonWithAttribute = new #[SomeAttribute('summary')] class {
43+
public const SOME_STUFF = 'foo';
44+
};

0 commit comments

Comments
 (0)