Skip to content

Ternaries used in constant scalar expression for param default misidentified by tokenizer #1421

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

Closed
jrfnl opened this issue Apr 11, 2017 · 2 comments

Comments

@jrfnl
Copy link
Contributor

jrfnl commented Apr 11, 2017

Since PHP 5.6 function param defaults (as well as class constants, class properties and static variables) can be initialized with constant scalar expressions, including ternaries.

However, when using such a ternary in a function declaration for the default value:

  • The ? gets misidentified as T_NULLABLE while I would expect it to be T_INLINE_THEN
  • The : gets misidentified as T_COLON while I would expect it to be T_INLINE_ELSE

Test code:

function foo($c = ((BAR)?10:100)) {}

Debug output:

        *** START LEVEL MAP ***
        Process token 0 on line 1 [col:1;len:5;lvl:0;]: T_OPEN_TAG => <?php\n
        Process token 1 on line 2 [col:1;len:0;lvl:0;]: T_WHITESPACE => \n
        Process token 2 on line 3 [col:1;len:8;lvl:0;]: T_FUNCTION => function
        Process token 3 on line 3 [col:9;len:1;lvl:0;]: T_WHITESPACE =>
        Process token 4 on line 3 [col:10;len:3;lvl:0;]: T_STRING => foo
        Process token 5 on line 3 [col:13;len:1;lvl:0;]: T_OPEN_PARENTHESIS => (
        Process token 6 on line 3 [col:14;len:2;lvl:0;]: T_VARIABLE => $c
        Process token 7 on line 3 [col:16;len:1;lvl:0;]: T_WHITESPACE =>
        Process token 8 on line 3 [col:17;len:1;lvl:0;]: T_EQUAL => =
        Process token 9 on line 3 [col:18;len:1;lvl:0;]: T_WHITESPACE =>
        Process token 10 on line 3 [col:19;len:1;lvl:0;]: T_OPEN_PARENTHESIS => (
        Process token 11 on line 3 [col:20;len:1;lvl:0;]: T_OPEN_PARENTHESIS => (
        Process token 12 on line 3 [col:21;len:3;lvl:0;]: T_STRING => BAR
        Process token 13 on line 3 [col:24;len:1;lvl:0;]: T_CLOSE_PARENTHESIS => )
        Process token 14 on line 3 [col:25;len:1;lvl:0;]: T_NULLABLE => ?
        Process token 15 on line 3 [col:26;len:2;lvl:0;]: T_LNUMBER => 10
        Process token 16 on line 3 [col:28;len:1;lvl:0;]: T_COLON => :
        Process token 17 on line 3 [col:29;len:3;lvl:0;]: T_LNUMBER => 100
        Process token 18 on line 3 [col:32;len:1;lvl:0;]: T_CLOSE_PARENTHESIS => )
        Process token 19 on line 3 [col:33;len:1;lvl:0;]: T_CLOSE_PARENTHESIS => )
        Process token 20 on line 3 [col:34;len:1;lvl:0;]: T_WHITESPACE =>
        Process token 21 on line 3 [col:35;len:1;lvl:0;]: T_OPEN_CURLY_BRACKET => {
        => Found scope opener for 2:T_FUNCTION
                * level increased *
                * token 2:T_FUNCTION added to conditions array *
                Process token 22 on line 3 [col:36;len:1;lvl:1;conds;T_FUNCTION;]: T_CLOSE_CURLY_BRA
CKET => }
                => Found scope closer for 21:T_OPEN_CURLY_BRACKET
                * token T_FUNCTION removed from conditions array *
                * level decreased *
        Process token 23 on line 3 [col:37;len:0;lvl:0;]: T_WHITESPACE => \n
        *** END LEVEL MAP ***
        *** START ADDITIONAL PHP PROCESSING ***
        *** END ADDITIONAL PHP PROCESSING ***
@gsherwood gsherwood changed the title Tokenizer bug: ternaries in constant scalar expression param default misidentified. Ternaries used in constant scalar expression for param default misidentified by tokenizer Apr 12, 2017
gsherwood added a commit that referenced this issue Apr 12, 2017
@gsherwood
Copy link
Member

I've committed a fix. Thanks for the report.

@jrfnl
Copy link
Contributor Author

jrfnl commented Apr 12, 2017

Excellent! Thanks for the quick turn-around ❤️

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

No branches or pull requests

2 participants