Skip to content

Commit a13c15e

Browse files
committed
SlevomatCodingStandard.Commenting.DocCommentSpacing: Fixed fixer
1 parent ea34bea commit a13c15e

7 files changed

+104
-36
lines changed

SlevomatCodingStandard/Helpers/AnnotationHelper.php

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,12 @@
2525
use PHPStan\PhpDocParser\Ast\Type\ObjectShapeItemNode;
2626
use PHPStan\PhpDocParser\Ast\Type\ObjectShapeNode;
2727
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
28-
use function array_merge;
2928
use function count;
3029
use function in_array;
3130
use function sprintf;
3231
use function strlen;
3332
use function strtolower;
34-
use const T_DOC_COMMENT_STAR;
3533
use const T_DOC_COMMENT_STRING;
36-
use const T_DOC_COMMENT_WHITESPACE;
3734

3835
/**
3936
* @internal
@@ -356,40 +353,18 @@ private static function getEndPointer(
356353
}
357354
}
358355

359-
$nextPointer = $searchPointer;
360-
while (true) {
361-
$nextPointer = TokenHelper::findNext(
362-
$phpcsFile,
363-
array_merge(TokenHelper::$annotationTokenCodes, [T_DOC_COMMENT_STRING]),
364-
$nextPointer + 1,
365-
$parsedDocComment->getClosePointer()
366-
);
367-
368-
if ($nextPointer === null) {
369-
break;
370-
}
371-
372-
if (in_array($tokens[$nextPointer]['code'], TokenHelper::$annotationTokenCodes, true)) {
373-
break;
374-
}
356+
$nextAnnotationStartPointer = TokenHelper::findNext(
357+
$phpcsFile,
358+
TokenHelper::$annotationTokenCodes,
359+
$searchPointer + 1,
360+
$parsedDocComment->getClosePointer()
361+
);
375362

376-
if (
377-
$tokens[$searchPointer]['line'] + 1 !== $tokens[$nextPointer]['line']
378-
&& (
379-
$tokens[$nextPointer - 1]['code'] === T_DOC_COMMENT_STAR
380-
|| (
381-
$tokens[$nextPointer - 1]['code'] === T_DOC_COMMENT_WHITESPACE
382-
&& strlen($tokens[$nextPointer - 1]['content']) === 1
383-
)
384-
)
385-
) {
386-
break;
387-
}
363+
$pointerAfter = $nextAnnotationStartPointer ?? $parsedDocComment->getClosePointer();
388364

389-
$searchPointer = $nextPointer;
390-
}
365+
$stringPointerBefore = TokenHelper::findPrevious($phpcsFile, T_DOC_COMMENT_STRING, $pointerAfter - 1, $searchPointer);
391366

392-
return $searchPointer;
367+
return $stringPointerBefore ?? $searchPointer;
393368
}
394369

395370
private static function changeAnnotationNode(PhpDocTagNode $tagNode, Node $nodeToChange, Node $changedNode): PhpDocTagNode

tests/Sniffs/Commenting/DocCommentSpacingSniffTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testDefaultSettingsErrors(): void
4646
{
4747
$report = self::checkFile(__DIR__ . '/data/docCommentSpacingDefaultSettingsErrors.php');
4848

49-
self::assertSame(11, $report->getErrorCount());
49+
self::assertSame(12, $report->getErrorCount());
5050

5151
self::assertSniffError($report, 5, DocCommentSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_FIRST_CONTENT);
5252
self::assertSniffError($report, 26, DocCommentSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_FIRST_CONTENT);
@@ -63,6 +63,7 @@ public function testDefaultSettingsErrors(): void
6363

6464
self::assertSniffError($report, 77, DocCommentSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_DIFFERENT_ANNOTATIONS_TYPES);
6565
self::assertSniffError($report, 101, DocCommentSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_DIFFERENT_ANNOTATIONS_TYPES);
66+
self::assertSniffError($report, 120, DocCommentSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_DIFFERENT_ANNOTATIONS_TYPES);
6667

6768
self::assertAllFixedInFile($report);
6869
}
@@ -152,7 +153,7 @@ public function testAnnotationsGroupsErrors(): void
152153
DocCommentSpacingSniff::CODE_INCORRECT_ORDER_OF_ANNOTATIONS_IN_GROUP,
153154
]);
154155

155-
self::assertSame(12, $report->getErrorCount());
156+
self::assertSame(13, $report->getErrorCount());
156157

157158
self::assertSniffError($report, 12, DocCommentSpacingSniff::CODE_INCORRECT_ORDER_OF_ANNOTATIONS_GROUPS);
158159
self::assertSniffError($report, 23, DocCommentSpacingSniff::CODE_INCORRECT_ANNOTATIONS_GROUP);
@@ -166,6 +167,7 @@ public function testAnnotationsGroupsErrors(): void
166167
self::assertSniffError($report, 105, DocCommentSpacingSniff::CODE_INCORRECT_ANNOTATIONS_GROUP);
167168
self::assertSniffError($report, 118, DocCommentSpacingSniff::CODE_INCORRECT_ANNOTATIONS_GROUP);
168169
self::assertSniffError($report, 133, DocCommentSpacingSniff::CODE_INCORRECT_ANNOTATIONS_GROUP);
170+
self::assertSniffError($report, 164, DocCommentSpacingSniff::CODE_INCORRECT_ANNOTATIONS_GROUP);
169171

170172
self::assertAllFixedInFile($report);
171173
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,24 @@ public function method()
165165
{
166166
}
167167

168+
/**
169+
* @dataProvider
170+
*
171+
* @param string $a First line
172+
* Second line
173+
*
174+
* Third line
175+
* Forth line
176+
* @param string $b First line
177+
* Second line
178+
*
179+
* Third line
180+
* Forth line
181+
*
182+
* @return void
183+
*/
184+
public function multiLineAnnotations($a, $b)
185+
{
186+
}
187+
168188
}

tests/Sniffs/Commenting/data/docCommentSpacingAnnotationsGroupsErrors.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,22 @@ public function method()
160160
{
161161
}
162162

163+
/**
164+
* @return void
165+
* @param string $a First line
166+
* Second line
167+
*
168+
* Third line
169+
* Forth line
170+
* @param string $b First line
171+
* Second line
172+
*
173+
* Third line
174+
* Forth line
175+
* @dataProvider
176+
*/
177+
public function multiLineAnnotations($a, $b)
178+
{
179+
}
180+
163181
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,21 @@ public function method()
9090
{
9191
}
9292

93+
/**
94+
* @param string $a First line
95+
* Second line
96+
*
97+
* Third line
98+
* Forth line
99+
* @param string $b First line
100+
* Second line
101+
*
102+
* Third line
103+
* Forth line
104+
* @return void
105+
*/
106+
public function multiLineAnnotations($a, $b)
107+
{
108+
}
109+
93110
}

tests/Sniffs/Commenting/data/docCommentSpacingDefaultSettingsErrors.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,23 @@ public function method()
104104
{
105105
}
106106

107+
/**
108+
* @param string $a First line
109+
* Second line
110+
*
111+
* Third line
112+
* Forth line
113+
* @param string $b First line
114+
* Second line
115+
*
116+
* Third line
117+
* Forth line
118+
*
119+
*
120+
* @return void
121+
*/
122+
public function multiLineAnnotations($a, $b)
123+
{
124+
}
125+
107126
}

tests/Sniffs/Commenting/data/docCommentSpacingDefaultSettingsNoErrors.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,21 @@ public function descriptionAfterAnnotation()
4444
{
4545
}
4646

47+
/**
48+
* @param string $a First line
49+
* Second line
50+
*
51+
* Third line
52+
* Forth line
53+
* @param string $b First line
54+
* Second line
55+
*
56+
* Third line
57+
* Forth line
58+
* @return void
59+
*/
60+
public function multiLineAnnotations($a, $b)
61+
{
62+
}
63+
4764
}

0 commit comments

Comments
 (0)