From 7815b1397bd9ff3e09bac9446ecb680f3fc69844 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Wed, 14 Apr 2021 20:15:42 +0200 Subject: [PATCH] ParameterTypeHintSniff, PropertyTypeHintSniff, ReturnTypeHintSniff: Fixed false positives --- .../Helpers/AnnotationTypeHelper.php | 13 ++++++++----- .../Sniffs/TypeHints/ParameterTypeHintSniff.php | 6 +----- .../Sniffs/TypeHints/PropertyTypeHintSniff.php | 6 +----- .../Sniffs/TypeHints/ReturnTypeHintSniff.php | 6 +----- .../data/parameterTypeHintWithUnionNoErrors.php | 7 +++++++ ...opertyTypeHintEnabledNativeWithUnionNoErrors.php | 5 +++++ .../data/returnTypeHintWithUnionNoErrors.php | 7 +++++++ 7 files changed, 30 insertions(+), 20 deletions(-) diff --git a/SlevomatCodingStandard/Helpers/AnnotationTypeHelper.php b/SlevomatCodingStandard/Helpers/AnnotationTypeHelper.php index c79cbd232..33e32b5f7 100644 --- a/SlevomatCodingStandard/Helpers/AnnotationTypeHelper.php +++ b/SlevomatCodingStandard/Helpers/AnnotationTypeHelper.php @@ -19,7 +19,6 @@ use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; -use function array_filter; use function array_merge; use function count; use function in_array; @@ -661,12 +660,16 @@ public static function getTraversableTypeHintsFromType( return []; } - return array_filter($typeHints, static function (string $typeHint) use ($phpcsFile, $pointer, $traversableTypeHints): bool { - return TypeHintHelper::isTraversableType( + foreach ($typeHints as $typeHint) { + if (!TypeHintHelper::isTraversableType( TypeHintHelper::getFullyQualifiedTypeHint($phpcsFile, $pointer, $typeHint), $traversableTypeHints - ); - }); + )) { + return []; + } + } + + return $typeHints; } /** diff --git a/SlevomatCodingStandard/Sniffs/TypeHints/ParameterTypeHintSniff.php b/SlevomatCodingStandard/Sniffs/TypeHints/ParameterTypeHintSniff.php index 0d15c753c..9abf4f028 100644 --- a/SlevomatCodingStandard/Sniffs/TypeHints/ParameterTypeHintSniff.php +++ b/SlevomatCodingStandard/Sniffs/TypeHints/ParameterTypeHintSniff.php @@ -245,17 +245,13 @@ private function checkTypeHints( $itemsSpecificationTypeHint = AnnotationTypeHelper::getItemsSpecificationTypeFromType($parameterTypeNode); if ($itemsSpecificationTypeHint !== null) { - $possibleParameterTypeHints = AnnotationTypeHelper::getTraversableTypeHintsFromType( + $typeHints = AnnotationTypeHelper::getTraversableTypeHintsFromType( $parameterTypeNode, $phpcsFile, $functionPointer, $this->getTraversableTypeHints(), $canTryUnionTypeHint ); - - if (count($possibleParameterTypeHints) > 0) { - $typeHints = $possibleParameterTypeHints; - } } } diff --git a/SlevomatCodingStandard/Sniffs/TypeHints/PropertyTypeHintSniff.php b/SlevomatCodingStandard/Sniffs/TypeHints/PropertyTypeHintSniff.php index 2df45f28f..450ecf8eb 100644 --- a/SlevomatCodingStandard/Sniffs/TypeHints/PropertyTypeHintSniff.php +++ b/SlevomatCodingStandard/Sniffs/TypeHints/PropertyTypeHintSniff.php @@ -267,17 +267,13 @@ private function checkTypeHint( $itemsSpecificationTypeHint = AnnotationTypeHelper::getItemsSpecificationTypeFromType($typeNode); if ($itemsSpecificationTypeHint !== null) { - $possibleTypeHints = AnnotationTypeHelper::getTraversableTypeHintsFromType( + $typeHints = AnnotationTypeHelper::getTraversableTypeHintsFromType( $typeNode, $phpcsFile, $propertyPointer, $this->getTraversableTypeHints(), $this->enableUnionTypeHint ); - - if (count($possibleTypeHints) > 0) { - $typeHints = $possibleTypeHints; - } } } diff --git a/SlevomatCodingStandard/Sniffs/TypeHints/ReturnTypeHintSniff.php b/SlevomatCodingStandard/Sniffs/TypeHints/ReturnTypeHintSniff.php index 58e8503cb..b5e635a17 100644 --- a/SlevomatCodingStandard/Sniffs/TypeHints/ReturnTypeHintSniff.php +++ b/SlevomatCodingStandard/Sniffs/TypeHints/ReturnTypeHintSniff.php @@ -287,17 +287,13 @@ private function checkFunctionTypeHint( $itemsSpecificationTypeHint = AnnotationTypeHelper::getItemsSpecificationTypeFromType($returnTypeNode); if ($itemsSpecificationTypeHint !== null) { - $possibleReturnTypeHints = AnnotationTypeHelper::getTraversableTypeHintsFromType( + $typeHints = AnnotationTypeHelper::getTraversableTypeHintsFromType( $returnTypeNode, $phpcsFile, $functionPointer, $this->getTraversableTypeHints(), $canTryUnionTypeHint ); - - if (count($possibleReturnTypeHints) > 0) { - $typeHints = $possibleReturnTypeHints; - } } } diff --git a/tests/Sniffs/TypeHints/data/parameterTypeHintWithUnionNoErrors.php b/tests/Sniffs/TypeHints/data/parameterTypeHintWithUnionNoErrors.php index dc97f7e6e..2b06b4852 100644 --- a/tests/Sniffs/TypeHints/data/parameterTypeHintWithUnionNoErrors.php +++ b/tests/Sniffs/TypeHints/data/parameterTypeHintWithUnionNoErrors.php @@ -14,4 +14,11 @@ private function withTrue(string|bool $a) { } + /** + * @param ArrayHash|array|mixed[] $a + */ + public function arrayAndArrayHash($a) + { + } + } diff --git a/tests/Sniffs/TypeHints/data/propertyTypeHintEnabledNativeWithUnionNoErrors.php b/tests/Sniffs/TypeHints/data/propertyTypeHintEnabledNativeWithUnionNoErrors.php index 48cbff336..b111c5d0a 100644 --- a/tests/Sniffs/TypeHints/data/propertyTypeHintEnabledNativeWithUnionNoErrors.php +++ b/tests/Sniffs/TypeHints/data/propertyTypeHintEnabledNativeWithUnionNoErrors.php @@ -8,4 +8,9 @@ class Whatever /** @var string|true */ private string|bool $withTrue; + /** + * @var ArrayHash|array|mixed[] + */ + protected $arrayAndArrayHash; + } diff --git a/tests/Sniffs/TypeHints/data/returnTypeHintWithUnionNoErrors.php b/tests/Sniffs/TypeHints/data/returnTypeHintWithUnionNoErrors.php index af27d2115..0d0b3e4a5 100644 --- a/tests/Sniffs/TypeHints/data/returnTypeHintWithUnionNoErrors.php +++ b/tests/Sniffs/TypeHints/data/returnTypeHintWithUnionNoErrors.php @@ -12,4 +12,11 @@ private function withTrue(): string|bool { } + /** + * @return ArrayHash|array|mixed[] + */ + public function arrayAndArrayHash() + { + } + }