diff --git a/PhpCollective/Sniffs/AbstractSniffs/AbstractSniff.php b/PhpCollective/Sniffs/AbstractSniffs/AbstractSniff.php index ea08484..e2f79de 100644 --- a/PhpCollective/Sniffs/AbstractSniffs/AbstractSniff.php +++ b/PhpCollective/Sniffs/AbstractSniffs/AbstractSniff.php @@ -67,6 +67,13 @@ protected function getGlobalFunctionName(File $phpcsFile, int $stackPtr): ?strin { $tokens = $phpcsFile->getTokens(); + // An attribute name sits in front of a parenthesis just like a call does, but it names a + // class. Every token between `#[` and its closer carries the opener, including grouped + // attributes after a comma. + if (isset($tokens[$stackPtr]['attribute_opener'])) { + return null; + } + if ($tokens[$stackPtr]['code'] === T_STRING) { return strtolower($tokens[$stackPtr]['content']); } diff --git a/tests/_data/RemoveFunctionAlias/after.php b/tests/_data/RemoveFunctionAlias/after.php index 33ca232..d5116eb 100644 --- a/tests/_data/RemoveFunctionAlias/after.php +++ b/tests/_data/RemoveFunctionAlias/after.php @@ -4,6 +4,13 @@ class FixMe { + + /** + * Attribute names sit in front of a parenthesis but name a class, not a function. + */ + #[Pos(1), Chop(2)] + #[\Join(3)] + public int $attributed = 1; /** * Type check aliases. */ diff --git a/tests/_data/RemoveFunctionAlias/before.php b/tests/_data/RemoveFunctionAlias/before.php index b0b7a61..11c8642 100644 --- a/tests/_data/RemoveFunctionAlias/before.php +++ b/tests/_data/RemoveFunctionAlias/before.php @@ -4,6 +4,13 @@ class FixMe { + + /** + * Attribute names sit in front of a parenthesis but name a class, not a function. + */ + #[Pos(1), Chop(2)] + #[\Join(3)] + public int $attributed = 1; /** * Type check aliases. */