Skip to content

Commit

Permalink
Tests/Tokenizer: test to ensure scope closer is set correctly 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. This
test was added in b498dbe before the Tokenizer tests were created. It
ensures that the scope for the `T_CASE` token is correctly set when
there is an 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
`T_RETURN` instead of `T_BREAK`.

The original sniff test was kept without any modification as testing
if/elseif/else with and without curly braces inside another control
structure is still valid.
  • Loading branch information
rodrigoprimo committed Oct 9, 2024
1 parent 180ed7f commit c37e583
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,17 @@ switch ($value):
case 1:
echo 'one';
endswitch;

// Test for https://github.com/squizlabs/PHP_CodeSniffer/issues/879
switch ($type) {
/* testSwitchCaseNestedIfWithAndWithoutBraces */
case 1:
if ($foo) {
return true;
} elseif ($baz)
return true;
else {
echo 'else';
}
break;
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,68 +139,75 @@ public function testNotEnumCases($testMarker, $expectedTokens, $testCloserMarker
public static function dataNotEnumCases()
{
return [
'switch case with constant, semicolon condition end' => [
'switch case with constant, semicolon condition end' => [
'testMarker' => '/* testCaseWithSemicolonIsNotEnumCase */',
'expectedTokens' => [
'scope_opener' => T_SEMICOLON,
'scope_closer' => T_CLOSE_CURLY_BRACKET,
],
],
'switch case with constant, colon condition end' => [
'switch case with constant, colon condition end' => [
'testMarker' => '/* testCaseWithConstantIsNotEnumCase */',
'expectedTokens' => [
'scope_opener' => T_COLON,
'scope_closer' => T_CLOSE_CURLY_BRACKET,
],
'testCloserMarker' => '/* testCaseConstantCloserMarker */',
],
'switch case with constant, comparison' => [
'switch case with constant, comparison' => [
'testMarker' => '/* testCaseWithConstantAndIdenticalIsNotEnumCase */',
'expectedTokens' => [
'scope_opener' => T_COLON,
'scope_closer' => T_CLOSE_CURLY_BRACKET,
],
'testCloserMarker' => '/* testCaseConstantCloserMarker */',
],
'switch case with constant, assignment' => [
'switch case with constant, assignment' => [
'testMarker' => '/* testCaseWithAssigmentToConstantIsNotEnumCase */',
'expectedTokens' => [
'scope_opener' => T_COLON,
'scope_closer' => T_CLOSE_CURLY_BRACKET,
],
'testCloserMarker' => '/* testCaseConstantCloserMarker */',
],
'switch case with constant, keyword in mixed case' => [
'switch case with constant, keyword in mixed case' => [
'testMarker' => '/* testIsNotEnumCaseIsCaseInsensitive */',
'expectedTokens' => [
'scope_opener' => T_COLON,
'scope_closer' => T_CLOSE_CURLY_BRACKET,
],
'testCloserMarker' => '/* testCaseConstantCloserMarker */',
],
'switch case, body in curlies declares enum' => [
'switch case, body in curlies declares enum' => [
'testMarker' => '/* testCaseInSwitchWhenCreatingEnumInSwitch1 */',
'expectedTokens' => [
'scope_opener' => T_OPEN_CURLY_BRACKET,
'scope_closer' => T_CLOSE_CURLY_BRACKET,
],
'testCloserMarker' => '/* testCaseInSwitchWhenCreatingEnumInSwitch1CloserMarker */',
],
'switch case, body after semicolon declares enum' => [
'switch case, body after semicolon declares enum' => [
'testMarker' => '/* testCaseInSwitchWhenCreatingEnumInSwitch2 */',
'expectedTokens' => [
'scope_opener' => T_SEMICOLON,
'scope_closer' => T_BREAK,
],
'testCloserMarker' => '/* testCaseInSwitchWhenCreatingEnumInSwitch2CloserMarker */',
],
'switch case, shared closer with switch' => [
'switch case, shared closer with switch' => [
'testMarker' => '/* testSwitchCaseWithoutBreakReturn */',
'expectedTokens' => [
'scope_opener' => T_COLON,
'scope_closer' => T_ENDSWITCH,
],
],
'switch case, nested inline if/elseif/else with and without braces' => [
'testMarker' => '/* testSwitchCaseNestedIfWithAndWithoutBraces */',
'expectedTokens' => [
'scope_opener' => T_COLON,
'scope_closer' => T_BREAK,
],
],
];

}//end dataNotEnumCases()
Expand Down

0 comments on commit c37e583

Please sign in to comment.