Skip to content

Commit 87bd8d0

Browse files
committed
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. The sniff tests that were using T_SWITCH will be changed to use different control structures that trigger this sniff. This will happen either in this commit or subsequent commits depending on whether it is necessary to add related tokenizer tests or not. 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 b430e54 commit 87bd8d0

File tree

6 files changed

+25
-24
lines changed

6 files changed

+25
-24
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.inc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,16 @@ if (true)
201201
catch(Exception $e) {
202202
}
203203

204-
switch ($num) {
205-
case 0:
206-
if (1 > $num)
207-
return bar(
208-
baz(
209-
"foobarbaz"
210-
)
211-
);
212-
break;
213-
}
204+
for ($i = 0; $i <= 4; $i++)
205+
if ($i % 2)
206+
return bar(
207+
baz(
208+
"foobarbaz"
209+
)
210+
);
211+
212+
213+
214214

215215
do {
216216
$i++;

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,18 +229,19 @@ if (true) {
229229
}
230230
}
231231

232-
switch ($num) {
233-
case 0:
234-
if (1 > $num) {
235-
return bar(
236-
baz(
237-
"foobarbaz"
238-
)
239-
);
240-
}
241-
break;
232+
for ($i = 0; $i <= 4; $i++) {
233+
if ($i % 2) {
234+
return bar(
235+
baz(
236+
"foobarbaz"
237+
)
238+
);
239+
}
242240
}
243241

242+
243+
244+
244245
do {
245246
$i++;
246247
}

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.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.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

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public function getErrorList($testFile='')
6969
191 => 1,
7070
195 => 1,
7171
198 => 1,
72-
206 => 1,
72+
204 => 1,
73+
205 => 1,
7374
222 => 1,
7475
232 => 1,
7576
235 => 1,

0 commit comments

Comments
 (0)