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

False-positive UndefinedVariable for static variable inside an anonymous function #277

Closed
shvlv opened this issue Sep 2, 2022 · 4 comments · Fixed by #279
Closed

False-positive UndefinedVariable for static variable inside an anonymous function #277

shvlv opened this issue Sep 2, 2022 · 4 comments · Fixed by #279
Labels

Comments

@shvlv
Copy link

shvlv commented Sep 2, 2022

I stumbled upon the behavior when using the following code:

add_action('hook', static function (): void {
            static $providerId;
        });

It causes Variable $providerId is undefined. (VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable)

For my tests the minimum reproducible snippet was

$test = (function() {
            static $test;
        });

It seems round brackets do matter.

Thanks for maintaining a very useful library!

@sirbrillig sirbrillig added the bug label Sep 2, 2022
@sirbrillig
Copy link
Owner

It looks like this is happening because the code that detects if a variable is inside a list of function call arguments is incorrectly finding the static variable.

if (Helpers::isTokenInsideFunctionCallArgument($phpcsFile, $stackPtr)) {

@sirbrillig
Copy link
Owner

This should be fixed by #279

However, I wanted to ask, @shvlv: in your examples, would you expect those static variables to produce no warnings at all?

For example,

add_action('hook', static function (): void {
            static $providerId;
        });

In this code, $providerId is never used, only declared, so I think this sniff should mark it as unused (but not undefined). Is there a reason why that would be wrong? I wanted to check before I closed this issue in case I'm mistaken.

@shvlv
Copy link
Author

shvlv commented Sep 8, 2022

Thanks, @sirbrillig!

In this code, $providerId is never used, only declared, so I think this sniff should mark it as unused (but not undefined). Is there a reason why that would be wrong? I wanted to check before I closed this issue in case I'm mistaken.

Yes, there was a minimum reproducible example. So Unused warning would be pretty nice.

This should be fixed by #279

I've tested your code. It works nicely for $test = (function() { static $test;}); but snippet with add_action still triggers UndefinedVariable:

add_action('test', function () {
    static $providerId;
});

@sirbrillig
Copy link
Owner

Ohhh... thanks. I bet it's because in that case, $providerId is, lexically, inside of a function call's arguments, except that it's inside a closure and we don't account for that. I can fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants