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.66k stars 1.48k forks source link

Tokenizer/PHP: bug fix for match within ternary #3792

Closed jrfnl closed 1 year ago

jrfnl commented 1 year ago

This fixes the mis-identification of a T_DEFAULT token within a (not yet retokenized) match structure as a switch default token, which resulted in a T_COLON token incorrectly not being retokenized to T_INLINE_ELSE.

As a complete test file for ternary tokenization does not exist (yet), I've added a unit test to the Squiz.Objects.ObjectInstantiation sniff. The code in the test would cause a false positive for the ObjectInstantiation sniff without this fix.

Fixes #3789

schlndh commented 1 year ago

@jrfnl Thank you. I tried patching my phpcs with changes from this PR. It did fix the issue that I reported in the context where I encountered it. No new issues popped up in that project. I tried the same in a much larger project and I didn't encounter any new issues either.

jrfnl commented 1 year ago

Thank you for testing and confirming the fix @schlndh !

jrfnl commented 9 months ago

FYI: this fix is included in today's PHP_CodeSniffer 3.8.0 release.

As per #3932, development on PHP_CodeSniffer will continue in the PHPCSStandards/PHP_CodeSniffer repository. If you want to stay informed, you may want to start "watching" that repo (or watching releases from that repo).