Skip to content

Commit 6783711

Browse files
committed
BlockControlStructureSpacingSniff: Fixed false positive
1 parent ce75dc2 commit 6783711

File tree

5 files changed

+46
-6
lines changed

5 files changed

+46
-6
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/AbstractControlStructureSpacing.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,19 @@ private function findControlStructureEnd(File $phpcsFile, int $controlStructureP
463463

464464
foreach ($pointers as $pointer) {
465465
if (TokenHelper::findPrevious($phpcsFile, T_SWITCH, $pointer - 1) === $switchPointer) {
466-
return TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $pointer - 1);
466+
$pointerBeforeCaseOrDefault = TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $pointer - 1);
467+
if (
468+
in_array($tokens[$pointerBeforeCaseOrDefault]['code'], Tokens::$commentTokens, true)
469+
&& $tokens[$pointerBeforeCaseOrDefault]['line'] + 1 === $tokens[$pointer]['line']
470+
) {
471+
$pointerBeforeCaseOrDefault = TokenHelper::findPreviousExcluding(
472+
$phpcsFile,
473+
T_WHITESPACE,
474+
$pointerBeforeCaseOrDefault - 1
475+
);
476+
}
477+
478+
return $pointerBeforeCaseOrDefault;
467479
}
468480
}
469481

tests/Sniffs/ControlStructures/BlockControlStructureSpacingSniffTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ public function testSwitchErrors(): void
358358
'Expected 1 line before "case", found 2.'
359359
);
360360

361+
self::assertNoSniffError($report, 44);
362+
361363
self::assertAllFixedInFile($report);
362364
}
363365

tests/Sniffs/ControlStructures/data/blockControlStructureSpacingSwitchErrors.fixed.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,16 @@
3535
case 2:
3636
return 2;
3737
}
38+
39+
switch ($foo) {
40+
case 1:
41+
case 2:
42+
echo 'Group 1';
43+
break;
44+
45+
// Group 2
46+
case 3:
47+
case 4:
48+
echo 'Group 2';
49+
break;
50+
}

tests/Sniffs/ControlStructures/data/blockControlStructureSpacingSwitchErrors.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,16 @@
3838
case 2:
3939
return 2;
4040
}
41+
42+
switch ($foo) {
43+
case 1:
44+
case 2:
45+
echo 'Group 1';
46+
break;
47+
48+
// Group 2
49+
case 3:
50+
case 4:
51+
echo 'Group 2';
52+
break;
53+
}

tests/Sniffs/ControlStructures/data/blockControlStructureSpacingWithDefaultSettingsNoErrors.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ function () {
101101
};
102102

103103
switch ($foo) {
104-
case 'bar':
104+
case 'bar':
105105

106-
case 'baz':
107-
return true;
106+
case 'baz':
107+
return true;
108108

109-
case 'lorem':
110-
return false;
109+
case 'lorem':
110+
return false;
111111
}
112112

113113
if (true) {

0 commit comments

Comments
 (0)