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

Ternary operator inside arrow function #297

Closed
tomrajnoha opened this issue Mar 31, 2023 · 3 comments · Fixed by #298
Closed

Ternary operator inside arrow function #297

tomrajnoha opened this issue Mar 31, 2023 · 3 comments · Fixed by #298
Labels

Comments

@tomrajnoha
Copy link

Consider the below MRE, which was OK for phpcs-variable-analysis v2.11.12, but receives a warning in v2.11.14 for the part of the code on the last line (between question mark and colon).

Warning: Variable $cls is undefined. (VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable)

Note that PHP (8.0) has no issue with the code and it works as expected. As far as I can tell, it might be connected to PR #296.

class Test
{
    public function status(): bool
    {
        return true;
    }

    public static function hello(callable $callback): void
    {
        print('Hello');
    }
}

Test::hello(fn(Test $cls) => $cls->status() ? $cls : null);

This workaround can fix it for now.

class Test
{
    public function status(): bool
    {
        return true;
    }

    public static function hello(callable $callback): void
    {
        print('Hello');
    }
}

Test::hello(static function (Test $cls): ?Test {
    return $cls->status() ? $cls : null;
});
@sirbrillig
Copy link
Owner

Hm... yes, it looks like the scope detection for arrow functions is still buggy. Even before #296, it would have broken for versions of PHP/phpcs that did not support arrow functions, so in a way this is good to discover.

@tomrajnoha
Copy link
Author

Amazing, thank you very much for this quick fix. I will check later if I can be of some help dealing with these detections.

@sirbrillig
Copy link
Owner

This should be fixed now but if you discover any more arrow functions behaving weird, please let me know!

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