Skip to content

Commit 4bd90b5

Browse files
committed
SlevomatCodingStandard.Commenting.DocCommentSpacing: Fixed internal error
1 parent 91f6a5d commit 4bd90b5

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

SlevomatCodingStandard/Sniffs/Commenting/DocCommentSpacingSniff.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use function strpos;
3131
use function substr;
3232
use function substr_count;
33+
use function trim;
3334
use function uasort;
3435
use function usort;
3536
use const T_DOC_COMMENT_OPEN_TAG;
@@ -608,35 +609,43 @@ private function checkAnnotationsGroupsOrder(
608609
$fixedAnnotations .= sprintf(
609610
'%s * %s%s',
610611
$indentation,
611-
TokenHelper::getContent($phpcsFile, $sortedAnnotation->getStartPointer(), $sortedAnnotation->getEndPointer()),
612+
trim(TokenHelper::getContent($phpcsFile, $sortedAnnotation->getStartPointer(), $sortedAnnotation->getEndPointer())),
612613
$phpcsFile->eolChar
613614
);
614615
}
615616
}
616617

618+
$tokens = $phpcsFile->getTokens();
619+
$docCommentCloserPointer = $tokens[$docCommentOpenerPointer]['comment_closer'];
620+
617621
$endOfLineBeforeFirstAnnotation = TokenHelper::findPreviousContent(
618622
$phpcsFile,
619623
T_DOC_COMMENT_WHITESPACE,
620624
$phpcsFile->eolChar,
621625
$firstAnnotation->getStartPointer() - 1,
622626
$docCommentOpenerPointer
623627
);
624-
$endOfLineAfterLastAnnotation = TokenHelper::findNextContent(
628+
$docCommentContentEndPointer = TokenHelper::findNextContent(
625629
$phpcsFile,
626630
T_DOC_COMMENT_WHITESPACE,
627631
$phpcsFile->eolChar,
628-
$lastAnnotation->getEndPointer() + 1
632+
$lastAnnotation->getEndPointer() + 1,
633+
$docCommentCloserPointer
629634
);
630635

636+
if ($docCommentContentEndPointer === null) {
637+
$docCommentContentEndPointer = $lastAnnotation->getEndPointer();
638+
}
639+
631640
$phpcsFile->fixer->beginChangeset();
632641
if ($endOfLineBeforeFirstAnnotation === null) {
633642
$phpcsFile->fixer->replaceToken($docCommentOpenerPointer, '/**' . $phpcsFile->eolChar . $fixedAnnotations);
634-
for ($i = $docCommentOpenerPointer + 1; $i <= $endOfLineAfterLastAnnotation; $i++) {
643+
for ($i = $docCommentOpenerPointer + 1; $i <= $docCommentContentEndPointer; $i++) {
635644
$phpcsFile->fixer->replaceToken($i, '');
636645
}
637646
} else {
638647
$phpcsFile->fixer->replaceToken($endOfLineBeforeFirstAnnotation + 1, $fixedAnnotations);
639-
for ($i = $endOfLineBeforeFirstAnnotation + 2; $i <= $endOfLineAfterLastAnnotation; $i++) {
648+
for ($i = $endOfLineBeforeFirstAnnotation + 2; $i <= $docCommentContentEndPointer; $i++) {
640649
$phpcsFile->fixer->replaceToken($i, '');
641650
}
642651
}

tests/Sniffs/Commenting/DocCommentSpacingSniffTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function testAnnotationsGroupsErrors(): void
147147
DocCommentSpacingSniff::CODE_INCORRECT_ORDER_OF_ANNOTATIONS_IN_GROUP,
148148
]);
149149

150-
self::assertSame(9, $report->getErrorCount());
150+
self::assertSame(10, $report->getErrorCount());
151151

152152
self::assertSniffError($report, 20, DocCommentSpacingSniff::CODE_INCORRECT_ANNOTATIONS_GROUP);
153153
self::assertSniffError($report, 34, DocCommentSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_ANNOTATIONS_GROUPS);
@@ -158,6 +158,7 @@ public function testAnnotationsGroupsErrors(): void
158158
self::assertSniffError($report, 83, DocCommentSpacingSniff::CODE_INCORRECT_ANNOTATIONS_GROUP);
159159
self::assertSniffError($report, 92, DocCommentSpacingSniff::CODE_INCORRECT_ANNOTATIONS_GROUP);
160160
self::assertSniffError($report, 102, DocCommentSpacingSniff::CODE_INCORRECT_ANNOTATIONS_GROUP);
161+
self::assertSniffError($report, 115, DocCommentSpacingSniff::CODE_INCORRECT_ANNOTATIONS_GROUP);
161162

162163
self::assertAllFixedInFile($report);
163164
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,14 @@ public function phpstanAndPhpcsAnnotations($a)
113113
return false;
114114
}
115115

116+
/**
117+
* @phpstan-return array<string, string>
118+
*
119+
* @return array<string, string>
120+
*/
121+
public function phpstanReturnAndReturnAnnotations()
122+
{
123+
return [];
124+
}
125+
116126
}

tests/Sniffs/Commenting/data/docCommentSpacingAnnotationsGroupsErrors.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,11 @@ public function phpstanAndPhpcsAnnotations($a)
112112
return false;
113113
}
114114

115+
/** @phpstan-return array<string, string>
116+
* @return array<string, string> */
117+
public function phpstanReturnAndReturnAnnotations()
118+
{
119+
return [];
120+
}
121+
115122
}

0 commit comments

Comments
 (0)