Skip to content

Commit a9b6fa0

Browse files
rodrigoprimojrfnl
authored andcommitted
Generic/InlineControlStructure: stop listening for T_SWITCH tokens
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: ad96eb#diff-e1ea4eabd79d6324057bbf726a27074250478f87d92c11a3725a97f0afbd5513R50 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. Those commits will also update the related sniff tests to use different control structures other than `T_SWITCH` when appropriate. 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
1 parent 12c260f commit a9b6fa0

File tree

3 files changed

+2
-3
lines changed

3 files changed

+2
-3
lines changed

src/Standards/Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public function register()
4848
T_FOREACH,
4949
T_WHILE,
5050
T_DO,
51-
T_SWITCH,
5251
T_FOR,
5352
];
5453

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ do {
2020

2121
do i++; while (i < 5);
2222

23-
SomeClass.prototype.switch = function() {
23+
SomeClass.prototype.for = function() {
2424
// do something
2525
};
2626

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.js.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ do {
2626
do { i++;
2727
} while (i < 5);
2828

29-
SomeClass.prototype.switch = function() {
29+
SomeClass.prototype.for = function() {
3030
// do something
3131
};
3232

0 commit comments

Comments
 (0)