-
-
Notifications
You must be signed in to change notification settings - Fork 55
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
Generic/InlineControlStructure: stop listening for T_SWITCH #595
Open
rodrigoprimo
wants to merge
7
commits into
PHPCSStandards:master
Choose a base branch
from
rodrigoprimo:inline-control-strutucture-remove-switch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Generic/InlineControlStructure: stop listening for T_SWITCH #595
rodrigoprimo
wants to merge
7
commits into
PHPCSStandards:master
from
rodrigoprimo:inline-control-strutucture-remove-switch
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
There is no inline version of a `switch` in PHP and JS so there is no reason for this sniff to listen to `T_SWITCH` tokens. Before this change, the sniff would bail early on the line below as a switch always has a scope opener, so this change should not impact in any way the behavior of the sniff. https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/9a0c2546ea2fa7aac19881da7b655cc5f022bc10/src/Standards/Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php#L71 The InlineControlStructure sniff started listening for T_SWITCH tokens since the commit that added it to PHPCS: PHPCSStandards@ad96ebb#diff-e1ea4eabd79d6324057bbf726a27074250478f87d92c11a3725a97f0afbd5513R50 The sniff tests that were using T_SWITCH were changed to use different control structures that trigger this sniff or were removed when not necessary anymore. Some of the sniff tests using the T_SWITCH token were added to protect against regressions for changes in the tokenizer. For those, dedicated tokenizer tests will be created in subsequent commits. The modified JS test 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 than T_SWITCH. This test was originally added in b3f4f83. References: - PHP: https://www.php.net/manual/en/control-structures.switch.php - JS: https://tc39.es/ecma262/multipage/ecmascript-language-statements-and-declarations.html
This commit adds tests to safeguard against regressions related to b24b96b which was added to fix one of the issues in squizlabs/PHP_CodeSniffer#497. b24b96b originally added a test to a sniff test (InlineControlStructureUnitTest.php) as back then there were no Tokenizer tests. This test was added to ensure that Tokenizer::recurseScopeMap() would correctly add scope information to the T_SWITCH token when handling a `switch` that uses the alternative syntax. Now that the InlineControlStructure sniff doesn't listen for the T_SWITCH token anymore, the original test added in b24b96b became useless and thus was refactored in a previous commit to use different control structure.
This commit adds tests to safeguard against regressions related to fddc61a which is part of squizlabs/PHP_CodeSniffer#497. fddc61a originally added a test to a sniff test (InlineControlStructureUnitTest.php) as back then there were no Tokenizer tests. Those tests were added to ensure that Tokenizer::recurseScopeMap() would correctly add scope information to the `T_CASE` token when it shares the closer with `T_SWITCH` and to `T_IF` when a nested `T_CASE` shares the scope closer with `T_SWITCH`. Now that the InlineControlStructure sniff doesn't listen for the T_SWITCH token anymore, the original test added in fddc61a became useless and thus was refactored in a previous commit to use different control structure.
This commit adds a test to ensure that the scope opener is set correctly for T_SWITCH when there is a closure in the condition. This should safeguard against regressions related to 30c618e which fixed squizlabs/PHP_CodeSniffer#543. 30c618e originally added a test to InlineControlStructureUnitTest.php as back then there were no Tokenizer tests. Now that the InlineControlStructure sniff doesn't listen for the T_SWITCH token anymore, the original test added in 30c618e became useless and was removed in a previous commit.
This commit expands the existing `Tokenizer::recurseScopeMap()` tests for the case keyword when used in a switch statement to check the value of the `scope_condition`, `scope_opener` and `scope_closer` indexes besides checking if they are set. This will be needed for a new test case that will be added in a subsequent commit.
This commit copies a sniff test from InlineControlStructureUnitTest.php to the Tokenizer::recurseScopeMap() tests for the case keyword. This test was added in b498dbe before the Tokenizer tests were created. It ensures that the scope for the T_CASE token is set correctly when there is a if/elseif/else with and without braces inside it. Note that this test currently does not fail even when the changes to the tokenizer applied in b498dbe are reverted. I'm assuming that a subsequent commit further improved the tokenizer and made the changes from b498dbe redundant, but I was not able to find the specific commit using git bissect. That being said, I was able to confirm that before b498dbe, the scope closer for the T_CASE token was incorrectly set to the T_RETURN token instead of T_BREAK.
This commit copies a sniff test from InlineControlStructureUnitTest.php to the Tokenizer::recurseScopeMap() tests for the case keyword. This test was added in 65ef053 before the Tokenizer tests were created. It ensures that the scope for the T_CASE token is set correctly when there is a inline control structure inside it with more than three lines.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
As originally discussed in #482 (review) (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 #482 (review).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 set 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 as I believe it is still valid and 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 inPHPCS 4.0, I opted to simply use a control structure other than
T_SWITCH
. This test was originally added in b3f4f83.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 #482 (review)
Types of changes
PR checklist