Generic/UpperCaseConstantName: skip when local define() is in scope#1412
Closed
dereuromark wants to merge 2 commits intoPHPCSStandards:4.xfrom
Closed
Generic/UpperCaseConstantName: skip when local define() is in scope#1412dereuromark wants to merge 2 commits intoPHPCSStandards:4.xfrom
dereuromark wants to merge 2 commits intoPHPCSStandards:4.xfrom
Conversation
When a file declares a top-level `function define(...)` or imports one via `use function ...\define;` (including group use and `as` aliases), unqualified `define(...)` calls may resolve to that function instead of the global one. The sniff should bow out in those cases. Fully qualified `\define(...)` calls remain checked as before because they tokenize as T_NAME_FULLY_QUALIFIED and unambiguously refer to the global function. Fixes PHPCSStandards#733
Member
|
@dereuromark Did you use AI for this PR ? |
Author
|
I managed to fix this with own sniff for now. Feel free to reuse if it is deemed useful. |
Member
|
@dereuromark That doesn't answer the question I asked ? |
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.
Summary
Fixes #733.
When a file declares a top-level
function define(...)or imports one viause function ...\define;(incl. group use andasaliases), unqualifieddefine(...)calls may resolve to that local function instead of the global one. The sniff currently flags lowercase constant names in those calls as a false positive. With this change, the sniff bows out for unqualifieddefine(...)calls in such files.Fully qualified
\define(...)calls (T_NAME_FULLY_QUALIFIED) remain checked as before, since they unambiguously refer to the global function.Repro
The issue lists several variants. On modern 4.x most of them are already silently handled because qualified names tokenize as
T_NAME_QUALIFIED/T_NAME_RELATIVE, which the sniff does not register. The remaining real false positive is the baredefine(...)case:Approach
Added a private helper
fileHasCustomDefine()that scans the file once (cached per file path via astaticvariable inside the method) for either:function define(...)declaration (skips class methods viaconditions).use function ...\define;import — including group use (use function Foo\{bar, define};) and aliases. Aliases todefinecount (use function Foo\bar as define;); aliases away fromdefinedo not (use function Foo\define as something;).In
process(), when the visited token is a bareT_STRINGfordefineand the helper returns true, the sniff returns early.Why a
staticcache and not an instance propertytests/Core/Ruleset/PopulateTokenListenersTest::testDoesntTriggerPropertySettingForNoPropertiesuses this exact sniff class as the canonical "sniff with no declared properties" sample. Adding an instance property breaks that meta test. Astaticvariable inside the helper method gives the same per-file caching without changing the sniff's reflection footprint. Happy to switch to an instance property + meta-test update if maintainers prefer.Test changes
function define(\$param) {}declaration fromUpperCaseConstantNameUnitTest.1.inc. With the fix, that declaration would correctly cause the sniff to bow out for the entire file, invalidating the rest of the fixture's expectations. Moved a comparable scenario into a dedicated new fixture instead.UpperCaseConstantNameUnitTest.6.inc— localfunction define()declared in a namespace; only the fully qualified\define()call with a lowercase name is flagged.UpperCaseConstantNameUnitTest.7.inc—use function Bar\define;and a groupuse function Bar\{otherFunc, define as renamedDefine};; only the fully qualified\define()call is flagged.UpperCaseConstantNameUnitTest.8.inc—use function Bar\define as something;aliases away fromdefine, so the localdefinesymbol is unaffected and the unqualified call is still flagged.UpperCaseConstantNameUnitTest.phpto register the three new fixtures and adjust one shifted line number in1.inc(94 → 91, the result of removing two lines from1.inc).Verification
phpcson the modified sniff: clean.Notes for reviewers