Skip to content

Commit cbfa258

Browse files
committed
Tests/Tokenizer: test related to setting the scope for T_CASE
This commit copies a sniff test from InlineControlStructureUnitTest.php to the Tokenizer::recurseScopeMap() tests for the `case` keyword. The copied test was added in 65ef053 before the Tokenizer tests were created. It ensures that the scope for the `T_CASE` token is correctly set when there is an inline control structure inside it with more than three lines. The original sniff test was modified to use `T_FOR` instead of `T_SWITCH`, but its purpose was not altered. The test is about adding braces to an `if` that returns a nested function call.
1 parent a7cd997 commit cbfa258

File tree

5 files changed

+43
-21
lines changed

5 files changed

+43
-21
lines changed

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
@@ -230,18 +230,19 @@ if (true) {
230230
}
231231
}
232232

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

243+
244+
245+
245246
do {
246247
$i++;
247248
}

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

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

tests/Core/Tokenizers/Tokenizer/RecurseScopeMapCaseKeywordConditionsTest.inc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,16 @@ switch ($type) {
117117
}
118118
break;
119119
}
120+
121+
// Test for https://github.com/squizlabs/PHP_CodeSniffer/issues/1590
122+
switch ($num) {
123+
/* testSwitchCaseNestedInlineIfWithMoreThanThreeLines */
124+
case 0:
125+
if (1 > $num)
126+
return bar(
127+
baz(
128+
"foobarbaz"
129+
)
130+
);
131+
break;
132+
}

tests/Core/Tokenizers/Tokenizer/RecurseScopeMapCaseKeywordConditionsTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ public static function dataNotEnumCases()
208208
'scope_closer' => T_BREAK,
209209
],
210210
],
211+
'switch case, nested inline if' => [
212+
'testMarker' => '/* testSwitchCaseNestedInlineIfWithMoreThanThreeLines */',
213+
'expectedTokens' => [
214+
'scope_opener' => T_COLON,
215+
'scope_closer' => T_BREAK,
216+
],
217+
],
211218
];
212219

213220
}//end dataNotEnumCases()

0 commit comments

Comments
 (0)