Skip to content

Commit 15aedfc

Browse files
committed
Fixed bug #2688 : Case statements not tokenized correctly when switch is contained within ternary
1 parent 5c9586b commit 15aedfc

File tree

6 files changed

+165
-45
lines changed

6 files changed

+165
-45
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
2626
</stability>
2727
<license uri="https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt">BSD 3-Clause License</license>
2828
<notes>
29+
- Fixed bug #2688 : Case statements not tokenized correctly when switch is contained within ternary
2930
- Fixed bug #2698 : PHPCS throws errors determining auto report width when shell_exec is disabled
3031
-- Thanks to Matthew Peveler for the patch
3132
- Fixed bug #2751 : Autoload relative paths first to avoid confusion with files from the global include path

src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,12 @@ switch ($sContext)
262262
do_something();
263263
}
264264
}
265+
266+
$foo = $foo ?
267+
function () {
268+
switch ($a) {
269+
case 'a':
270+
break;
271+
}
272+
} :
273+
null;

src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,12 @@ switch ($sContext)
265265
do_something();
266266
}
267267
}
268+
269+
$foo = $foo ?
270+
function () {
271+
switch ($a) {
272+
case 'a':
273+
break;
274+
}
275+
} :
276+
null;

src/Standards/Squiz/Tests/ControlStructures/SwitchDeclarationUnitTest.inc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,19 @@ switch ($foo) {
315315
'bar'
316316
);
317317
}
318+
319+
$foo = $foo ?
320+
function () {
321+
switch ($a) {
322+
case 'a':
323+
break;
324+
325+
case (preg_match('/foo/i', $foo) ? $a : $b):
326+
echo 'really?'
327+
break;
328+
329+
default:
330+
break;
331+
}
332+
} :
333+
null;

src/Standards/Squiz/Tests/ControlStructures/SwitchDeclarationUnitTest.php

Lines changed: 97 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -27,48 +27,103 @@ class SwitchDeclarationUnitTest extends AbstractSniffUnitTest
2727
*/
2828
public function getErrorList($testFile='SwitchDeclarationUnitTest.inc')
2929
{
30-
return [
31-
27 => 1,
32-
29 => 1,
33-
34 => 1,
34-
36 => 1,
35-
44 => 1,
36-
48 => 1,
37-
52 => 1,
38-
54 => 1,
39-
55 => 1,
40-
56 => 1,
41-
58 => 1,
42-
59 => 1,
43-
61 => 1,
44-
62 => 1,
45-
79 => 1,
46-
85 => 2,
47-
88 => 2,
48-
89 => 2,
49-
92 => 1,
50-
95 => 3,
51-
99 => 1,
52-
116 => 1,
53-
122 => 1,
54-
127 => 2,
55-
134 => 2,
56-
135 => 1,
57-
138 => 1,
58-
143 => 1,
59-
144 => 1,
60-
147 => 1,
61-
165 => 1,
62-
172 => 1,
63-
176 => 2,
64-
180 => 1,
65-
192 => 2,
66-
196 => 1,
67-
223 => 1,
68-
266 => 1,
69-
282 => 1,
70-
284 => 2,
71-
];
30+
switch ($testFile) {
31+
case 'SwitchDeclarationUnitTest.inc':
32+
return [
33+
27 => 1,
34+
29 => 1,
35+
34 => 1,
36+
36 => 1,
37+
44 => 1,
38+
48 => 1,
39+
52 => 1,
40+
54 => 1,
41+
55 => 1,
42+
56 => 1,
43+
58 => 1,
44+
59 => 1,
45+
61 => 1,
46+
62 => 1,
47+
79 => 1,
48+
85 => 2,
49+
88 => 2,
50+
89 => 2,
51+
92 => 1,
52+
95 => 3,
53+
99 => 1,
54+
116 => 1,
55+
122 => 1,
56+
127 => 2,
57+
134 => 2,
58+
135 => 1,
59+
138 => 1,
60+
143 => 1,
61+
144 => 1,
62+
147 => 1,
63+
165 => 1,
64+
172 => 1,
65+
176 => 2,
66+
180 => 1,
67+
192 => 2,
68+
196 => 1,
69+
223 => 1,
70+
266 => 1,
71+
282 => 1,
72+
284 => 2,
73+
322 => 1,
74+
323 => 1,
75+
327 => 1,
76+
329 => 1,
77+
330 => 1,
78+
];
79+
80+
case 'SwitchDeclarationUnitTest.js':
81+
return [
82+
27 => 1,
83+
29 => 1,
84+
34 => 1,
85+
36 => 1,
86+
44 => 1,
87+
48 => 1,
88+
52 => 1,
89+
54 => 1,
90+
55 => 1,
91+
56 => 1,
92+
58 => 1,
93+
59 => 1,
94+
61 => 1,
95+
62 => 1,
96+
79 => 1,
97+
85 => 2,
98+
88 => 2,
99+
89 => 2,
100+
92 => 1,
101+
95 => 3,
102+
99 => 1,
103+
116 => 1,
104+
122 => 1,
105+
127 => 2,
106+
134 => 2,
107+
135 => 1,
108+
138 => 1,
109+
143 => 1,
110+
144 => 1,
111+
147 => 1,
112+
165 => 1,
113+
172 => 1,
114+
176 => 2,
115+
180 => 1,
116+
192 => 2,
117+
196 => 1,
118+
223 => 1,
119+
266 => 1,
120+
282 => 1,
121+
284 => 2,
122+
];
123+
124+
default:
125+
return [];
126+
}//end switch
72127

73128
}//end getErrorList()
74129

src/Tokenizers/PHP.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ function return types. We want to keep the parenthesis map clean,
14501450
// inline IF statement.
14511451
if (empty($insideInlineIf) === false && $newToken['code'] === T_COLON) {
14521452
// Make sure this isn't the return type separator of a closure.
1453-
$isReturnType = false;
1453+
$isInlineIf = true;
14541454
for ($i = ($stackPtr - 1); $i > 0; $i--) {
14551455
if (is_array($tokens[$i]) === false
14561456
|| ($tokens[$i][0] !== T_DOC_COMMENT
@@ -1487,14 +1487,44 @@ function return types. We want to keep the parenthesis map clean,
14871487
}
14881488

14891489
if ($tokens[$i][0] === T_FUNCTION || $tokens[$i][0] === T_FN || $tokens[$i][0] === T_USE) {
1490-
$isReturnType = true;
1490+
$isInlineIf = false;
1491+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1492+
echo "\t\t* token is function return type, not T_INLINE_ELSE".PHP_EOL;
1493+
}
14911494
}
14921495
}//end if
14931496

1494-
if ($isReturnType === false) {
1497+
// Check to see if this is a CASE or DEFAULT opener.
1498+
$inlineIfToken = $insideInlineIf[(count($insideInlineIf) - 1)];
1499+
for ($i = $stackPtr; $i > $inlineIfToken; $i--) {
1500+
if (is_array($tokens[$i]) === true
1501+
&& ($tokens[$i][0] === T_CASE
1502+
|| $tokens[$i][0] === T_DEFAULT)
1503+
) {
1504+
$isInlineIf = false;
1505+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1506+
echo "\t\t* token is T_CASE or T_DEFAULT opener, not T_INLINE_ELSE".PHP_EOL;
1507+
}
1508+
1509+
break;
1510+
}
1511+
1512+
if (is_array($tokens[$i]) === false
1513+
&& ($tokens[$i] === ';'
1514+
|| $tokens[$i] === '{')
1515+
) {
1516+
break;
1517+
}
1518+
}
1519+
1520+
if ($isInlineIf === true) {
14951521
array_pop($insideInlineIf);
14961522
$newToken['code'] = T_INLINE_ELSE;
14971523
$newToken['type'] = 'T_INLINE_ELSE';
1524+
1525+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1526+
echo "\t\t* token changed from T_COLON to T_INLINE_ELSE".PHP_EOL;
1527+
}
14981528
}
14991529
}//end if
15001530

0 commit comments

Comments
 (0)