Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 3 additions & 32 deletions src/Type/Php/ArraySearchFunctionDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -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 [];
}

}