diff --git a/src/Type/Php/ArraySearchFunctionDynamicReturnTypeExtension.php b/src/Type/Php/ArraySearchFunctionDynamicReturnTypeExtension.php index 997178b24d..7ef5a08adf 100644 --- a/src/Type/Php/ArraySearchFunctionDynamicReturnTypeExtension.php +++ b/src/Type/Php/ArraySearchFunctionDynamicReturnTypeExtension.php @@ -6,18 +6,15 @@ use PHPStan\Analyser\Scope; use PHPStan\Reflection\FunctionReflection; use PHPStan\Reflection\ParametersAcceptorSelector; -use PHPStan\Type\ArrayType; use PHPStan\Type\Constant\ConstantArrayType; use PHPStan\Type\Constant\ConstantBooleanType; use PHPStan\Type\ConstantScalarType; use PHPStan\Type\DynamicFunctionReturnTypeExtension; -use PHPStan\Type\IntersectionType; -use PHPStan\Type\MixedType; use PHPStan\Type\NullType; use PHPStan\Type\ObjectWithoutClassType; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; -use PHPStan\Type\UnionType; +use PHPStan\Type\TypeUtils; use function count; final class ArraySearchFunctionDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension @@ -36,7 +33,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, } $haystackArgType = $scope->getType($functionCall->getArgs()[1]->value); - $haystackIsArray = (new ArrayType(new MixedType(), new MixedType()))->isSuperTypeOf($haystackArgType); + $haystackIsArray = $haystackArgType->isArray(); if ($haystackIsArray->no()) { return new NullType(); } @@ -62,7 +59,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, $typesFromConstantArrays[] = new NullType(); } - $haystackArrays = $this->pickArrays($haystackArgType); + $haystackArrays = TypeUtils::getAnyArrays($haystackArgType); if (count($haystackArrays) === 0) { return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(); } @@ -134,30 +131,4 @@ private function resolveTypeFromConstantHaystackAndNeedle(Type $needle, Constant return new ConstantBooleanType(false); } - /** - * @return Type[] - */ - private function pickArrays(Type $type): array - { - if ($type instanceof ArrayType) { - return [$type]; - } - - if ($type instanceof UnionType || $type instanceof IntersectionType) { - $arrayTypes = []; - - foreach ($type->getTypes() as $innerType) { - if (!($innerType instanceof ArrayType)) { - continue; - } - - $arrayTypes[] = $innerType; - } - - return $arrayTypes; - } - - return []; - } - }