Skip to content

Commit

Permalink
ParameterTypeHintSniff, PropertyTypeHintSniff, ReturnTypeHintSniff: F…
Browse files Browse the repository at this point in the history
…ixed false positives
  • Loading branch information
kukulich committed Apr 14, 2021
1 parent e7286b9 commit 7815b13
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 20 deletions.
13 changes: 8 additions & 5 deletions SlevomatCodingStandard/Helpers/AnnotationTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ private function withTrue(string|bool $a)
{
}

/**
* @param ArrayHash|array|mixed[] $a
*/
public function arrayAndArrayHash($a)
{
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ class Whatever
/** @var string|true */
private string|bool $withTrue;

/**
* @var ArrayHash|array|mixed[]
*/
protected $arrayAndArrayHash;

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@ private function withTrue(): string|bool
{
}

/**
* @return ArrayHash|array|mixed[]
*/
public function arrayAndArrayHash()
{
}

}

0 comments on commit 7815b13

Please sign in to comment.