Skip to content

Commit cc5174a

Browse files
committed
Improved tokenizing of fallthrough CASE and DEFAULT statements that share a closing statement and use curly braces (ref #1558)
1 parent 71f7468 commit cc5174a

File tree

10 files changed

+140
-19
lines changed

10 files changed

+140
-19
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
3636
- NOWDOC tokens are now considered conditions, just as HEREDOC tokens are
3737
-- This makes it easier to find the start and end of a NOWDOC from any token within it
3838
-- Thanks to Michał Bundyra for the patch
39+
- Improved tokenizing of fallthrough CASE and DEFAULT statements that share a closing statement and use curly braces
3940
- Improved the error message when Squiz.ControlStructures.ControlSignature detects a newline after the closing parenthesis
4041
- Fixed bug #1465 : Generic.WhiteSpace.ScopeIndent reports incorrect errors when indenting double arrows in short arrays
4142
- Fixed bug #1478 : Indentation in fallthrough CASE that contains a closure

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,21 @@ return [
12131213
],
12141214
];
12151215

1216+
switch ($sContext) {
1217+
case 'SOMETHING':
1218+
case 'CONSTANT':
1219+
do_something();
1220+
break;
1221+
case 'GLOBAL':
1222+
case 'GLOBAL1':
1223+
do_something();
1224+
// Fall through
1225+
default:
1226+
{
1227+
do_something();
1228+
}
1229+
}
1230+
12161231
function foo()
12171232
{
12181233
$foo = array(

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,21 @@ return [
12131213
],
12141214
];
12151215

1216+
switch ($sContext) {
1217+
case 'SOMETHING':
1218+
case 'CONSTANT':
1219+
do_something();
1220+
break;
1221+
case 'GLOBAL':
1222+
case 'GLOBAL1':
1223+
do_something();
1224+
// Fall through
1225+
default:
1226+
{
1227+
do_something();
1228+
}
1229+
}
1230+
12161231
function foo()
12171232
{
12181233
$foo = array(

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,21 @@ return [
12131213
],
12141214
];
12151215

1216+
switch ($sContext) {
1217+
case 'SOMETHING':
1218+
case 'CONSTANT':
1219+
do_something();
1220+
break;
1221+
case 'GLOBAL':
1222+
case 'GLOBAL1':
1223+
do_something();
1224+
// Fall through
1225+
default:
1226+
{
1227+
do_something();
1228+
}
1229+
}
1230+
12161231
function foo()
12171232
{
12181233
$foo = array(

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,21 @@ return [
12131213
],
12141214
];
12151215

1216+
switch ($sContext) {
1217+
case 'SOMETHING':
1218+
case 'CONSTANT':
1219+
do_something();
1220+
break;
1221+
case 'GLOBAL':
1222+
case 'GLOBAL1':
1223+
do_something();
1224+
// Fall through
1225+
default:
1226+
{
1227+
do_something();
1228+
}
1229+
}
1230+
12161231
function foo()
12171232
{
12181233
$foo = array(

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@ public function getErrorList($testFile='ScopeIndentUnitTest.inc')
141141
1163 => 1,
142142
1197 => 1,
143143
1198 => 1,
144-
1216 => 1,
145-
1221 => 1,
146-
1223 => 1,
147-
1226 => 1,
148-
1230 => 1,
149144
1231 => 1,
150-
1232 => 1,
151-
1233 => 1,
145+
1236 => 1,
146+
1238 => 1,
147+
1241 => 1,
148+
1245 => 1,
149+
1246 => 1,
150+
1247 => 1,
151+
1248 => 1,
152152
);
153153

154154
}//end getErrorList()

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,19 @@ switch ($foo) {
246246
case 2:
247247
return 2;
248248
}
249+
250+
switch ($sContext)
251+
{
252+
case 'SOMETHING':
253+
case 'CONSTANT':
254+
do_something();
255+
break;
256+
case 'GLOBAL':
257+
case 'GLOBAL1':
258+
do_something();
259+
// Fall through
260+
default:
261+
{
262+
do_something();
263+
}
264+
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,19 @@ switch ($foo) {
249249
case 2:
250250
return 2;
251251
}
252+
253+
switch ($sContext)
254+
{
255+
case 'SOMETHING':
256+
case 'CONSTANT':
257+
do_something();
258+
break;
259+
case 'GLOBAL':
260+
case 'GLOBAL1':
261+
do_something();
262+
// Fall through
263+
default:
264+
{
265+
do_something();
266+
}
267+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function getErrorList()
4646
194 => 1,
4747
224 => 1,
4848
236 => 1,
49+
260 => 1,
4950
);
5051

5152
}//end getErrorList()

src/Tokenizers/PHP.php

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,18 +1592,6 @@ protected function processAdditional()
15921592
$this->tokens[$index]['scope_closer'] = $newCloser;
15931593
}
15941594

1595-
unset($this->tokens[$scopeOpener]['scope_condition']);
1596-
unset($this->tokens[$scopeOpener]['scope_opener']);
1597-
unset($this->tokens[$scopeOpener]['scope_closer']);
1598-
unset($this->tokens[$scopeCloser]['scope_condition']);
1599-
unset($this->tokens[$scopeCloser]['scope_opener']);
1600-
unset($this->tokens[$scopeCloser]['scope_closer']);
1601-
unset($this->tokens[$x]['bracket_opener']);
1602-
unset($this->tokens[$x]['bracket_closer']);
1603-
unset($this->tokens[$newCloser]['bracket_opener']);
1604-
unset($this->tokens[$newCloser]['bracket_closer']);
1605-
$this->tokens[$scopeCloser]['conditions'][] = $i;
1606-
16071595
if (PHP_CODESNIFFER_VERBOSITY > 1) {
16081596
$line = $this->tokens[$i]['line'];
16091597
$tokenType = $this->tokens[$i]['type'];
@@ -1617,6 +1605,45 @@ protected function processAdditional()
16171605
echo "\t* token $i ($tokenType) on line $line closer changed from $scopeCloser ($oldType) to $newCloser ($newType)".PHP_EOL;
16181606
}
16191607

1608+
if ($this->tokens[$scopeOpener]['scope_condition'] === $i) {
1609+
unset($this->tokens[$scopeOpener]['scope_condition']);
1610+
unset($this->tokens[$scopeOpener]['scope_opener']);
1611+
unset($this->tokens[$scopeOpener]['scope_closer']);
1612+
}
1613+
1614+
if ($this->tokens[$scopeCloser]['scope_condition'] === $i) {
1615+
unset($this->tokens[$scopeCloser]['scope_condition']);
1616+
unset($this->tokens[$scopeCloser]['scope_opener']);
1617+
unset($this->tokens[$scopeCloser]['scope_closer']);
1618+
} else {
1619+
// We were using a shared closer. All tokens that were
1620+
// sharing this closer with us, except for the scope condition
1621+
// and it's opener, need to now point to the new closer.
1622+
$condition = $this->tokens[$scopeCloser]['scope_condition'];
1623+
$start = ($this->tokens[$condition]['scope_opener'] + 1);
1624+
for ($y = $start; $y < $scopeCloser; $y++) {
1625+
if (isset($this->tokens[$y]['scope_closer']) === true
1626+
&& $this->tokens[$y]['scope_closer'] === $scopeCloser
1627+
) {
1628+
$this->tokens[$y]['scope_closer'] = $newCloser;
1629+
1630+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1631+
$line = $this->tokens[$y]['line'];
1632+
$tokenType = $this->tokens[$y]['type'];
1633+
$oldType = $this->tokens[$scopeCloser]['type'];
1634+
$newType = $this->tokens[$newCloser]['type'];
1635+
echo "\t\t* token $y ($tokenType) on line $line closer changed from $scopeCloser ($oldType) to $newCloser ($newType)".PHP_EOL;
1636+
}
1637+
}
1638+
}
1639+
}//end if
1640+
1641+
unset($this->tokens[$x]['bracket_opener']);
1642+
unset($this->tokens[$x]['bracket_closer']);
1643+
unset($this->tokens[$newCloser]['bracket_opener']);
1644+
unset($this->tokens[$newCloser]['bracket_closer']);
1645+
$this->tokens[$scopeCloser]['conditions'][] = $i;
1646+
16201647
// Now fix up all the tokens that think they are
16211648
// inside the CASE/DEFAULT statement when they are really outside.
16221649
for ($x = $newCloser; $x < $scopeCloser; $x++) {

0 commit comments

Comments
 (0)