Skip to content

Commit ee74108

Browse files
committed
SlevomatCodingStandard.Commenting.DisallowCommentAfterCode: Workaround for // phpcs:disable in doc comment
1 parent 0bcf8f0 commit ee74108

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

SlevomatCodingStandard/Sniffs/Commenting/DisallowCommentAfterCodeSniff.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,24 @@ public function register(): array
4242
*/
4343
public function process(File $phpcsFile, $commentPointer): void
4444
{
45+
$tokens = $phpcsFile->getTokens();
46+
47+
if ($tokens[$commentPointer]['column'] === 1) {
48+
return;
49+
}
50+
4551
$firstNonWhitespacePointerOnLine = TokenHelper::findFirstNonWhitespaceOnLine($phpcsFile, $commentPointer);
4652

4753
if ($firstNonWhitespacePointerOnLine === $commentPointer) {
4854
return;
4955
}
5056

51-
$tokens = $phpcsFile->getTokens();
57+
if (
58+
$tokens[$firstNonWhitespacePointerOnLine]['code'] === T_DOC_COMMENT_OPEN_TAG
59+
&& $tokens[$firstNonWhitespacePointerOnLine]['comment_closer'] > $commentPointer
60+
) {
61+
return;
62+
}
5263

5364
$commentEndPointer = CommentHelper::getCommentEndPointer($phpcsFile, $commentPointer);
5465
$nextNonWhitespacePointer = TokenHelper::findNextNonWhitespace($phpcsFile, $commentEndPointer + 1);

tests/Sniffs/Commenting/data/disallowCommentAfterCodeNoErrors.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,21 @@ function () {
2929
*/
3030
class Whatever
3131
{
32-
32+
public function doAnything()
33+
{
34+
/*
35+
Comment
36+
*/
37+
while (true) {
38+
39+
}
40+
}
3341
}
3442

3543
$match = Strings::match($address, /** @lang RegExp */'~^regexp$~');
44+
45+
/** phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps */
46+
class Something
47+
{
48+
49+
}

0 commit comments

Comments
 (0)