Skip to content

Commit

Permalink
SlevomatCodingStandard.ControlStructures.NewWithParentheses: Fixed fa…
Browse files Browse the repository at this point in the history
…lse positives for anonymous class with attributes
  • Loading branch information
kukulich committed Mar 27, 2023
1 parent 0177637 commit 3d4fe0c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions SlevomatCodingStandard/Helpers/AttributeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use InvalidArgumentException;
use PHP_CodeSniffer\Files\File;
use function sprintf;
use const T_ANON_CLASS;
use const T_ATTRIBUTE;
use const T_CLASS;
use const T_CLOSURE;
Expand All @@ -26,6 +27,7 @@ class AttributeHelper
{

private const ATTRIBUTE_TARGETS = [
T_ANON_CLASS,
T_CLASS,
T_CLOSURE,
T_CONST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\AttributeHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use const T_ANON_CLASS;
use const T_ATTRIBUTE;
use const T_CLOSE_PARENTHESIS;
use const T_CLOSE_SHORT_ARRAY;
use const T_CLOSE_SQUARE_BRACKET;
Expand Down Expand Up @@ -43,6 +45,10 @@ public function process(File $phpcsFile, $newPointer): void
/** @var int $nextPointer */
$nextPointer = TokenHelper::findNextEffective($phpcsFile, $newPointer + 1);

if ($tokens[$nextPointer]['code'] === T_ATTRIBUTE) {
$nextPointer = AttributeHelper::getAttributeTarget($phpcsFile, $nextPointer);
}

if ($tokens[$nextPointer]['code'] === T_ANON_CLASS) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ public static function getInstance()

};

new
#[AllowDynamicProperties]
#[Attribute1]
class
{
};

$whitespaceBetweenClassNameAndParentheses = new stdClass ();

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

0 comments on commit 3d4fe0c

Please sign in to comment.