Skip to content
Snippets Groups Projects
Commit 3c47de1a authored by Henrik Danielsson's avatar Henrik Danielsson
Browse files

Issue #3292573: Fix "missing comment" when using class annotations

parent 70f8c7eb
No related merge requests found
......@@ -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) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment