Skip to content

Commit 73612ee

Browse files
committed
BlockControlStructureSpacingSniff: Fixed false positive
1 parent ae2b81e commit 73612ee

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/AbstractControlStructureSpacing.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,20 @@ protected function checkLinesBefore(File $phpcsFile, int $controlStructurePointe
125125
}
126126
}
127127

128+
$pointerBeforeComment = $pointerBefore;
128129
if (in_array($tokens[$pointerBefore]['code'], Tokens::$commentTokens, true)) {
129130
$pointerBeforeComment = TokenHelper::findPreviousEffective($phpcsFile, $pointerBefore - 1);
130-
$isFirstControlStructure = in_array($tokens[$pointerBeforeComment]['code'], [T_OPEN_CURLY_BRACKET, T_COLON], true);
131-
} else {
132-
$isFirstControlStructure = in_array($tokens[$pointerBefore]['code'], [T_OPEN_CURLY_BRACKET, T_COLON], true);
131+
}
132+
133+
$isFirstControlStructure = in_array($tokens[$pointerBeforeComment]['code'], [T_OPEN_CURLY_BRACKET, T_COLON], true);
134+
135+
if (
136+
$isFirstControlStructure
137+
&& in_array($tokens[$controlStructurePointer]['code'], [T_CASE, T_DEFAULT], true)
138+
&& array_key_exists('scope_condition', $tokens[$pointerBeforeComment])
139+
&& in_array($tokens[$tokens[$pointerBeforeComment]['scope_condition']]['code'], [T_CASE, T_DEFAULT], true)
140+
) {
141+
$isFirstControlStructure = false;
133142
}
134143

135144
$whitespaceBefore = '';

tests/Sniffs/ControlStructures/data/blockControlStructureSpacingWithDefaultSettingsNoErrors.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ function () {
7979
}
8080
};
8181

82+
switch ($foo) {
83+
case 'bar':
84+
85+
case 'baz':
86+
return true;
87+
88+
case 'lorem':
89+
return false;
90+
}
91+
8292
if (true) {
8393

8494
}

0 commit comments

Comments
 (0)