Open rodrigoprimo opened 2 months ago
@jrfnl, as we discussed last week, I reorganized the commits in this PR so that the changes to the sniff tests are not all in the commit that changes the InlineControlStructure sniff to stop listening for T_SWITCH
. Instead, each change to the sniff tests is in the commit that creates the related tokenizer test.
This PR is now ready to be reviewed.
Description
As originally discussed in https://github.com/PHPCSStandards/PHP_CodeSniffer/pull/482#pullrequestreview-2062765716 (see the "Commit 2 - stop listening for T_SWITCH" section), this PR changes the
Generic.ControlStructures.InlineControlStructure
sniff to stop listening forT_SWITCH
tokens as there is no inline version of aswitch
in PHP and JS.Before this change, the sniff would bail early for a
T_SWITCH
token either because it has a scope opener or because of a syntax error.Code samples used for the sniff tests that contained a
T_SWITCH
token were updated to reflect this change. Some of those were actually added as Tokenizer tests before PHPCS had dedicated Tokenizer tests. Below there is more information about each of the modified tests answering the questions left by @jrfnl in https://github.com/PHPCSStandards/PHP_CodeSniffer/pull/482#pullrequestreview-2062765716.I updated the original code sample used in the sniff test to use
T_IF
andT_ELSEIF
instead ofT_SWITCH
. I also created a new Tokenizer::recurseScopeMap() test file and added tests to ensure that the scope indexes are set correctly for switches using the normal and alternative syntaxes.Similar to the above, I updated the code sample to use
T_FOREACH
instead ofT_SWITCH
and created dedicated Tokenizer tests to check setting the scope forT_IF
with a nestedT_SWITCH
andT_CASE
whenT_CASE
is missingT_BREAK
.I replaced the code sample with an inline
T_IF
with a closure within the condition. Also, I added a dedicated Tokenizer test to ensure that the scope for aT_SWITCH
is set correctly when there is a closure within the condition.I left the code sample as is because I believe it is still valid. The original problem only manifested when the
T_IF
was nested within aT_SWITCH
. A dedicated tokenizer test was added using the same code sample.I replaced the
T_SWITCH
in the original code sample with aT_FOR
and created a dedicated Tokenizer test.I did replace
switch
withfor
. But it is important to note that the original code sample lost part of its value over time as now the sniff bails early if there is no opening parenthesis after the control structure. Ideally, it should be moved to a tokenizer test, but since there are no tests for the JS tokenizer, and support will be dropped in PHPCS 4.0, I opted to simply use a control structure other thanT_SWITCH
. This test was originally added in https://github.com/PHPCSStandards/PHP_CodeSniffer/commit/b3f4f83b2c1ad1a07086a8af09cb371fb0f4b1a4.Suggested changelog entry
Generic.ControlStructures.InlineControlStructure
stop listening for T_SWITCH as there is no inline version of this control structure in PHP and JSRelated issues/external references
First discussed in https://github.com/PHPCSStandards/PHP_CodeSniffer/pull/482#pullrequestreview-2062765716
Types of changes
PR checklist