Skip to content

Commit ec015a8

Browse files
committed
PSR2/SwitchDeclaration: bug fix - improve handling of comments
Comments in certain places in the code flow would throw the `findNestedTerminator()` method used for determining the `TerminatingComment` error off. Fixed now. Includes unit tests. Without this fix, the first test would not throw an error (false negative), while the second would (false positive).
1 parent 478d4e5 commit ec015a8

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

src/Standards/PSR2/Sniffs/ControlStructures/SwitchDeclarationSniff.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ private function findNestedTerminator($phpcsFile, $stackPtr, $end)
264264
$scopeOpener = $tokens[$currentCloser]['scope_opener'];
265265
$scopeCloser = $tokens[$currentCloser]['scope_closer'];
266266

267-
$prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($scopeOpener - 1), $stackPtr, true);
267+
$prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($scopeOpener - 1), $stackPtr, true);
268268
if ($prevToken === false) {
269269
return false;
270270
}
@@ -292,7 +292,7 @@ private function findNestedTerminator($phpcsFile, $stackPtr, $end)
292292
return false;
293293
}
294294

295-
$currentCloser = $phpcsFile->findPrevious(T_WHITESPACE, ($prevToken - 1), $stackPtr, true);
295+
$currentCloser = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prevToken - 1), $stackPtr, true);
296296
if ($tokens[$prevToken]['code'] === T_ELSE) {
297297
$hasElseBlock = true;
298298
}
@@ -309,7 +309,7 @@ private function findNestedTerminator($phpcsFile, $stackPtr, $end)
309309

310310
$opener = $tokens[$nextCase]['scope_opener'];
311311

312-
$nextCode = $phpcsFile->findNext(T_WHITESPACE, ($opener + 1), $endOfSwitch, true);
312+
$nextCode = $phpcsFile->findNext(Tokens::$emptyTokens, ($opener + 1), $endOfSwitch, true);
313313
if ($tokens[$nextCode]['code'] === T_CASE || $tokens[$nextCode]['code'] === T_DEFAULT) {
314314
// This case statement has no content, so skip it.
315315
continue;

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,29 @@ switch ( $test ) {
378378

379379
break;
380380
}
381+
382+
// Handle comments correctly.
383+
switch ($foo) {
384+
case 1:
385+
if ($bar > 0) {
386+
doSomething();
387+
}
388+
// Comment
389+
else {
390+
return 1;
391+
}
392+
case 2:
393+
return 2;
394+
}
395+
396+
switch ($foo) {
397+
case 1:
398+
if ($bar > 0) /*comment*/ {
399+
return doSomething();
400+
}
401+
else {
402+
return 1;
403+
}
404+
case 2:
405+
return 2;
406+
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,29 @@ switch ( $test ) {
373373

374374
break;
375375
}
376+
377+
// Handle comments correctly.
378+
switch ($foo) {
379+
case 1:
380+
if ($bar > 0) {
381+
doSomething();
382+
}
383+
// Comment
384+
else {
385+
return 1;
386+
}
387+
case 2:
388+
return 2;
389+
}
390+
391+
switch ($foo) {
392+
case 1:
393+
if ($bar > 0) /*comment*/ {
394+
return doSomething();
395+
}
396+
else {
397+
return 1;
398+
}
399+
case 2:
400+
return 2;
401+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function getErrorList()
5353
350 => 1,
5454
356 => 1,
5555
362 => 1,
56+
384 => 1,
5657
];
5758

5859
}//end getErrorList()

0 commit comments

Comments
 (0)