Skip to content

Commit

Permalink
Tests/Tokenizer: test related to setting the scope for T_CASE
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rodrigoprimo committed Oct 9, 2024
1 parent 19199df commit 356f8bf
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,16 @@ if (true)
catch(Exception $e) {
}

switch ($num) {
case 0:
if (1 > $num)
return bar(
baz(
"foobarbaz"
)
);
break;
}
for ($i = 0; $i <= 4; $i++)
if ($i % 2)
return bar(
baz(
"foobarbaz"
)
);




do {
$i++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,19 @@ if (true) {
}
}

switch ($num) {
case 0:
if (1 > $num) {
return bar(
baz(
"foobarbaz"
)
);
}
break;
for ($i = 0; $i <= 4; $i++) {
if ($i % 2) {
return bar(
baz(
"foobarbaz"
)
);
}
}




do {
$i++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public function getErrorList($testFile='')
191 => 1,
195 => 1,
198 => 1,
206 => 1,
204 => 1,
205 => 1,
222 => 1,
232 => 1,
235 => 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,16 @@ switch ($type) {
}
break;
}

// Test for https://github.com/squizlabs/PHP_CodeSniffer/issues/1590
switch ($num) {
/* testSwitchCaseNestedInlineIfWithMoreThanThreeLines */
case 0:
if (1 > $num)
return bar(
baz(
"foobarbaz"
)
);
break;
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ public static function dataNotEnumCases()
'scope_closer' => T_BREAK,
],
],
'switch case, nested inline if' => [
'testMarker' => '/* testSwitchCaseNestedInlineIfWithMoreThanThreeLines */',
'expectedTokens' => [
'scope_opener' => T_COLON,
'scope_closer' => T_BREAK,
],
],
];

}//end dataNotEnumCases()
Expand Down

0 comments on commit 356f8bf

Please sign in to comment.