Skip to content

Commit 82c9d12

Browse files
committed
Squiz/InlineComment: prevent false positives with attributes
PHP 8.0+ attributes can be placed between a docblock and the function/class declaration it applies to. The `Squiz.Commenting.InlineComment` sniff did not yet take this into account when determining whether a comment was a docblock or an single line comment incorrectly using the docblock syntax. This would result in false positive `Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead` errors. Fixed now.
1 parent d5c1cf7 commit 82c9d12

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

src/Standards/Squiz/Sniffs/Commenting/InlineCommentSniff.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,16 @@ public function process(File $phpcsFile, $stackPtr)
5959
// We are only interested in inline doc block comments, which are
6060
// not allowed.
6161
if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_OPEN_TAG) {
62-
$nextToken = $phpcsFile->findNext(
63-
Tokens::$emptyTokens,
64-
($stackPtr + 1),
65-
null,
66-
true
67-
);
62+
$nextToken = $stackPtr;
63+
do {
64+
$nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextToken + 1), null, true);
65+
if ($tokens[$nextToken]['code'] === T_ATTRIBUTE) {
66+
$nextToken = $tokens[$nextToken]['attribute_closer'];
67+
continue;
68+
}
69+
70+
break;
71+
} while (true);
6872

6973
$ignore = [
7074
T_CLASS,

src/Standards/Squiz/Tests/Commenting/InlineCommentUnitTest.inc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,22 @@ if ($foo) {
149149
// another comment here.
150150
$foo++;
151151

152+
/**
153+
* Comment should be ignored, even though there is an attribute between the docblock and the class declaration.
154+
*/
155+
156+
#[AttributeA]
157+
158+
final class MyClass
159+
{
160+
/**
161+
* Comment should be ignored, even though there is an attribute between the docblock and the function declaration
162+
*/
163+
#[AttributeA]
164+
#[AttributeB]
165+
final public function test() {}
166+
}
167+
152168
/*
153169
* N.B.: The below test line must be the last test in the file.
154170
* Testing that a new line after an inline comment when it's the last non-whitespace

src/Standards/Squiz/Tests/Commenting/InlineCommentUnitTest.inc.fixed

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,22 @@ if ($foo) {
142142
// another comment here.
143143
$foo++;
144144

145+
/**
146+
* Comment should be ignored, even though there is an attribute between the docblock and the class declaration.
147+
*/
148+
149+
#[AttributeA]
150+
151+
final class MyClass
152+
{
153+
/**
154+
* Comment should be ignored, even though there is an attribute between the docblock and the function declaration
155+
*/
156+
#[AttributeA]
157+
#[AttributeB]
158+
final public function test() {}
159+
}
160+
145161
/*
146162
* N.B.: The below test line must be the last test in the file.
147163
* Testing that a new line after an inline comment when it's the last non-whitespace

0 commit comments

Comments
 (0)