Skip to content

Commit

Permalink
SlevomatCodingStandard.Commenting.DocCommentSpacing: Fixed fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Jun 25, 2023
1 parent ea34bea commit a13c15e
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 36 deletions.
43 changes: 9 additions & 34 deletions SlevomatCodingStandard/Helpers/AnnotationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions tests/Sniffs/Commenting/DocCommentSpacingSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

}

0 comments on commit a13c15e

Please sign in to comment.