From f451ed77629b3c45da9fb8cb659c2f3b7c213f31 Mon Sep 17 00:00:00 2001 From: Lazizbek Ergashev Date: Sat, 25 Jul 2026 11:23:27 +0500 Subject: [PATCH] Generic/ScopeIndent: don't flag comments between stacked case statements --- .../Sniffs/WhiteSpace/ScopeIndentSniff.php | 34 +++++++++++++++++++ .../WhiteSpace/ScopeIndentUnitTest.1.inc | 11 ++++++ .../ScopeIndentUnitTest.1.inc.fixed | 11 ++++++ .../WhiteSpace/ScopeIndentUnitTest.2.inc | 11 ++++++ .../ScopeIndentUnitTest.2.inc.fixed | 11 ++++++ .../Tests/WhiteSpace/ScopeIndentUnitTest.php | 8 ++--- 6 files changed, 82 insertions(+), 4 deletions(-) diff --git a/src/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php b/src/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php index 348b5a5245..9aa939719c 100644 --- a/src/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php +++ b/src/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php @@ -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; } diff --git a/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc b/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc index eafa67896a..34cb92afc5 100644 --- a/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc +++ b/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc @@ -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! */ ?> diff --git a/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc.fixed b/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc.fixed index 355b5a9752..2f796451d0 100644 --- a/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc.fixed +++ b/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc.fixed @@ -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! */ ?> diff --git a/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc b/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc index 2bca107c35..a73eafa7e3 100644 --- a/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc +++ b/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc @@ -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! */ ?> diff --git a/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc.fixed b/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc.fixed index c820264af0..653964295b 100644 --- a/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc.fixed +++ b/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc.fixed @@ -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! */ ?> diff --git a/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.php b/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.php index 7a3ccded67..7e1d782b3c 100644 --- a/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.php +++ b/src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.php @@ -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, ]; }