From 37e5b0bfb5c6e4187e7cc06b784c2c9f7a15b6d6 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 27 Feb 2018 13:43:13 +0100 Subject: [PATCH] Squiz/BlockComment: fix issue with PHPCS annotations causing the comment closer to be misidentified. Whether something was identified as a comment line was based on the token code being the same. This did not allow for PHPCS annotations to be included in block comments as those have a different token code. This resulted in comments being "cut short", i.e. the line above a PHPCS annotation being seen as the last line in a comment and subsequent comment lines being ignored, resulting in incorrect `Comment closer must be on a new line` and `Empty line required after block comment` notices. Fixes 1918 --- .../Squiz/Sniffs/Commenting/BlockCommentSniff.php | 4 +++- .../Squiz/Tests/Commenting/BlockCommentUnitTest.inc | 11 +++++++++++ .../Tests/Commenting/BlockCommentUnitTest.inc.fixed | 11 +++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Standards/Squiz/Sniffs/Commenting/BlockCommentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/BlockCommentSniff.php index a0ab309b54..edf4a7fe61 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/BlockCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/BlockCommentSniff.php @@ -118,7 +118,9 @@ public function process(File $phpcsFile, $stackPtr) // Construct the comment into an array. while (($nextComment = $phpcsFile->findNext(T_WHITESPACE, ($nextComment + 1), null, true)) !== false) { - if ($tokens[$nextComment]['code'] !== $tokens[$stackPtr]['code']) { + if ($tokens[$nextComment]['code'] !== $tokens[$stackPtr]['code'] + && isset(Tokens::$phpcsCommentTokens[$tokens[$nextComment]['code']]) === false + ) { // Found the next bit of code. break; } diff --git a/src/Standards/Squiz/Tests/Commenting/BlockCommentUnitTest.inc b/src/Standards/Squiz/Tests/Commenting/BlockCommentUnitTest.inc index e33d609a46..709b5f6ae4 100644 --- a/src/Standards/Squiz/Tests/Commenting/BlockCommentUnitTest.inc +++ b/src/Standards/Squiz/Tests/Commenting/BlockCommentUnitTest.inc @@ -231,3 +231,14 @@ class TabTest { // Mid-line inline comment style should not be auto-fixed. if (true || /* test */ -1 == $b) {} $y = 10 + /* test */ -2; + +/* + * When the comment contains PHPCS annotations, the comment closer was being misidentified. + * phpcs:disable Standard.Category.Sniff + */ + +/* + * When the comment contains PHPCS annotations, the comment closer was being misidentified. + * See: {@link https://github.com/squizlabs/PHP_CodeSniffer/issues/1918} + * @phpcs:disable Standard.Category.Sniff + */ diff --git a/src/Standards/Squiz/Tests/Commenting/BlockCommentUnitTest.inc.fixed b/src/Standards/Squiz/Tests/Commenting/BlockCommentUnitTest.inc.fixed index f1d86214a5..8038ecb97b 100644 --- a/src/Standards/Squiz/Tests/Commenting/BlockCommentUnitTest.inc.fixed +++ b/src/Standards/Squiz/Tests/Commenting/BlockCommentUnitTest.inc.fixed @@ -233,3 +233,14 @@ class TabTest { // Mid-line inline comment style should not be auto-fixed. if (true || /* test */ -1 == $b) {} $y = 10 + /* test */ -2; + +/* + * When the comment contains PHPCS annotations, the comment closer was being misidentified. + * phpcs:disable Standard.Category.Sniff + */ + +/* + * When the comment contains PHPCS annotations, the comment closer was being misidentified. + * See: {@link https://github.com/squizlabs/PHP_CodeSniffer/issues/1918} + * @phpcs:disable Standard.Category.Sniff + */