From 9397de6586507f5146bf5aa37c9e9761017c7cd9 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 24 Sep 2023 12:11:50 +0200 Subject: [PATCH] Generic/DocComment: bug fix - don't remove ignore annotations when fixing The code sample included in the tests would previously result in an "There must be a single blank line after a tag group" error, even though there _is_ a blank line after the `@codeCoverageIgnore` tag. The auto-fixer would subsequently fix this by removing the `@phpcs:disable` comment + the blank line after it. Fixed now. Includes test. --- CHANGELOG.md | 2 ++ .../Generic/Sniffs/Commenting/DocCommentSniff.php | 8 ++++++-- .../Generic/Tests/Commenting/DocCommentUnitTest.inc | 10 ++++++++++ .../Tests/Commenting/DocCommentUnitTest.inc.fixed | 10 ++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7aa67818a0..4ab0156be0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -155,6 +155,8 @@ The file documents changes to the PHP_CodeSniffer project. - Thanks to Juliette Reinders Folmer (@jrfnl) for the patch - Fixed bug #3877 : Filter names can be case-sensitive. The -h help text will now display the correct case for the available filters - Thanks to @simonsan for the patch +- Fixed bug #3893 : Generic/DocComment : the SpacingAfterTagGroup fixer could accidentally remove ignore annotations + - Thanks to Juliette Reinders Folmer (@jrfnl) for the patch - Fixed bug #3906 : Tokenizer/CSS: fixed a bug related to the unsupported slash comment syntax - Thanks to Dan Wallis (@fredden) for the patch diff --git a/src/Standards/Generic/Sniffs/Commenting/DocCommentSniff.php b/src/Standards/Generic/Sniffs/Commenting/DocCommentSniff.php index 1e96e64b75..62c08cea10 100644 --- a/src/Standards/Generic/Sniffs/Commenting/DocCommentSniff.php +++ b/src/Standards/Generic/Sniffs/Commenting/DocCommentSniff.php @@ -11,6 +11,7 @@ use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Util\Tokens; class DocCommentSniff implements Sniff { @@ -280,9 +281,12 @@ public function process(File $phpcsFile, $stackPtr) } // Check that there was single blank line after the tag block - // but account for a multi-line tag comments. + // but account for multi-line tag comments. + $find = Tokens::$phpcsCommentTokens; + $find[T_DOC_COMMENT_TAG] = T_DOC_COMMENT_TAG; + $lastTag = $group[$pos]; - $next = $phpcsFile->findNext(T_DOC_COMMENT_TAG, ($lastTag + 3), $commentEnd); + $next = $phpcsFile->findNext($find, ($lastTag + 3), $commentEnd); if ($next !== false) { $prev = $phpcsFile->findPrevious([T_DOC_COMMENT_TAG, T_DOC_COMMENT_STRING], ($next - 1), $commentStart); if ($tokens[$next]['line'] !== ($tokens[$prev]['line'] + 2)) { diff --git a/src/Standards/Generic/Tests/Commenting/DocCommentUnitTest.inc b/src/Standards/Generic/Tests/Commenting/DocCommentUnitTest.inc index 8824166722..bcd8256d7b 100644 --- a/src/Standards/Generic/Tests/Commenting/DocCommentUnitTest.inc +++ b/src/Standards/Generic/Tests/Commenting/DocCommentUnitTest.inc @@ -249,4 +249,14 @@ * @link http://pear.php.net/package/PHP_CodeSniffer */ +/** + * Do something. + * + * @codeCoverageIgnore + * + * @phpcs:disable Stnd.Cat.SniffName + * + * @return void + */ + /** No docblock close tag. Must be last test without new line. \ No newline at end of file diff --git a/src/Standards/Generic/Tests/Commenting/DocCommentUnitTest.inc.fixed b/src/Standards/Generic/Tests/Commenting/DocCommentUnitTest.inc.fixed index 46b4cd05bb..7da1c8ae8e 100644 --- a/src/Standards/Generic/Tests/Commenting/DocCommentUnitTest.inc.fixed +++ b/src/Standards/Generic/Tests/Commenting/DocCommentUnitTest.inc.fixed @@ -254,4 +254,14 @@ * @link http://pear.php.net/package/PHP_CodeSniffer */ +/** + * Do something. + * + * @codeCoverageIgnore + * + * @phpcs:disable Stnd.Cat.SniffName + * + * @return void + */ + /** No docblock close tag. Must be last test without new line. \ No newline at end of file