Skip to content

Commit

Permalink
PSR2/SwitchDeclaration: bug fix - improve handling of comments
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
jrfnl authored and gsherwood committed May 27, 2021
1 parent c3eab21 commit 8c9feda
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ private function findNestedTerminator($phpcsFile, $stackPtr, $end)
$scopeOpener = $tokens[$currentCloser]['scope_opener'];
$scopeCloser = $tokens[$currentCloser]['scope_closer'];

$prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($scopeOpener - 1), $stackPtr, true);
$prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($scopeOpener - 1), $stackPtr, true);
if ($prevToken === false) {
return false;
}
Expand Down Expand Up @@ -292,7 +292,7 @@ private function findNestedTerminator($phpcsFile, $stackPtr, $end)
return false;
}

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

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

$nextCode = $phpcsFile->findNext(T_WHITESPACE, ($opener + 1), $endOfSwitch, true);
$nextCode = $phpcsFile->findNext(Tokens::$emptyTokens, ($opener + 1), $endOfSwitch, true);
if ($tokens[$nextCode]['code'] === T_CASE || $tokens[$nextCode]['code'] === T_DEFAULT) {
// This case statement has no content, so skip it.
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,29 @@ switch ( $test ) {

break;
}

// Handle comments correctly.
switch ($foo) {
case 1:
if ($bar > 0) {
doSomething();
}
// Comment
else {
return 1;
}
case 2:
return 2;
}

switch ($foo) {
case 1:
if ($bar > 0) /*comment*/ {
return doSomething();
}
else {
return 1;
}
case 2:
return 2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,29 @@ switch ( $test ) {

break;
}

// Handle comments correctly.
switch ($foo) {
case 1:
if ($bar > 0) {
doSomething();
}
// Comment
else {
return 1;
}
case 2:
return 2;
}

switch ($foo) {
case 1:
if ($bar > 0) /*comment*/ {
return doSomething();
}
else {
return 1;
}
case 2:
return 2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function getErrorList()
350 => 1,
356 => 1,
362 => 1,
384 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit 8c9feda

Please sign in to comment.