Skip to content

Commit 34304e3

Browse files
committed
DocCommentSpacingSniff: Fixed fixer
1 parent 5d77d35 commit 34304e3

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

SlevomatCodingStandard/Sniffs/Commenting/DocCommentSpacingSniff.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -567,13 +567,20 @@ private function checkAnnotationsGroupsOrder(
567567
}
568568
}
569569

570-
$endOfLineBeforeFirstAnnotation = TokenHelper::findPreviousContent($phpcsFile, T_DOC_COMMENT_WHITESPACE, $phpcsFile->eolChar, $firstAnnotation->getStartPointer() - 1);
570+
$endOfLineBeforeFirstAnnotation = TokenHelper::findPreviousContent($phpcsFile, T_DOC_COMMENT_WHITESPACE, $phpcsFile->eolChar, $firstAnnotation->getStartPointer() - 1, $docCommentOpenerPointer);
571571
$endOfLineAfterLastAnnotation = TokenHelper::findNextContent($phpcsFile, T_DOC_COMMENT_WHITESPACE, $phpcsFile->eolChar, $lastAnnotation->getEndPointer() + 1);
572572

573573
$phpcsFile->fixer->beginChangeset();
574-
$phpcsFile->fixer->replaceToken($endOfLineBeforeFirstAnnotation + 1, $fixedAnnotations);
575-
for ($i = $endOfLineBeforeFirstAnnotation + 2; $i <= $endOfLineAfterLastAnnotation; $i++) {
576-
$phpcsFile->fixer->replaceToken($i, '');
574+
if ($endOfLineBeforeFirstAnnotation === null) {
575+
$phpcsFile->fixer->replaceToken($docCommentOpenerPointer, '/**' . $phpcsFile->eolChar . $fixedAnnotations);
576+
for ($i = $docCommentOpenerPointer + 1; $i <= $endOfLineAfterLastAnnotation; $i++) {
577+
$phpcsFile->fixer->replaceToken($i, '');
578+
}
579+
} else {
580+
$phpcsFile->fixer->replaceToken($endOfLineBeforeFirstAnnotation + 1, $fixedAnnotations);
581+
for ($i = $endOfLineBeforeFirstAnnotation + 2; $i <= $endOfLineAfterLastAnnotation; $i++) {
582+
$phpcsFile->fixer->replaceToken($i, '');
583+
}
577584
}
578585
$phpcsFile->fixer->endChangeset();
579586
}

tests/Sniffs/Commenting/DocCommentSpacingSniffTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,14 @@ public function testAnnotationsGroupsErrors(): void
134134
DocCommentSpacingSniff::CODE_INCORRECT_ORDER_OF_ANNOTATIONS_IN_GROUP,
135135
]);
136136

137-
self::assertSame(5, $report->getErrorCount());
137+
self::assertSame(6, $report->getErrorCount());
138138

139139
self::assertSniffError($report, 20, DocCommentSpacingSniff::CODE_INCORRECT_ANNOTATIONS_GROUP);
140140
self::assertSniffError($report, 34, DocCommentSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_ANNOTATIONS_GROUPS);
141141
self::assertSniffError($report, 46, DocCommentSpacingSniff::CODE_INCORRECT_ORDER_OF_ANNOTATIONS_GROUPS);
142142
self::assertSniffError($report, 49, DocCommentSpacingSniff::CODE_INCORRECT_ORDER_OF_ANNOTATIONS_IN_GROUP);
143143
self::assertSniffError($report, 62, DocCommentSpacingSniff::CODE_INCORRECT_ORDER_OF_ANNOTATIONS_GROUPS);
144+
self::assertSniffError($report, 83, DocCommentSpacingSniff::CODE_INCORRECT_ANNOTATIONS_GROUP);
144145

145146
self::assertAllFixedInFile($report);
146147
}

tests/Sniffs/Commenting/data/docCommentSpacingAnnotationsGroupsErrors.fixed.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,24 @@ public function oneMoreMethod($a, $b, $c)
6969

7070
}
7171

72+
/**
73+
* @return bool
74+
*
75+
* @param int $a
76+
*/
77+
public function methodBeforeInvalidDocComment($a): bool
78+
{
79+
80+
}
81+
82+
/**
83+
* @param int $a
84+
*
85+
* @return bool
86+
*/
87+
public function methodWithInvalidDocComment($a): bool
88+
{
89+
90+
}
91+
7292
}

tests/Sniffs/Commenting/data/docCommentSpacingAnnotationsGroupsErrors.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,22 @@ public function oneMoreMethod($a, $b, $c)
7070

7171
}
7272

73+
/**
74+
* @return bool
75+
*
76+
* @param int $a
77+
*/
78+
public function methodBeforeInvalidDocComment($a): bool
79+
{
80+
81+
}
82+
83+
/** @return bool
84+
* @param int $a
85+
*/
86+
public function methodWithInvalidDocComment($a): bool
87+
{
88+
89+
}
90+
7391
}

0 commit comments

Comments
 (0)