From 3c47de1a7f30eca18a9d7468460098cbda085f54 Mon Sep 17 00:00:00 2001 From: TwoD <twod@244227.no-reply.drupal.org> Date: Sat, 20 Aug 2022 17:35:18 +0000 Subject: [PATCH] Issue #3292573: Fix "missing comment" when using class annotations --- .../Sniffs/Commenting/ClassCommentSniff.php | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/coder_sniffer/Drupal/Sniffs/Commenting/ClassCommentSniff.php b/coder_sniffer/Drupal/Sniffs/Commenting/ClassCommentSniff.php index 8dfc7413..ca77626b 100644 --- a/coder_sniffer/Drupal/Sniffs/Commenting/ClassCommentSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Commenting/ClassCommentSniff.php @@ -62,6 +62,18 @@ class ClassCommentSniff implements Sniff $name = $tokens[$stackPtr]['content']; $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true); + $attributeStart = -1; + $attributeEnd = -1; + while ($tokens[$commentEnd]['code'] === T_ATTRIBUTE_END) { + if ($attributeEnd === -1) { + $attributeEnd = $commentEnd; + } + $commentEnd = $phpcsFile->findPrevious(T_ATTRIBUTE, ($commentEnd - 1), null); + if ($attributeStart === -1 || $attributeStart > $commentEnd) { + $attributeStart = $commentEnd; + } + $commentEnd = $phpcsFile->findPrevious($find, ($commentEnd - 1), null, true); + } if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG && $tokens[$commentEnd]['code'] !== T_COMMENT ) { @@ -109,12 +121,13 @@ class ClassCommentSniff implements Sniff return; } - if ($tokens[$commentEnd]['line'] !== ($tokens[$stackPtr]['line'] - 1)) { + $target = $attributeStart !== -1 ? $attributeStart : $stackPtr; + if (($tokens[$commentEnd]['line'] !== $tokens[$target]['line'] - 1)) { $error = 'There must be exactly one newline after the %s comment'; $fix = $phpcsFile->addFixableError($error, $commentEnd, 'SpacingAfter', [$name]); if ($fix === true) { $phpcsFile->fixer->beginChangeset(); - for ($i = ($commentEnd + 1); $tokens[$i]['code'] === T_WHITESPACE && $i < $stackPtr; $i++) { + for ($i = ($commentEnd + 1); $tokens[$i]['code'] === T_WHITESPACE && $i < $target; $i++) { $phpcsFile->fixer->replaceToken($i, ''); } @@ -123,6 +136,20 @@ class ClassCommentSniff implements Sniff } } + if ($attributeEnd !== -1 && ($tokens[$attributeEnd]['line'] !== $tokens[$stackPtr]['line'] - 1)) { + $error = 'There must be exactly one newline after the %s attribute'; + $fix = $phpcsFile->addFixableError($error, $attributeEnd, 'SpacingAfter', [$name]); + if ($fix === true) { + $phpcsFile->fixer->beginChangeset(); + for ($i = ($attributeEnd + 1); $tokens[$i]['code'] === T_WHITESPACE && $i < $stackPtr; $i++) { + $phpcsFile->fixer->replaceToken($i, ''); + } + + $phpcsFile->fixer->addContent($attributeEnd, "\n"); + $phpcsFile->fixer->endChangeset(); + } + } + $comment = []; for ($i = $start; $i < $commentEnd; $i++) { if ($tokens[$i]['code'] === T_DOC_COMMENT_TAG) { -- GitLab