Skip to content

Commit dd73c0f

Browse files
committed
DisallowCommentAfterCodeSniff: Fixed false positive
1 parent 4f5d56d commit dd73c0f

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

SlevomatCodingStandard/Sniffs/Commenting/DisallowCommentAfterCodeSniff.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,24 @@ public function process(File $phpcsFile, $commentPointer): void
4747
return;
4848
}
4949

50+
$tokens = $phpcsFile->getTokens();
51+
52+
$commentEndPointer = CommentHelper::getCommentEndPointer($phpcsFile, $commentPointer);
53+
$nextNonWhitespacePointer = TokenHelper::findNextExcluding($phpcsFile, T_WHITESPACE, $commentEndPointer + 1);
54+
55+
if (
56+
$nextNonWhitespacePointer !== null
57+
&& $tokens[$nextNonWhitespacePointer]['line'] === $tokens[$commentEndPointer]['line']
58+
59+
) {
60+
return;
61+
}
62+
5063
$fix = $phpcsFile->addFixableError('Comment after code is disallowed.', $commentPointer, self::CODE_DISALLOWED_COMMENT_AFTER_CODE);
5164
if (!$fix) {
5265
return;
5366
}
5467

55-
$tokens = $phpcsFile->getTokens();
56-
57-
$commentEndPointer = CommentHelper::getCommentEndPointer($phpcsFile, $commentPointer);
5868
$commentContent = TokenHelper::getContent($phpcsFile, $commentPointer, $commentEndPointer);
5969
$commentHasNewLineAtTheEnd = substr($commentContent, -strlen($phpcsFile->eolChar)) === $phpcsFile->eolChar;
6070

tests/Sniffs/Commenting/data/disallowCommentAfterCodeNoErrors.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ class Whatever
3030
{
3131

3232
}
33+
34+
$match = Strings::match($address, /** @lang RegExp */'~^regexp$~');

0 commit comments

Comments
 (0)