Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 3.7.2

Bug #20093 Bug with ternary operator token
Submitted: 2013-10-13 01:12 UTC
From: euromark Assigned: squiz
Status: Closed Package: PHP_CodeSniffer (version 1.5.0RC4)
PHP Version: 5.4.3 OS: WIN
Roadmaps: (Not assigned)    
Subscription  


 [2013-10-13 01:12 UTC] euromark (Mark Scherer)
Description: ------------ Example: $x = $f ? false : true; Usually, the ternary operator returns these tokens to work with: ... T_WHITESPACE T_VARIABLE T_WHITESPACE T_EQUAL T_WHITESPACE T_VARIABLE T_WHITESPACE T_INLINE_THEN T_WHITESPACE T_FALSE T_WHITESPACE T_INLINE_ELSE T_WHITESPACE T_TRUE T_SEMICOLON T_WHITESPACE But with $x = $f?false:true; it suddenly is (note that this is the exact same meaning in PHP, only the spacing is reduced): ... T_WHITESPACE T_VARIABLE T_WHITESPACE T_EQUAL T_WHITESPACE T_VARIABLE T_INLINE_THEN T_GOTO_LABEL T_TRUE T_SEMICOLON T_WHITESPACE Now a "T_GOTO_LABEL" apears here, making it impossible to use swallowed T_FALSE that should have been there after T_INLINE_THEN. Test script: --------------- We just used $tokens = $phpcsFile->getTokens(); Expected result: ---------------- With $x = $f?false:true; one expects ... T_WHITESPACE T_VARIABLE T_WHITESPACE T_EQUAL T_WHITESPACE T_VARIABLE T_INLINE_THEN T_FALSE T_INLINE_ELSE T_TRUE T_SEMICOLON T_WHITESPACE Actual result: -------------- With $x = $f?false:true; it suddenly is (note that this is the exact same meaning in PHP, only the spacing is reduced): ... T_WHITESPACE T_VARIABLE T_WHITESPACE T_EQUAL T_WHITESPACE T_VARIABLE T_INLINE_THEN T_GOTO_LABEL T_TRUE T_SEMICOLON T_WHITESPACE Now a "T_GOTO_LABEL" apears here, making it impossible to use swallowed T_FALSE that should have been there after T_INLINE_THEN.

Comments

 [2013-10-13 01:26 UTC] euromark (Mark Scherer)
This is only happening for phpcs-fixer, of course, as this has only been introduced there.
 [2013-10-13 01:31 UTC] euromark (Mark Scherer)
 [2013-10-23 07:14 UTC] squiz (Greg Sherwood)
-Status: Open +Status: Closed -Assigned To: +Assigned To: squiz
I ended up fixing it by putting a specific case in: https://github.com/squizlabs/PHP_CodeSniffer/commit/28d5a293d661bb01092905562 3e17a7db3a84f41 If you find other bits of code this doesn't work with, please reopen this report.
 [2013-10-23 07:22 UTC] euromark (Mark Scherer)
Thank you. Seems to work just fine. Will this also be merged into phpcs branch (where I actually found this issue when writing a fixer for ternary)?
 [2013-10-23 07:27 UTC] squiz (Greg Sherwood)
Yep, it will be merged in. I'm just doing a few more changes at the moment.