Skip to content

Commit 3d4fe0c

Browse files
committed
SlevomatCodingStandard.ControlStructures.NewWithParentheses: Fixed false positives for anonymous class with attributes
1 parent 0177637 commit 3d4fe0c

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

SlevomatCodingStandard/Helpers/AttributeHelper.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use InvalidArgumentException;
66
use PHP_CodeSniffer\Files\File;
77
use function sprintf;
8+
use const T_ANON_CLASS;
89
use const T_ATTRIBUTE;
910
use const T_CLASS;
1011
use const T_CLOSURE;
@@ -26,6 +27,7 @@ class AttributeHelper
2627
{
2728

2829
private const ATTRIBUTE_TARGETS = [
30+
T_ANON_CLASS,
2931
T_CLASS,
3032
T_CLOSURE,
3133
T_CONST,

SlevomatCodingStandard/Sniffs/ControlStructures/NewWithParenthesesSniff.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use PHP_CodeSniffer\Files\File;
66
use PHP_CodeSniffer\Sniffs\Sniff;
7+
use SlevomatCodingStandard\Helpers\AttributeHelper;
78
use SlevomatCodingStandard\Helpers\TokenHelper;
89
use const T_ANON_CLASS;
10+
use const T_ATTRIBUTE;
911
use const T_CLOSE_PARENTHESIS;
1012
use const T_CLOSE_SHORT_ARRAY;
1113
use const T_CLOSE_SQUARE_BRACKET;
@@ -43,6 +45,10 @@ public function process(File $phpcsFile, $newPointer): void
4345
/** @var int $nextPointer */
4446
$nextPointer = TokenHelper::findNextEffective($phpcsFile, $newPointer + 1);
4547

48+
if ($tokens[$nextPointer]['code'] === T_ATTRIBUTE) {
49+
$nextPointer = AttributeHelper::getAttributeTarget($phpcsFile, $nextPointer);
50+
}
51+
4652
if ($tokens[$nextPointer]['code'] === T_ANON_CLASS) {
4753
return;
4854
}

tests/Sniffs/ControlStructures/data/newWithParenthesesNoErrors.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ public static function getInstance()
3232

3333
};
3434

35+
new
36+
#[AllowDynamicProperties]
37+
#[Attribute1]
38+
class
39+
{
40+
};
41+
3542
$whitespaceBetweenClassNameAndParentheses = new stdClass ();
3643

3744
new $a->{'b'}["c"]->$d['e'][1]();

0 commit comments

Comments
 (0)