Stop the function-name sniffs rewriting attribute names - #78
Merged
Conversation
An attribute name sits in front of a parenthesis exactly like a call does, so the five function-name sniffs matched it. An attribute happening to share a name with a function alias was reported, and phpcbf rewrote it: an attribute class Pos became current(), which does not compile. The previous-token guard did not help. It rejects T_FUNCTION, T_OBJECT_OPERATOR, T_NEW and T_DOUBLE_COLON, none of which precede an attribute name, and in a grouped attribute the second name follows a comma rather than the opener. Tokens between an attribute opener and its closer all carry the opener, so a single check in the shared lookup covers every one of them, grouped names included. This existed for plain names before fully qualified ones were added, which widened it to the leading-backslash form. The fixtures gain both.
There was a problem hiding this comment.
Pull request overview
This PR prevents the global function-name sniffs (e.g., RemoveFunctionAlias, NoIsNull, etc.) from misidentifying PHP 8 attributes as function calls and applying autofixes that rewrite attribute class names into function names, producing invalid PHP.
Changes:
- Add an early guard in the shared
AbstractSniff::getGlobalFunctionName()lookup to ignore tokens that are part of an attribute (attribute_opener). - Add fixtures to
RemoveFunctionAliastests covering both grouped and fully-qualified attribute names (#[Pos(1), Chop(2)]and#[\Join(3)]).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| tests/_data/RemoveFunctionAlias/before.php | Adds attribute examples to ensure attribute class names are not treated as function calls. |
| tests/_data/RemoveFunctionAlias/after.php | Keeps expected fixed output unchanged for attributes while still validating normal alias fixes. |
| PhpCollective/Sniffs/AbstractSniffs/AbstractSniff.php | Adds a shared attribute guard so function-name sniffs don’t rewrite attribute names. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The five function-name sniffs rewrote PHP attribute names into function calls.
Before:
Those are marked fixable, so phpcbf applied them - turning an attribute class
Posintocurrent(). The result does not compile.Why the existing guard missed it
Each sniff rejects a match whose previous non-whitespace token is
T_FUNCTION,T_OBJECT_OPERATOR,T_NEWorT_DOUBLE_COLON. An attribute name is preceded by#[, which is in none of them - and in a grouped attribute the second name follows a comma, so even addingT_ATTRIBUTEto that list would still missChopabove.Every token between an attribute opener and its closer carries
attribute_opener, including names after a comma, so one check in the shared lookup inAbstractSniffcovers all five sniffs and every position.After, with a real call added to prove nothing was over-corrected:
Scope
This predates #72 for plain names -
#[Pos(1)]was matched asT_STRINGbefore. Adding fully-qualified support widened it to#[\Join(3)]. Both forms are now in the fixtures.