PHP_CodeSniffer icon indicating copy to clipboard operation
PHP_CodeSniffer copied to clipboard

Tokenizer: Inconsistent `conditions` behavior for `T_DEFAULT` with/without curly braces

Open rodrigoprimo opened this issue 10 months ago • 2 comments

An inconsistency has been discovered in how the tokenizer handles conditions for T_DEFAULT statements depending on whether they use curly braces or not:

  • Without curly braces: The tokenizer sets conditions for all tokens until just before T_BREAK|T_RETURN|T_CONTINUE, which is the scope closer.
  • With curly braces: The tokenizer sets conditions until the T_BREAK|T_RETURN|T_CONTINUE token, which is not the scope closer.

This behavior was discovered while working on PR #850. It needs to be investigated whether this difference in behavior is intentional or not and if it is a bug that needs to be fixed.

This may be related to https://github.com/squizlabs/PHP_CodeSniffer/issues/3794.

Code sample

switch($var) {
    default:       // Without curly braces.
        echo "a";
        break;     // T_BREAK does NOT have `conditions`.
}

switch($var) {
    default: {     // With curly braces.
        echo "b";
        break;      // T_BREAK has `conditions`.
    }
}

Please confirm

  • [x] I have searched the issue list and am not opening a duplicate issue.
  • [x] I have read the Contribution Guidelines and this is not a support question.
  • [x] I have verified the issue still exists in the master branch of PHP_CodeSniffer.

rodrigoprimo avatar Mar 07 '25 18:03 rodrigoprimo

@rodrigoprimo Just checking, but isn't this a duplicate of #810 ?

jrfnl avatar Mar 07 '25 21:03 jrfnl

@jrfnl, I believe this issue and #810 are different issues. This one is about the tokenizer behaving differently when setting conditions for tokens depending on whether T_DEFAULT is used with or without curlies. #810 is about highlighting that one of the expectations of a tokenizer test (that the scope_condition of the scope closer points to T_DEFAULT) is invalid when T_SWITCH and T_DEFAULT share the scope closer.

They are related in that they are both about how the tokenizer handles the T_DEFAULT scope.

rodrigoprimo avatar Mar 24 '25 18:03 rodrigoprimo