diff --git a/src/Standards/Squiz/Sniffs/Commenting/PostStatementCommentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/PostStatementCommentSniff.php index 9b36abe76f..82b934a792 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/PostStatementCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/PostStatementCommentSniff.php @@ -109,6 +109,14 @@ public function process(File $phpcsFile, $stackPtr) } } + if ($phpcsFile->tokenizerType === 'PHP' + && preg_match('|^//[ \t]*@[^\s]+|', $tokens[$stackPtr]['content']) === 1 + ) { + $error = 'Annotations may not appear after statements'; + $phpcsFile->addError($error, $stackPtr, 'AnnotationFound'); + return; + } + $error = 'Comments may not appear after statements'; $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found'); if ($fix === true) { diff --git a/src/Standards/Squiz/Tests/Commenting/PostStatementCommentUnitTest.inc b/src/Standards/Squiz/Tests/Commenting/PostStatementCommentUnitTest.inc index b83469613f..3374c4760a 100644 --- a/src/Standards/Squiz/Tests/Commenting/PostStatementCommentUnitTest.inc +++ b/src/Standards/Squiz/Tests/Commenting/PostStatementCommentUnitTest.inc @@ -52,3 +52,13 @@ $match = match($foo // comment ) { 1 => 1, // comment }; + +// Issue #560: Annotations should be reported separately and be non-auto-fixable as their meaning may change when moved. +$a = 1; //@codeCoverageIgnore +$b = 2; // @phpstan-ignore variable.undefined +$c = 3; // @phpstan-ignore variable.undefined +$d = 4; // @tabInsteadOfSpace + +// Comments that include `@`, but are not recognized as annotations by this sniff. +$a = 1; // @ = add tag. +$b = 2; // Some comment. // @username diff --git a/src/Standards/Squiz/Tests/Commenting/PostStatementCommentUnitTest.inc.fixed b/src/Standards/Squiz/Tests/Commenting/PostStatementCommentUnitTest.inc.fixed index 21a4bbe032..bd6b171b04 100644 --- a/src/Standards/Squiz/Tests/Commenting/PostStatementCommentUnitTest.inc.fixed +++ b/src/Standards/Squiz/Tests/Commenting/PostStatementCommentUnitTest.inc.fixed @@ -57,3 +57,15 @@ $match = match($foo // comment 1 => 1, // comment }; + +// Issue #560: Annotations should be reported separately and be non-auto-fixable as their meaning may change when moved. +$a = 1; //@codeCoverageIgnore +$b = 2; // @phpstan-ignore variable.undefined +$c = 3; // @phpstan-ignore variable.undefined +$d = 4; // @tabInsteadOfSpace + +// Comments that include `@`, but are not recognized as annotations by this sniff. +$a = 1; +// @ = add tag. +$b = 2; +// Some comment. // @username diff --git a/src/Standards/Squiz/Tests/Commenting/PostStatementCommentUnitTest.php b/src/Standards/Squiz/Tests/Commenting/PostStatementCommentUnitTest.php index b9751f6147..ba3b1c7222 100644 --- a/src/Standards/Squiz/Tests/Commenting/PostStatementCommentUnitTest.php +++ b/src/Standards/Squiz/Tests/Commenting/PostStatementCommentUnitTest.php @@ -40,6 +40,12 @@ public function getErrorList($testFile='') 18 => 1, 35 => 1, 53 => 1, + 57 => 1, + 58 => 1, + 59 => 1, + 60 => 1, + 63 => 1, + 64 => 1, ]; case 'PostStatementCommentUnitTest.1.js':