Skip to content

Commit f6053a3

Browse files
committed
Squiz/PostStatementComment: use a different error code for annotations
This commit changes `Squiz.Commenting.PostStatementComment` to use a different error code when an annotation is found. This will allow ruleset maintainers to selectively exclude the flagging of trailing annotations from their ruleset. For example, for PHPCS itself, this will make it possible to use the `// @codeCoverageIgnore` annotation where needed. An annotation is defined as any comment that starts with an optional space, followed by a `@` and any character, as long as it is not a whitespace. For now, only `//` comments are supported as this is the only type of comment supported by this sniff. This change only applies to PHP code as support for JS will be dropped soon, and JS doesn't seem to follow the same convention of annotations starting with `@`.
1 parent 2e3c58b commit f6053a3

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ public function process(File $phpcsFile, $stackPtr)
109109
}
110110
}
111111

112+
if ($phpcsFile->tokenizerType === 'PHP'
113+
&& preg_match('|//\s?@[^\s]+|', $tokens[$stackPtr]['content']) === 1
114+
) {
115+
$error = 'Annotations may not appear after statements';
116+
$phpcsFile->addError($error, $stackPtr, 'AnnotationFound');
117+
return;
118+
}
119+
112120
$error = 'Comments may not appear after statements';
113121
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found');
114122
if ($fix === true) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,7 @@ $match = match($foo // comment
5252
) {
5353
1 => 1, // comment
5454
};
55+
56+
$a = 1; // @codeCoverageIgnore
57+
$b = 2; //@phpstan-ignore variable.undefined
58+
$c = 3; // @thisIsNotAnAnnotation - more than one space before the @ symbol.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,8 @@ $match = match($foo // comment
5757
1 => 1,
5858
// comment
5959
};
60+
61+
$a = 1; // @codeCoverageIgnore
62+
$b = 2; //@phpstan-ignore variable.undefined
63+
$c = 3;
64+
// @thisIsNotAnAnnotation - more than one space before the @ symbol.

src/Standards/Squiz/Tests/Commenting/PostStatementCommentUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public function getErrorList($testFile='')
4040
18 => 1,
4141
35 => 1,
4242
53 => 1,
43+
56 => 1,
44+
57 => 1,
45+
58 => 1,
4346
];
4447

4548
case 'PostStatementCommentUnitTest.1.js':

0 commit comments

Comments
 (0)