From 5e878708a16a75ed11a7d9aa02f50c257065d4fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Tue, 25 Sep 2018 08:49:15 +0200 Subject: [PATCH] DocCommentSpacingSniff: Some errors were not reported and fixed --- .../Sniffs/Commenting/DocCommentSpacingSniff.php | 11 +++++++++++ .../Sniffs/Commenting/DocCommentSpacingSniffTest.php | 5 +++-- ...docCommentSpacingAnnotationsGroupsErrors.fixed.php | 4 ++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/SlevomatCodingStandard/Sniffs/Commenting/DocCommentSpacingSniff.php b/SlevomatCodingStandard/Sniffs/Commenting/DocCommentSpacingSniff.php index d971c4ac4..e699b042d 100644 --- a/SlevomatCodingStandard/Sniffs/Commenting/DocCommentSpacingSniff.php +++ b/SlevomatCodingStandard/Sniffs/Commenting/DocCommentSpacingSniff.php @@ -462,6 +462,7 @@ private function checkAnnotationsGroupsOrder( $annotationsGroupsPositions = []; $fix = false; + $undefinedAnnotationsGroups = []; foreach ($annotationsGroups as $annotationsGroupPosition => $annotationsGroup) { foreach ($sortedAnnotationsGroups as $sortedAnnotationsGroupPosition => $sortedAnnotationsGroup) { if ($equals($annotationsGroup, $sortedAnnotationsGroup)) { @@ -482,6 +483,7 @@ private function checkAnnotationsGroupsOrder( } if ($undefinedAnnotationsGroup) { + $undefinedAnnotationsGroups[] = $annotationsGroupPosition; continue 2; } } @@ -496,6 +498,11 @@ private function checkAnnotationsGroupsOrder( } if (!$incorrectAnnotationsGroupsExist) { + foreach ($undefinedAnnotationsGroups as $undefinedAnnotationsGroupPosition) { + $annotationsGroupsPositions[$undefinedAnnotationsGroupPosition] = max($annotationsGroupsPositions) + 1; + } + ksort($annotationsGroupsPositions); + $positionsMappedToGroups = array_keys($annotationsGroupsPositions); $tmp = array_values($annotationsGroupsPositions); asort($tmp); @@ -520,6 +527,10 @@ private function checkAnnotationsGroupsOrder( continue; } + if (!array_key_exists($annotationsGroupsPositions[$annotationsGroupPosition], $sortedAnnotationsGroups)) { + continue; + } + $sortedAnnotationsGroup = $sortedAnnotationsGroups[$annotationsGroupsPositions[$annotationsGroupPosition]]; foreach ($annotationsGroup as $annotationPosition => $annotation) { diff --git a/tests/Sniffs/Commenting/DocCommentSpacingSniffTest.php b/tests/Sniffs/Commenting/DocCommentSpacingSniffTest.php index badb7c4bb..2b181c242 100644 --- a/tests/Sniffs/Commenting/DocCommentSpacingSniffTest.php +++ b/tests/Sniffs/Commenting/DocCommentSpacingSniffTest.php @@ -134,13 +134,14 @@ public function testAnnotationsGroupsErrors(): void DocCommentSpacingSniff::CODE_INCORRECT_ORDER_OF_ANNOTATIONS_IN_GROUP, ]); - self::assertSame(6, $report->getErrorCount()); + self::assertSame(7, $report->getErrorCount()); self::assertSniffError($report, 20, DocCommentSpacingSniff::CODE_INCORRECT_ANNOTATIONS_GROUP); self::assertSniffError($report, 34, DocCommentSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_ANNOTATIONS_GROUPS); - self::assertSniffError($report, 46, DocCommentSpacingSniff::CODE_INCORRECT_ORDER_OF_ANNOTATIONS_GROUPS); + self::assertSniffError($report, 44, DocCommentSpacingSniff::CODE_INCORRECT_ORDER_OF_ANNOTATIONS_GROUPS); self::assertSniffError($report, 49, DocCommentSpacingSniff::CODE_INCORRECT_ORDER_OF_ANNOTATIONS_IN_GROUP); self::assertSniffError($report, 62, DocCommentSpacingSniff::CODE_INCORRECT_ORDER_OF_ANNOTATIONS_GROUPS); + self::assertSniffError($report, 74, DocCommentSpacingSniff::CODE_INCORRECT_ORDER_OF_ANNOTATIONS_GROUPS); self::assertSniffError($report, 83, 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 e49ab2992..df556441f 100644 --- a/tests/Sniffs/Commenting/data/docCommentSpacingAnnotationsGroupsErrors.fixed.php +++ b/tests/Sniffs/Commenting/data/docCommentSpacingAnnotationsGroupsErrors.fixed.php @@ -70,9 +70,9 @@ public function oneMoreMethod($a, $b, $c) } /** - * @return bool - * * @param int $a + * + * @return bool */ public function methodBeforeInvalidDocComment($a): bool {