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

findStartOfStatement doesn't work correctly inside switch #3192

Closed
VincentLanglet opened this issue Jan 3, 2021 · 0 comments · Fixed by #3186
Closed

findStartOfStatement doesn't work correctly inside switch #3192

VincentLanglet opened this issue Jan 3, 2021 · 0 comments · Fixed by #3186
Milestone

Comments

@VincentLanglet
Copy link
Contributor

Code sample

switch ($foo) {
    case 1:
        return 2;
    case 2:
        return 4;
}

Describe the bug
When working on #3186, I got a weird behavior with the scope opener/closer of switch/case. In the code sample, when I dump the tokens, : is the scope opener of the scope_condition case, and return is the scope closer.

Seems like it's because of this: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Tokenizers/PHP.php#L220

But then, when using the findStartOfStatement with the index of the token 4, it will return the token 4 instead of the return one, because of this condition: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Files/File.php#L2311-L2319
Return is a scope closer here so it return the value before return (or after, since we're looking backward), which is 4.

So I think we should ignore the scope closer of the switch/case, the right fix would be this: e790378

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

Successfully merging a pull request may close this issue.

2 participants