Skip to content

Commit ee32501

Browse files
committed
Tests/Tokenizer: test to ensure scope closer is set correctly for T_CASE
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.
1 parent 35b20c0 commit ee32501

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

tests/Core/Tokenizer/Tokenizer/RecurseScopeMapCaseKeywordConditionsTest.inc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,17 @@ switch ($value):
103103
case 1:
104104
echo 'one';
105105
endswitch;
106+
107+
// Test for https://github.com/squizlabs/PHP_CodeSniffer/issues/879
108+
switch ($type) {
109+
/* testSwitchCaseNestedIfWithAndWithoutBraces */
110+
case 1:
111+
if ($foo) {
112+
return true;
113+
} elseif ($baz)
114+
return true;
115+
else {
116+
echo 'else';
117+
}
118+
break;
119+
}

tests/Core/Tokenizer/Tokenizer/RecurseScopeMapCaseKeywordConditionsTest.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,68 +139,75 @@ public function testNotEnumCases($testMarker, $expectedTokens, $testCloserMarker
139139
public static function dataNotEnumCases()
140140
{
141141
return [
142-
'switch case with constant, semicolon condition end' => [
142+
'switch case with constant, semicolon condition end' => [
143143
'testMarker' => '/* testCaseWithSemicolonIsNotEnumCase */',
144144
'expectedTokens' => [
145145
'scope_opener' => T_SEMICOLON,
146146
'scope_closer' => T_CLOSE_CURLY_BRACKET,
147147
],
148148
],
149-
'switch case with constant, colon condition end' => [
149+
'switch case with constant, colon condition end' => [
150150
'testMarker' => '/* testCaseWithConstantIsNotEnumCase */',
151151
'expectedTokens' => [
152152
'scope_opener' => T_COLON,
153153
'scope_closer' => T_CLOSE_CURLY_BRACKET,
154154
],
155155
'testCloserMarker' => '/* testCaseConstantCloserMarker */',
156156
],
157-
'switch case with constant, comparison' => [
157+
'switch case with constant, comparison' => [
158158
'testMarker' => '/* testCaseWithConstantAndIdenticalIsNotEnumCase */',
159159
'expectedTokens' => [
160160
'scope_opener' => T_COLON,
161161
'scope_closer' => T_CLOSE_CURLY_BRACKET,
162162
],
163163
'testCloserMarker' => '/* testCaseConstantCloserMarker */',
164164
],
165-
'switch case with constant, assignment' => [
165+
'switch case with constant, assignment' => [
166166
'testMarker' => '/* testCaseWithAssigmentToConstantIsNotEnumCase */',
167167
'expectedTokens' => [
168168
'scope_opener' => T_COLON,
169169
'scope_closer' => T_CLOSE_CURLY_BRACKET,
170170
],
171171
'testCloserMarker' => '/* testCaseConstantCloserMarker */',
172172
],
173-
'switch case with constant, keyword in mixed case' => [
173+
'switch case with constant, keyword in mixed case' => [
174174
'testMarker' => '/* testIsNotEnumCaseIsCaseInsensitive */',
175175
'expectedTokens' => [
176176
'scope_opener' => T_COLON,
177177
'scope_closer' => T_CLOSE_CURLY_BRACKET,
178178
],
179179
'testCloserMarker' => '/* testCaseConstantCloserMarker */',
180180
],
181-
'switch case, body in curlies declares enum' => [
181+
'switch case, body in curlies declares enum' => [
182182
'testMarker' => '/* testCaseInSwitchWhenCreatingEnumInSwitch1 */',
183183
'expectedTokens' => [
184184
'scope_opener' => T_OPEN_CURLY_BRACKET,
185185
'scope_closer' => T_CLOSE_CURLY_BRACKET,
186186
],
187187
'testCloserMarker' => '/* testCaseInSwitchWhenCreatingEnumInSwitch1CloserMarker */',
188188
],
189-
'switch case, body after semicolon declares enum' => [
189+
'switch case, body after semicolon declares enum' => [
190190
'testMarker' => '/* testCaseInSwitchWhenCreatingEnumInSwitch2 */',
191191
'expectedTokens' => [
192192
'scope_opener' => T_SEMICOLON,
193193
'scope_closer' => T_BREAK,
194194
],
195195
'testCloserMarker' => '/* testCaseInSwitchWhenCreatingEnumInSwitch2CloserMarker */',
196196
],
197-
'switch case, shared closer with switch' => [
197+
'switch case, shared closer with switch' => [
198198
'testMarker' => '/* testSwitchCaseScopeCloserSharedWithSwitch */',
199199
'expectedTokens' => [
200200
'scope_opener' => T_COLON,
201201
'scope_closer' => T_ENDSWITCH,
202202
],
203203
],
204+
'switch case, nested inline if/elseif/else with and without braces' => [
205+
'testMarker' => '/* testSwitchCaseNestedIfWithAndWithoutBraces */',
206+
'expectedTokens' => [
207+
'scope_opener' => T_COLON,
208+
'scope_closer' => T_BREAK,
209+
],
210+
],
204211
];
205212

206213
}//end dataNotEnumCases()

0 commit comments

Comments
 (0)