Skip to content

Commit f489891

Browse files
committed
JumpStatementsSpacingSniff: Fixed false positive
1 parent 808db4a commit f489891

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/AbstractControlStructureSpacing.php

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -226,18 +226,32 @@ protected function checkLinesAfter(File $phpcsFile, int $controlStructurePointer
226226
return;
227227
}
228228

229-
$hasCommentAfter = in_array(
230-
$tokens[$notWhitespacePointerAfter]['code'],
231-
Tokens::$commentTokens,
232-
true
233-
) && $tokens[$notWhitespacePointerAfter]['line'] === $tokens[$controlStructureEndPointer]['line'];
234-
$pointerAfter = $hasCommentAfter
235-
? TokenHelper::findNextExcluding($phpcsFile, T_WHITESPACE, $notWhitespacePointerAfter + 1)
236-
: $notWhitespacePointerAfter;
237-
238-
$isLastControlStructure = in_array($tokens[$controlStructurePointer]['code'], [T_CASE, T_DEFAULT], true)
239-
? $tokens[$pointerAfter]['code'] === T_CLOSE_CURLY_BRACKET
240-
: in_array($tokens[$pointerAfter]['code'], [T_CLOSE_CURLY_BRACKET, T_CASE, T_DEFAULT], true);
229+
$hasCommentAfter = in_array($tokens[$notWhitespacePointerAfter]['code'], Tokens::$commentTokens, true);
230+
$isCommentAfterOnSameLine = false;
231+
$pointerAfter = $notWhitespacePointerAfter;
232+
233+
$isControlStructureEndAfterPointer = static function (int $pointer) use ($tokens, $controlStructurePointer): bool {
234+
return in_array($tokens[$controlStructurePointer]['code'], [T_CASE, T_DEFAULT], true)
235+
? $tokens[$pointer]['code'] === T_CLOSE_CURLY_BRACKET
236+
: in_array($tokens[$pointer]['code'], [T_CLOSE_CURLY_BRACKET, T_CASE, T_DEFAULT], true);
237+
};
238+
239+
if ($hasCommentAfter) {
240+
if ($tokens[$notWhitespacePointerAfter]['line'] === $tokens[$controlStructureEndPointer]['line'] + 1) {
241+
$commentEndPointer = CommentHelper::getCommentEndPointer($phpcsFile, $notWhitespacePointerAfter);
242+
$pointerAfterComment = TokenHelper::findNextExcluding($phpcsFile, T_WHITESPACE, $commentEndPointer + 1);
243+
244+
if ($isControlStructureEndAfterPointer($pointerAfterComment)) {
245+
$controlStructureEndPointer = $commentEndPointer;
246+
$pointerAfter = $pointerAfterComment;
247+
}
248+
} elseif ($tokens[$notWhitespacePointerAfter]['line'] === $tokens[$controlStructureEndPointer]['line']) {
249+
$isCommentAfterOnSameLine = true;
250+
$pointerAfter = TokenHelper::findNextExcluding($phpcsFile, T_WHITESPACE, $notWhitespacePointerAfter + 1);
251+
}
252+
}
253+
254+
$isLastControlStructure = $isControlStructureEndAfterPointer($pointerAfter);
241255

242256
$requiredLinesCountAfter = $isLastControlStructure
243257
? $this->getLinesCountAfterLast($phpcsFile, $controlStructurePointer, $controlStructureEndPointer)
@@ -263,7 +277,7 @@ protected function checkLinesAfter(File $phpcsFile, int $controlStructurePointer
263277
return;
264278
}
265279

266-
$replaceStartPointer = $hasCommentAfter ? $notWhitespacePointerAfter : $controlStructureEndPointer;
280+
$replaceStartPointer = $isCommentAfterOnSameLine ? $notWhitespacePointerAfter : $controlStructureEndPointer;
267281
$endOfLineBeforeAfterPointer = TokenHelper::findPreviousContent($phpcsFile, T_WHITESPACE, $phpcsFile->eolChar, $pointerAfter - 1);
268282

269283
$phpcsFile->fixer->beginChangeset();
@@ -272,7 +286,7 @@ protected function checkLinesAfter(File $phpcsFile, int $controlStructurePointer
272286
$phpcsFile->fixer->replaceToken($i, '');
273287
}
274288

275-
if ($hasCommentAfter) {
289+
if ($isCommentAfterOnSameLine) {
276290
for ($i = 0; $i < $requiredLinesCountAfter; $i++) {
277291
$phpcsFile->fixer->addNewline($notWhitespacePointerAfter);
278292
}

tests/Sniffs/ControlStructures/data/jumpStatementsSpacingWithDefaultSettingsNoErrors.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,11 @@ function () {
178178
// second comment
179179
return $foo + $bar;
180180
};
181+
182+
function ($bool) {
183+
if (!$bool) {
184+
// @codeCoverageIgnoreStart
185+
throw new Exception('Some error');
186+
// @codeCoverageIgnoreEnd
187+
}
188+
};

0 commit comments

Comments
 (0)