diff --git a/SlevomatCodingStandard/Helpers/AnnotationHelper.php b/SlevomatCodingStandard/Helpers/AnnotationHelper.php index d90c2330e..d015f9566 100644 --- a/SlevomatCodingStandard/Helpers/AnnotationHelper.php +++ b/SlevomatCodingStandard/Helpers/AnnotationHelper.php @@ -25,15 +25,12 @@ use PHPStan\PhpDocParser\Ast\Type\ObjectShapeItemNode; use PHPStan\PhpDocParser\Ast\Type\ObjectShapeNode; use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; -use function array_merge; use function count; use function in_array; use function sprintf; use function strlen; use function strtolower; -use const T_DOC_COMMENT_STAR; use const T_DOC_COMMENT_STRING; -use const T_DOC_COMMENT_WHITESPACE; /** * @internal @@ -356,40 +353,18 @@ private static function getEndPointer( } } - $nextPointer = $searchPointer; - while (true) { - $nextPointer = TokenHelper::findNext( - $phpcsFile, - array_merge(TokenHelper::$annotationTokenCodes, [T_DOC_COMMENT_STRING]), - $nextPointer + 1, - $parsedDocComment->getClosePointer() - ); - - if ($nextPointer === null) { - break; - } - - if (in_array($tokens[$nextPointer]['code'], TokenHelper::$annotationTokenCodes, true)) { - break; - } + $nextAnnotationStartPointer = TokenHelper::findNext( + $phpcsFile, + TokenHelper::$annotationTokenCodes, + $searchPointer + 1, + $parsedDocComment->getClosePointer() + ); - if ( - $tokens[$searchPointer]['line'] + 1 !== $tokens[$nextPointer]['line'] - && ( - $tokens[$nextPointer - 1]['code'] === T_DOC_COMMENT_STAR - || ( - $tokens[$nextPointer - 1]['code'] === T_DOC_COMMENT_WHITESPACE - && strlen($tokens[$nextPointer - 1]['content']) === 1 - ) - ) - ) { - break; - } + $pointerAfter = $nextAnnotationStartPointer ?? $parsedDocComment->getClosePointer(); - $searchPointer = $nextPointer; - } + $stringPointerBefore = TokenHelper::findPrevious($phpcsFile, T_DOC_COMMENT_STRING, $pointerAfter - 1, $searchPointer); - return $searchPointer; + return $stringPointerBefore ?? $searchPointer; } private static function changeAnnotationNode(PhpDocTagNode $tagNode, Node $nodeToChange, Node $changedNode): PhpDocTagNode diff --git a/tests/Sniffs/Commenting/DocCommentSpacingSniffTest.php b/tests/Sniffs/Commenting/DocCommentSpacingSniffTest.php index da21eb572..64a4ecc42 100644 --- a/tests/Sniffs/Commenting/DocCommentSpacingSniffTest.php +++ b/tests/Sniffs/Commenting/DocCommentSpacingSniffTest.php @@ -46,7 +46,7 @@ public function testDefaultSettingsErrors(): void { $report = self::checkFile(__DIR__ . '/data/docCommentSpacingDefaultSettingsErrors.php'); - self::assertSame(11, $report->getErrorCount()); + self::assertSame(12, $report->getErrorCount()); self::assertSniffError($report, 5, DocCommentSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_FIRST_CONTENT); self::assertSniffError($report, 26, DocCommentSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_FIRST_CONTENT); @@ -63,6 +63,7 @@ public function testDefaultSettingsErrors(): void self::assertSniffError($report, 77, DocCommentSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_DIFFERENT_ANNOTATIONS_TYPES); self::assertSniffError($report, 101, DocCommentSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_DIFFERENT_ANNOTATIONS_TYPES); + self::assertSniffError($report, 120, DocCommentSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_DIFFERENT_ANNOTATIONS_TYPES); self::assertAllFixedInFile($report); } @@ -152,7 +153,7 @@ public function testAnnotationsGroupsErrors(): void DocCommentSpacingSniff::CODE_INCORRECT_ORDER_OF_ANNOTATIONS_IN_GROUP, ]); - self::assertSame(12, $report->getErrorCount()); + self::assertSame(13, $report->getErrorCount()); self::assertSniffError($report, 12, DocCommentSpacingSniff::CODE_INCORRECT_ORDER_OF_ANNOTATIONS_GROUPS); self::assertSniffError($report, 23, DocCommentSpacingSniff::CODE_INCORRECT_ANNOTATIONS_GROUP); @@ -166,6 +167,7 @@ public function testAnnotationsGroupsErrors(): void self::assertSniffError($report, 105, DocCommentSpacingSniff::CODE_INCORRECT_ANNOTATIONS_GROUP); self::assertSniffError($report, 118, DocCommentSpacingSniff::CODE_INCORRECT_ANNOTATIONS_GROUP); self::assertSniffError($report, 133, DocCommentSpacingSniff::CODE_INCORRECT_ANNOTATIONS_GROUP); + self::assertSniffError($report, 164, DocCommentSpacingSniff::CODE_INCORRECT_ANNOTATIONS_GROUP); self::assertAllFixedInFile($report); } diff --git a/tests/Sniffs/Commenting/data/docCommentSpacingAnnotationsGroupsErrors.fixed.php b/tests/Sniffs/Commenting/data/docCommentSpacingAnnotationsGroupsErrors.fixed.php index 5d17da849..96c5381bd 100644 --- a/tests/Sniffs/Commenting/data/docCommentSpacingAnnotationsGroupsErrors.fixed.php +++ b/tests/Sniffs/Commenting/data/docCommentSpacingAnnotationsGroupsErrors.fixed.php @@ -165,4 +165,24 @@ public function method() { } + /** + * @dataProvider + * + * @param string $a First line + * Second line + * + * Third line + * Forth line + * @param string $b First line + * Second line + * + * Third line + * Forth line + * + * @return void + */ + public function multiLineAnnotations($a, $b) + { + } + } diff --git a/tests/Sniffs/Commenting/data/docCommentSpacingAnnotationsGroupsErrors.php b/tests/Sniffs/Commenting/data/docCommentSpacingAnnotationsGroupsErrors.php index 0fb900685..8faf82e68 100644 --- a/tests/Sniffs/Commenting/data/docCommentSpacingAnnotationsGroupsErrors.php +++ b/tests/Sniffs/Commenting/data/docCommentSpacingAnnotationsGroupsErrors.php @@ -160,4 +160,22 @@ public function method() { } + /** + * @return void + * @param string $a First line + * Second line + * + * Third line + * Forth line + * @param string $b First line + * Second line + * + * Third line + * Forth line + * @dataProvider + */ + public function multiLineAnnotations($a, $b) + { + } + } diff --git a/tests/Sniffs/Commenting/data/docCommentSpacingDefaultSettingsErrors.fixed.php b/tests/Sniffs/Commenting/data/docCommentSpacingDefaultSettingsErrors.fixed.php index dac114de1..a7037005a 100644 --- a/tests/Sniffs/Commenting/data/docCommentSpacingDefaultSettingsErrors.fixed.php +++ b/tests/Sniffs/Commenting/data/docCommentSpacingDefaultSettingsErrors.fixed.php @@ -90,4 +90,21 @@ public function method() { } + /** + * @param string $a First line + * Second line + * + * Third line + * Forth line + * @param string $b First line + * Second line + * + * Third line + * Forth line + * @return void + */ + public function multiLineAnnotations($a, $b) + { + } + } diff --git a/tests/Sniffs/Commenting/data/docCommentSpacingDefaultSettingsErrors.php b/tests/Sniffs/Commenting/data/docCommentSpacingDefaultSettingsErrors.php index f636fe1aa..f5597470b 100644 --- a/tests/Sniffs/Commenting/data/docCommentSpacingDefaultSettingsErrors.php +++ b/tests/Sniffs/Commenting/data/docCommentSpacingDefaultSettingsErrors.php @@ -104,4 +104,23 @@ public function method() { } + /** + * @param string $a First line + * Second line + * + * Third line + * Forth line + * @param string $b First line + * Second line + * + * Third line + * Forth line + * + * + * @return void + */ + public function multiLineAnnotations($a, $b) + { + } + } diff --git a/tests/Sniffs/Commenting/data/docCommentSpacingDefaultSettingsNoErrors.php b/tests/Sniffs/Commenting/data/docCommentSpacingDefaultSettingsNoErrors.php index b47c83ebe..0a187fd05 100644 --- a/tests/Sniffs/Commenting/data/docCommentSpacingDefaultSettingsNoErrors.php +++ b/tests/Sniffs/Commenting/data/docCommentSpacingDefaultSettingsNoErrors.php @@ -44,4 +44,21 @@ public function descriptionAfterAnnotation() { } + /** + * @param string $a First line + * Second line + * + * Third line + * Forth line + * @param string $b First line + * Second line + * + * Third line + * Forth line + * @return void + */ + public function multiLineAnnotations($a, $b) + { + } + }