Skip to content

Commit 9546775

Browse files
rodrigoprimojrfnl
authored andcommitted
Tests/Tokenizer: scope opener is set correctly for T_SWITCH
This commit copies, with some modifications, a sniff test to the tokenizer tests. It ensures that the scope opener is set correctly for T_SWITCH when there is a closure in the condition. This safeguards against regressions related to 30c618e, which fixed https://github .com/squizlabs/PHP_CodeSniffer/issues/543. 30c618e originally added a test to InlineControlStructureUnitTest.php as there were no Tokenizer tests back then. Since the InlineControlStructure sniff doesn't listen for the `T_SWITCH` token anymore (see baa4f65), the original test added in 30c618e became useless and thus was refactored to use a different control structure. This new version of the sniff test was kept as testing code that uses a closure in the condition is still an interesting case and is not covered in other tests.
1 parent b9809c7 commit 9546775

File tree

5 files changed

+46
-18
lines changed

5 files changed

+46
-18
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ if ($test) {
106106
} else {
107107
}
108108

109-
switch($response = \Bar::baz('bat', function ($foo) {
109+
if ((function () {
110110
return 'bar';
111-
})) {
112-
case 1:
113-
return 'test';
111+
})())
112+
echo 'one';
113+
114+
115+
116+
114117

115-
case 2:
116-
return 'other';
117-
}
118118

119119
$stuff = [1,2,3];
120120
foreach($stuff as $num)

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,17 @@ if ($test) {
118118
} else {
119119
}
120120

121-
switch($response = \Bar::baz('bat', function ($foo) {
121+
if ((function () {
122122
return 'bar';
123-
})) {
124-
case 1:
125-
return 'test';
126-
127-
case 2:
128-
return 'other';
123+
})()) {
124+
echo 'one';
129125
}
130126

127+
128+
129+
130+
131+
131132
$stuff = [1,2,3];
132133
foreach($stuff as $num) {
133134
if ($num %2 ) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function getErrorList($testFile='')
4848
62 => 1,
4949
66 => 1,
5050
78 => 1,
51+
109 => 1,
5152
120 => 1,
5253
128 => 1,
5354
134 => 1,

tests/Core/Tokenizers/Tokenizer/RecurseScopeMapSwitchTokenScopeTest.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,12 @@ switch ($value):
2222
break;
2323
/* testSwitchAlternativeSyntaxEnd */
2424
endswitch;
25+
26+
// Test for https://github.com/squizlabs/PHP_CodeSniffer/issues/543
27+
/* testSwitchClosureWithinCondition */
28+
switch((function () {
29+
return 'bar';
30+
})()) /* testSwitchClosureWithinConditionScopeOpener */ {
31+
case 1:
32+
return 'test';
33+
}

tests/Core/Tokenizers/Tokenizer/RecurseScopeMapSwitchTokenScopeTest.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ final class RecurseScopeMapSwitchTokenScopeTest extends AbstractTokenizerTestCas
2020
*
2121
* @param string $testMarker The comment which prefaces the target token in the test file.
2222
* @param array<string, int|string> $expectedTokens The expected token codes for the scope opener/closer.
23+
* @param string|null $testOpenerMarker Optional. The comment which prefaces the scope opener if different
24+
* from the test marker.
2325
* @param string|null $testCloserMarker Optional. The comment which prefaces the scope closer if different
2426
* from the test marker.
2527
*
@@ -30,19 +32,24 @@ final class RecurseScopeMapSwitchTokenScopeTest extends AbstractTokenizerTestCas
3032
*
3133
* @return void
3234
*/
33-
public function testSwitchScope($testMarker, $expectedTokens, $testCloserMarker=null)
35+
public function testSwitchScope($testMarker, $expectedTokens, $testOpenerMarker=null, $testCloserMarker=null)
3436
{
3537
$tokens = $this->phpcsFile->getTokens();
3638
$switchIndex = $this->getTargetToken($testMarker, [T_SWITCH]);
3739
$tokenArray = $tokens[$switchIndex];
3840

41+
$scopeOpenerMarker = $testMarker;
42+
if (isset($testOpenerMarker) === true) {
43+
$scopeOpenerMarker = $testOpenerMarker;
44+
}
45+
3946
$scopeCloserMarker = $testMarker;
4047
if (isset($testCloserMarker) === true) {
4148
$scopeCloserMarker = $testCloserMarker;
4249
}
4350

4451
$expectedScopeCondition = $switchIndex;
45-
$expectedScopeOpener = $this->getTargetToken($testMarker, $expectedTokens['scope_opener']);
52+
$expectedScopeOpener = $this->getTargetToken($scopeOpenerMarker, $expectedTokens['scope_opener']);
4653
$expectedScopeCloser = $this->getTargetToken($scopeCloserMarker, $expectedTokens['scope_closer']);
4754

4855
$this->assertArrayHasKey('scope_condition', $tokenArray, 'Scope condition not set');
@@ -90,21 +97,31 @@ public function testSwitchScope($testMarker, $expectedTokens, $testCloserMarker=
9097
public static function dataSwitchScope()
9198
{
9299
return [
93-
'switch normal syntax' => [
100+
'switch normal syntax' => [
94101
'testMarker' => '/* testSwitchNormalSyntax */',
95102
'expectedTokens' => [
96103
'scope_opener' => T_OPEN_CURLY_BRACKET,
97104
'scope_closer' => T_CLOSE_CURLY_BRACKET,
98105
],
99106
],
100-
'switch alternative syntax' => [
107+
'switch alternative syntax' => [
101108
'testMarker' => '/* testSwitchAlternativeSyntax */',
102109
'expectedTokens' => [
103110
'scope_opener' => T_COLON,
104111
'scope_closer' => T_ENDSWITCH,
105112
],
113+
'testOpenerMarker' => null,
106114
'testCloserMarker' => '/* testSwitchAlternativeSyntaxEnd */',
107115
],
116+
'switch with closure in the condition' => [
117+
'testMarker' => '/* testSwitchClosureWithinCondition */',
118+
'expectedTokens' => [
119+
'scope_opener' => T_OPEN_CURLY_BRACKET,
120+
'scope_closer' => T_CLOSE_CURLY_BRACKET,
121+
],
122+
'testOpenerMarker' => '/* testSwitchClosureWithinConditionScopeOpener */',
123+
'testCloserMarker' => '/* testSwitchClosureWithinConditionScopeOpener */',
124+
],
108125
];
109126

110127
}//end dataSwitchScope()

0 commit comments

Comments
 (0)