squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.67k stars 1.48k forks source link

PSR2/SwitchDeclaration: bug fix when determining terminating statement #3551

Closed jrfnl closed 2 years ago

jrfnl commented 2 years ago

Trailing comments within control structures nested within a switch case would break the determination of whether or not there is a terminating statement within the nested control structure.

Making the findNestedTerminator() method look for non-empty instead of non-whitespace tokens fixes that and shouldn't break the TerminatingComment check as that has it's own check whether the last token in the case statement is a comment.

Includes unit tests.

Fixes #3550

zorac commented 2 years ago

I've tested this with a number of variations on my original test case, and it works perfectly. Many thanks for the speedy patch!

jrfnl commented 2 years ago

@zorac Thanks for testing & confirming the fix!

gsherwood commented 2 years ago

Thanks for the fix