Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,40 @@ public function process(File $phpcsFile, int $stackPtr)
$exact = false;
}

// A comment between two CASE or DEFAULT statements that share a scope
// closer (fall-through cases) belongs with the following statement, so
// it is allowed to use that statement's indent instead of the body
// indent of the preceding case.
if ($checkToken !== null
&& $exact === false
&& $tokens[$checkToken]['code'] === T_COMMENT
&& empty($tokens[$checkToken]['conditions']) === false
) {
$lastCondition = $tokens[$checkToken]['conditions'];
end($lastCondition);
$lastCondition = key($lastCondition);
if (($tokens[$lastCondition]['code'] === T_CASE
|| $tokens[$lastCondition]['code'] === T_DEFAULT)
&& isset($tokens[$lastCondition]['scope_closer']) === true
) {
$nextStatement = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($checkToken + 1), null, true);
if ($nextStatement !== false
&& ($tokens[$nextStatement]['code'] === T_CASE
|| $tokens[$nextStatement]['code'] === T_DEFAULT)
&& isset($tokens[$nextStatement]['scope_closer']) === true
&& $tokens[$nextStatement]['scope_closer'] === $tokens[$lastCondition]['scope_closer']
) {
$checkIndent = ($currentIndent - $this->indent);

if ($this->debug === true) {
$line = $tokens[$checkToken]['line'];
StatusWriter::write("Comment between fall-through case statements found on line $line");
StatusWriter::write("=> checking indent of $checkIndent; main indent remains at $currentIndent", 1);
}
}
}
}

if ($checkIndent === null) {
$checkIndent = $currentIndent;
}
Expand Down
11 changes: 11 additions & 0 deletions src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1648,6 +1648,17 @@ $result = array_map(
$data
);

// Issue #1460: Comments between stacked case/default statements should not be flagged.
switch ($foo) {
case 1:
// Comment.
case 2:
// Comment.
default:
echo 'Test';
break;
}

/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
?>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1648,6 +1648,17 @@ $result = array_map(
$data
);

// Issue #1460: Comments between stacked case/default statements should not be flagged.
switch ($foo) {
case 1:
// Comment.
case 2:
// Comment.
default:
echo 'Test';
break;
}

/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
?>

Expand Down
11 changes: 11 additions & 0 deletions src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1648,6 +1648,17 @@ $result = array_map(
$data
);

// Issue #1460: Comments between stacked case/default statements should not be flagged.
switch ($foo) {
case 1:
// Comment.
case 2:
// Comment.
default:
echo 'Test';
break;
}

/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
?>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1648,6 +1648,17 @@ $result = array_map(
$data
);

// Issue #1460: Comments between stacked case/default statements should not be flagged.
switch ($foo) {
case 1:
// Comment.
case 2:
// Comment.
default:
echo 'Test';
break;
}

/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
?>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ public function getErrorList($testFile = '')
1527 => 1,
1529 => 1,
1530 => 1,
1659 => 1,
1660 => 1,
1661 => 1,
1662 => 1,
1670 => 1,
1671 => 1,
1672 => 1,
1673 => 1,
];
}

Expand Down
Loading