Skip to content

Commit 6ea0278

Browse files
committed
SlevomatCodingStandard.Commenting.UselessFunctionDocCommentSniff: It should report simple "array" as useless
1 parent d16a31a commit 6ea0278

13 files changed

Lines changed: 34 additions & 15 deletions

SlevomatCodingStandard/Helpers/AnnotationHelper.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,21 @@ public static function isAnnotationUseless(
216216
return true;
217217
}
218218

219-
if (TypeHintHelper::isTraversableType(
220-
TypeHintHelper::getFullyQualifiedTypeHint($phpcsFile, $functionPointer, $typeHint->getTypeHintWithoutNullabilitySymbol()),
221-
$traversableTypeHints
222-
)) {
219+
$annotationType = $annotationValue->type;
220+
221+
if (
222+
TypeHintHelper::isTraversableType(
223+
TypeHintHelper::getFullyQualifiedTypeHint($phpcsFile, $functionPointer, $typeHint->getTypeHintWithoutNullabilitySymbol()),
224+
$traversableTypeHints
225+
)
226+
&& !(
227+
$annotationType instanceof IdentifierTypeNode
228+
&& TypeHintHelper::isSimpleIterableTypeHint(strtolower($annotationType->name))
229+
)
230+
) {
223231
return false;
224232
}
225233

226-
$annotationType = $annotationValue->type;
227-
228234
if (AnnotationTypeHelper::containsStaticOrThisType($annotationType)) {
229235
return false;
230236
}

tests/Sniffs/Commenting/UselessFunctionDocCommentSniffTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function testErrors(): void
1919
{
2020
$report = self::checkFile(__DIR__ . '/data/uselessFunctionDocCommentSniffErrors.php');
2121

22-
self::assertSame(7, $report->getErrorCount());
22+
self::assertSame(8, $report->getErrorCount());
2323

2424
self::assertSniffError($report, 10, UselessFunctionDocCommentSniff::CODE_USELESS_DOC_COMMENT);
2525
self::assertSniffError($report, 18, UselessFunctionDocCommentSniff::CODE_USELESS_DOC_COMMENT);
@@ -28,6 +28,7 @@ public function testErrors(): void
2828
self::assertSniffError($report, 39, UselessFunctionDocCommentSniff::CODE_USELESS_DOC_COMMENT);
2929
self::assertSniffError($report, 47, UselessFunctionDocCommentSniff::CODE_USELESS_DOC_COMMENT);
3030
self::assertSniffError($report, 54, UselessFunctionDocCommentSniff::CODE_USELESS_DOC_COMMENT);
31+
self::assertSniffError($report, 61, UselessFunctionDocCommentSniff::CODE_USELESS_DOC_COMMENT);
3132

3233
self::assertAllFixedInFile($report);
3334
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,8 @@ public function onlyParameterWithoutTypeArray(array $a): void
3333
{
3434
}
3535

36+
public function onlyParameterWithSimpleArrayType(array $a): void
37+
{
38+
}
39+
3640
}

tests/Sniffs/Commenting/data/uselessFunctionDocCommentSniffErrors.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,11 @@ public function onlyParameterWithoutTypeArray(array $a): void
5555
{
5656
}
5757

58+
/**
59+
* @param array $a
60+
*/
61+
public function onlyParameterWithSimpleArrayType(array $a): void
62+
{
63+
}
64+
5865
}

tests/Sniffs/TypeHints/ParameterTypeHintSniffTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function testErrors(): void
3030
'traversableTypeHints' => ['Traversable', '\ArrayIterator'],
3131
]);
3232

33-
self::assertSame(51, $report->getErrorCount());
33+
self::assertSame(52, $report->getErrorCount());
3434

3535
self::assertSniffError($report, 6, ParameterTypeHintSniff::CODE_MISSING_ANY_TYPE_HINT);
3636
self::assertSniffError($report, 14, ParameterTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
@@ -43,6 +43,7 @@ public function testErrors(): void
4343
self::assertSniffError($report, 67, ParameterTypeHintSniff::CODE_USELESS_ANNOTATION);
4444
self::assertSniffError($report, 74, ParameterTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION);
4545
self::assertSniffError($report, 80, ParameterTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION);
46+
self::assertSniffError($report, 80, ParameterTypeHintSniff::CODE_USELESS_ANNOTATION);
4647
self::assertSniffError($report, 90, ParameterTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
4748
self::assertSniffError($report, 96, ParameterTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION);
4849
self::assertSniffError($report, 98, ParameterTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);

tests/Sniffs/TypeHints/PropertyTypeHintSniffTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function testEnabledNativeErrors(): void
5757
'traversableTypeHints' => ['Traversable'],
5858
]);
5959

60-
self::assertSame(49, $report->getErrorCount());
60+
self::assertSame(50, $report->getErrorCount());
6161

6262
self::assertSniffError($report, 6, PropertyTypeHintSniff::CODE_MISSING_ANY_TYPE_HINT);
6363
self::assertSniffError($report, 11, PropertyTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
@@ -68,6 +68,7 @@ public function testEnabledNativeErrors(): void
6868
self::assertSniffError($report, 34, PropertyTypeHintSniff::CODE_USELESS_ANNOTATION);
6969
self::assertSniffError($report, 38, PropertyTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION);
7070
self::assertSniffError($report, 41, PropertyTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION);
71+
self::assertSniffError($report, 41, PropertyTypeHintSniff::CODE_USELESS_ANNOTATION);
7172
self::assertSniffError($report, 48, PropertyTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
7273
self::assertSniffError($report, 51, PropertyTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION);
7374
self::assertSniffError($report, 53, PropertyTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);

tests/Sniffs/TypeHints/ReturnTypeHintSniffTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testErrors(): void
3333
'traversableTypeHints' => ['Traversable', '\ArrayIterator'],
3434
]);
3535

36-
self::assertSame(61, $report->getErrorCount());
36+
self::assertSame(62, $report->getErrorCount());
3737

3838
self::assertSniffError($report, 6, ReturnTypeHintSniff::CODE_MISSING_ANY_TYPE_HINT);
3939
self::assertSniffError($report, 14, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
@@ -45,6 +45,7 @@ public function testErrors(): void
4545
self::assertSniffError($report, 60, ReturnTypeHintSniff::CODE_USELESS_ANNOTATION);
4646
self::assertSniffError($report, 67, ReturnTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION);
4747
self::assertSniffError($report, 73, ReturnTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION);
48+
self::assertSniffError($report, 73, ReturnTypeHintSniff::CODE_USELESS_ANNOTATION);
4849
self::assertSniffError($report, 80, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
4950
self::assertSniffError($report, 88, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
5051
self::assertSniffError($report, 92, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);

tests/Sniffs/TypeHints/data/parameterTypeHintErrors.fixed.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public function missingAnnotationForTraversable(array $a)
7373
}
7474

7575
/**
76-
* @param array $a
7776
*/
7877
public function missingItemsSpecification(array $a)
7978
{

tests/Sniffs/TypeHints/data/parameterTypeHintNoErrors.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ private function isSniffCodeMissingNativeTypeHintSuppressed($a)
4848

4949
/**
5050
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification
51+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.UselessAnnotation
5152
* @param array $a
5253
*/
5354
private function isSniffCodeMissingTravesableTypeHintSpecificationSuppressed(array $a)

tests/Sniffs/TypeHints/data/propertyTypeHintEnabledNativeErrors.fixed.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ class Whatever
3131

3232
public array $missingAnnotationForTraversable;
3333

34-
/**
35-
* @var array
36-
*/
3734
public array $missingItemsSpecification;
3835

3936
/**

0 commit comments

Comments
 (0)