Skip to content

Commit cad6b10

Browse files
committed
DocCommentSpacingSniff: Fixed support for @phpstan and @phpcs annotations
1 parent b99a3e1 commit cad6b10

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

SlevomatCodingStandard/Sniffs/Commenting/DocCommentSpacingSniff.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use function asort;
2222
use function count;
2323
use function explode;
24+
use function in_array;
2425
use function ksort;
2526
use function max;
2627
use function preg_match;
@@ -634,17 +635,14 @@ private function sortAnnotationsToGroups(array $annotations): array
634635

635636
foreach (array_keys($sortedAnnotationsGroups) as $annotationsGroupPosition) {
636637
$expectedAnnotationsGroupOrder = array_flip($expectedAnnotationsGroups[$annotationsGroupPosition]);
637-
usort($sortedAnnotationsGroups[$annotationsGroupPosition], static function (Annotation $firstAnnotation, Annotation $secondAnnotation) use ($expectedAnnotationsGroupOrder): int {
638-
$getExpectedOrder = static function (string $annotationName) use ($expectedAnnotationsGroupOrder): int {
638+
usort($sortedAnnotationsGroups[$annotationsGroupPosition], function (Annotation $firstAnnotation, Annotation $secondAnnotation) use ($expectedAnnotationsGroupOrder): int {
639+
$getExpectedOrder = function (string $annotationName) use ($expectedAnnotationsGroupOrder): int {
639640
if (array_key_exists($annotationName, $expectedAnnotationsGroupOrder)) {
640641
return $expectedAnnotationsGroupOrder[$annotationName];
641642
}
642643

643644
foreach ($expectedAnnotationsGroupOrder as $expectedAnnotationName => $expectedAnnotationOrder) {
644-
if (
645-
substr($expectedAnnotationName, -1) === '\\'
646-
&& strpos($annotationName, $expectedAnnotationName) === 0
647-
) {
645+
if ($this->isAnnotationNameInAnnotationNamespace($expectedAnnotationName, $annotationName)) {
648646
return $expectedAnnotationOrder;
649647
}
650648
}
@@ -665,13 +663,18 @@ private function sortAnnotationsToGroups(array $annotations): array
665663
return $sortedAnnotationsGroups;
666664
}
667665

666+
private function isAnnotationNameInAnnotationNamespace(string $annotationNamespace, string $annotationName): bool
667+
{
668+
return in_array(substr($annotationNamespace, -1), ['\\', '-', ':'], true) && strpos($annotationName, $annotationNamespace) === 0;
669+
}
670+
668671
private function isAnnotationMatched(Annotation $annotation, string $annotationName): bool
669672
{
670673
if ($annotation->getName() === $annotationName) {
671674
return true;
672675
}
673676

674-
return substr($annotationName, -1) === '\\' && strpos($annotation->getName(), $annotationName) === 0;
677+
return $this->isAnnotationNameInAnnotationNamespace($annotationName, $annotation->getName());
675678
}
676679

677680
private function checkLinesAfterLastContent(

tests/Sniffs/Commenting/DocCommentSpacingSniffTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ public function testAnnotationsGroupsErrors(): void
137137
'@link,@todo',
138138
'@whatever',
139139
'@anything',
140+
'@phpstan-param,@phpstan-return,@phpstan-',
141+
'@phpcs:',
140142
],
141143
], [
142144
DocCommentSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_ANNOTATIONS_GROUPS,
@@ -145,7 +147,7 @@ public function testAnnotationsGroupsErrors(): void
145147
DocCommentSpacingSniff::CODE_INCORRECT_ORDER_OF_ANNOTATIONS_IN_GROUP,
146148
]);
147149

148-
self::assertSame(8, $report->getErrorCount());
150+
self::assertSame(9, $report->getErrorCount());
149151

150152
self::assertSniffError($report, 20, DocCommentSpacingSniff::CODE_INCORRECT_ANNOTATIONS_GROUP);
151153
self::assertSniffError($report, 34, DocCommentSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_ANNOTATIONS_GROUPS);
@@ -155,6 +157,7 @@ public function testAnnotationsGroupsErrors(): void
155157
self::assertSniffError($report, 74, DocCommentSpacingSniff::CODE_INCORRECT_ORDER_OF_ANNOTATIONS_GROUPS);
156158
self::assertSniffError($report, 83, DocCommentSpacingSniff::CODE_INCORRECT_ANNOTATIONS_GROUP);
157159
self::assertSniffError($report, 92, DocCommentSpacingSniff::CODE_INCORRECT_ANNOTATIONS_GROUP);
160+
self::assertSniffError($report, 102, DocCommentSpacingSniff::CODE_INCORRECT_ANNOTATIONS_GROUP);
158161

159162
self::assertAllFixedInFile($report);
160163
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,19 @@ public function twoUndefinedAnnotations()
9898

9999
}
100100

101+
/**
102+
* @param int $a
103+
*
104+
* @phpstan-param int $a
105+
* @phpstan-return bool
106+
* @phpstan-whatever X
107+
*
108+
* @phpcs:disable
109+
* @phpcs:enable
110+
*/
111+
public function phpstanAndPhpcsAnnotations($a)
112+
{
113+
return false;
114+
}
115+
101116
}

tests/Sniffs/Commenting/data/docCommentSpacingAnnotationsGroupsErrors.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,18 @@ public function twoUndefinedAnnotations()
9898

9999
}
100100

101+
/**
102+
* @phpstan-whatever X
103+
* @phpcs:disable
104+
* @phpstan-param int $a
105+
* @phpcs:enable
106+
* @phpstan-return bool
107+
*
108+
* @param int $a
109+
*/
110+
public function phpstanAndPhpcsAnnotations($a)
111+
{
112+
return false;
113+
}
114+
101115
}

0 commit comments

Comments
 (0)