Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix function call detection to exclude non-function tokens #279

Merged
merged 6 commits into from
Sep 8, 2022

Conversation

sirbrillig
Copy link
Owner

@sirbrillig sirbrillig commented Sep 8, 2022

The helper function getFunctionIndexForFunctionCallArgument() (and by association isTokenInsideFunctionCallArgument()) tries to return the position of the function name token for a function call argument (eg: in the expression greet($user) it would return the token position for greet for the variable $user). It does this by finding the nearest enclosing parentheses and then looking at the non-whitespace token that comes before it. If that token does not look like a function definition, then it assumes it is a function call.

Unfortunately, this is too naive and will consider things like equals signs as function calls.

In this PR we modify the function to also reject anything not in PHPCS's list of valid function call token types and anything not in the same scope level.

Fixing this also fixed another bug, which had not been caught because one of the for loop tests was mis-written. Some variables inside for loops were being considered reads as well as writes.

Fixes #277

@sirbrillig sirbrillig merged commit aaf9022 into 2.x Sep 8, 2022
@sirbrillig sirbrillig deleted the fix/func-call-detection branch September 8, 2022 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False-positive UndefinedVariable for static variable inside an anonymous function
1 participant