Skip to content

Commit

Permalink
SlevomatCodingStandard.Commenting.UselessFunctionDocCommentSniff: It …
Browse files Browse the repository at this point in the history
…should report simple "array" as useless
  • Loading branch information
kukulich committed Mar 9, 2024
1 parent d16a31a commit 6ea0278
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 15 deletions.
18 changes: 12 additions & 6 deletions SlevomatCodingStandard/Helpers/AnnotationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,21 @@ public static function isAnnotationUseless(
return true;
}

if (TypeHintHelper::isTraversableType(
TypeHintHelper::getFullyQualifiedTypeHint($phpcsFile, $functionPointer, $typeHint->getTypeHintWithoutNullabilitySymbol()),
$traversableTypeHints
)) {
$annotationType = $annotationValue->type;

if (
TypeHintHelper::isTraversableType(
TypeHintHelper::getFullyQualifiedTypeHint($phpcsFile, $functionPointer, $typeHint->getTypeHintWithoutNullabilitySymbol()),
$traversableTypeHints
)
&& !(
$annotationType instanceof IdentifierTypeNode
&& TypeHintHelper::isSimpleIterableTypeHint(strtolower($annotationType->name))
)
) {
return false;
}

$annotationType = $annotationValue->type;

if (AnnotationTypeHelper::containsStaticOrThisType($annotationType)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/uselessFunctionDocCommentSniffErrors.php');

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

self::assertSniffError($report, 10, UselessFunctionDocCommentSniff::CODE_USELESS_DOC_COMMENT);
self::assertSniffError($report, 18, UselessFunctionDocCommentSniff::CODE_USELESS_DOC_COMMENT);
Expand All @@ -28,6 +28,7 @@ public function testErrors(): void
self::assertSniffError($report, 39, UselessFunctionDocCommentSniff::CODE_USELESS_DOC_COMMENT);
self::assertSniffError($report, 47, UselessFunctionDocCommentSniff::CODE_USELESS_DOC_COMMENT);
self::assertSniffError($report, 54, UselessFunctionDocCommentSniff::CODE_USELESS_DOC_COMMENT);
self::assertSniffError($report, 61, UselessFunctionDocCommentSniff::CODE_USELESS_DOC_COMMENT);

self::assertAllFixedInFile($report);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ public function onlyParameterWithoutTypeArray(array $a): void
{
}

public function onlyParameterWithSimpleArrayType(array $a): void
{
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,11 @@ public function onlyParameterWithoutTypeArray(array $a): void
{
}

/**
* @param array $a
*/
public function onlyParameterWithSimpleArrayType(array $a): void
{
}

}
3 changes: 2 additions & 1 deletion tests/Sniffs/TypeHints/ParameterTypeHintSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testErrors(): void
'traversableTypeHints' => ['Traversable', '\ArrayIterator'],
]);

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

self::assertSniffError($report, 6, ParameterTypeHintSniff::CODE_MISSING_ANY_TYPE_HINT);
self::assertSniffError($report, 14, ParameterTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
Expand All @@ -43,6 +43,7 @@ public function testErrors(): void
self::assertSniffError($report, 67, ParameterTypeHintSniff::CODE_USELESS_ANNOTATION);
self::assertSniffError($report, 74, ParameterTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION);
self::assertSniffError($report, 80, ParameterTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION);
self::assertSniffError($report, 80, ParameterTypeHintSniff::CODE_USELESS_ANNOTATION);
self::assertSniffError($report, 90, ParameterTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
self::assertSniffError($report, 96, ParameterTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION);
self::assertSniffError($report, 98, ParameterTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
Expand Down
3 changes: 2 additions & 1 deletion tests/Sniffs/TypeHints/PropertyTypeHintSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testEnabledNativeErrors(): void
'traversableTypeHints' => ['Traversable'],
]);

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

self::assertSniffError($report, 6, PropertyTypeHintSniff::CODE_MISSING_ANY_TYPE_HINT);
self::assertSniffError($report, 11, PropertyTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
Expand All @@ -68,6 +68,7 @@ public function testEnabledNativeErrors(): void
self::assertSniffError($report, 34, PropertyTypeHintSniff::CODE_USELESS_ANNOTATION);
self::assertSniffError($report, 38, PropertyTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION);
self::assertSniffError($report, 41, PropertyTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION);
self::assertSniffError($report, 41, PropertyTypeHintSniff::CODE_USELESS_ANNOTATION);
self::assertSniffError($report, 48, PropertyTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
self::assertSniffError($report, 51, PropertyTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION);
self::assertSniffError($report, 53, PropertyTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
Expand Down
3 changes: 2 additions & 1 deletion tests/Sniffs/TypeHints/ReturnTypeHintSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testErrors(): void
'traversableTypeHints' => ['Traversable', '\ArrayIterator'],
]);

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

self::assertSniffError($report, 6, ReturnTypeHintSniff::CODE_MISSING_ANY_TYPE_HINT);
self::assertSniffError($report, 14, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
Expand All @@ -45,6 +45,7 @@ public function testErrors(): void
self::assertSniffError($report, 60, ReturnTypeHintSniff::CODE_USELESS_ANNOTATION);
self::assertSniffError($report, 67, ReturnTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION);
self::assertSniffError($report, 73, ReturnTypeHintSniff::CODE_MISSING_TRAVERSABLE_TYPE_HINT_SPECIFICATION);
self::assertSniffError($report, 73, ReturnTypeHintSniff::CODE_USELESS_ANNOTATION);
self::assertSniffError($report, 80, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
self::assertSniffError($report, 88, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
self::assertSniffError($report, 92, ReturnTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public function missingAnnotationForTraversable(array $a)
}

/**
* @param array $a
*/
public function missingItemsSpecification(array $a)
{
Expand Down
1 change: 1 addition & 0 deletions tests/Sniffs/TypeHints/data/parameterTypeHintNoErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ private function isSniffCodeMissingNativeTypeHintSuppressed($a)

/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.UselessAnnotation
* @param array $a
*/
private function isSniffCodeMissingTravesableTypeHintSpecificationSuppressed(array $a)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ class Whatever

public array $missingAnnotationForTraversable;

/**
* @var array
*/
public array $missingItemsSpecification;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Whatever

/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingTraversableTypeHintSpecification
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint.UselessAnnotation
* @var array
*/
private array $isSniffCodeMissingTravesableTypeHintSpecificationSuppressed;
Expand Down
1 change: 0 additions & 1 deletion tests/Sniffs/TypeHints/data/returnTypeHintErrors.fixed.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public function missingAnnotationForTraversable(): array
}

/**
* @return array
*/
public function missingItemsSpecification(): array
{
Expand Down
1 change: 1 addition & 0 deletions tests/Sniffs/TypeHints/data/returnTypeHintNoErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ abstract public function isSniffCodeMissingNativeTypeHintSuppressedWithVoid();

/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.UselessAnnotation
* @return array
*/
private function isSniffCodeMissingTravesableTypeHintSpecificationSuppressed(): array
Expand Down

0 comments on commit 6ea0278

Please sign in to comment.