Skip to content

Commit

Permalink
Improve code in FilterVarArrayDynamicReturnTypeExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 12, 2023
1 parent 0a39cca commit cd1adfd
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions src/Type/Php/FilterVarArrayDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
Expand Down Expand Up @@ -45,8 +46,6 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,

$functionName = strtolower($functionReflection->getName());
$inputArgType = $scope->getType($functionCall->getArgs()[0]->value);

$inputArrayType = $inputArgType->getArrays()[0] ?? null;
$inputConstantArrayType = null;
if ($functionName === 'filter_var_array') {
if ($inputArgType->isArray()->no()) {
Expand All @@ -69,14 +68,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,

// Pragmatical solution since global expressions are not passed through the scope for performance reasons
// See https://github.com/phpstan/phpstan-src/pull/2012 for details
$inputArrayType = new ArrayType(new StringType(), new MixedType());
}

if ($inputArrayType === null) {
$inputArrayType = new ArrayType(new MixedType(), new MixedType());
if ($inputArgType->isSuperTypeOf($inputArrayType)->no()) {
return null;
}
$inputArgType = new ArrayType(new StringType(), new MixedType());
}

$filterArgType = $scope->getType($functionCall->getArgs()[1]->value);
Expand All @@ -90,11 +82,11 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
if ($inputConstantArrayType === null) {
$isList = $inputArgType->isList()->yes();
$valueType = $this->filterFunctionReturnTypeHelper->getType(
$inputArrayType->getItemType(),
$inputArgType->getIterableValueType(),
$filterArgType,
null,
);
$arrayType = new ArrayType($inputArrayType->getKeyType(), $valueType);
$arrayType = new ArrayType($inputArgType->getIterableKeyType(), $valueType);

return $isList ? AccessoryArrayListType::intersectWith($arrayType) : $arrayType;
}
Expand All @@ -116,11 +108,11 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
}
} elseif ($filterConstantArrayType === null) {
if ($inputConstantArrayType === null) {
$isList = $inputArrayType->isList()->yes();
$valueType = $this->filterFunctionReturnTypeHelper->getType($inputArrayType, $filterArgType, null);
$isList = $inputArgType->isList()->yes();
$valueType = $this->filterFunctionReturnTypeHelper->getType($inputArgType, $filterArgType, null);

$arrayType = new ArrayType(
$inputArrayType->getKeyType(),
$inputArgType->getIterableKeyType(),
$addEmpty ? TypeCombinator::addNull($valueType) : $valueType,
);

Expand Down Expand Up @@ -148,7 +140,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
}
} else {
$optionalKeys = $filterKeysList;
$inputTypesMap = array_fill_keys($optionalKeys, $inputArrayType->getItemType());
$inputTypesMap = array_fill_keys($optionalKeys, $inputArgType->getIterableValueType());
}
}

Expand Down Expand Up @@ -184,21 +176,23 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
/** @return array{?Type, ?Type} */
public function fetchFilter(Type $type): array
{
$constantType = $type->getConstantArrays()[0] ?? null;
if (!$type->isArray()->yes()) {
return [$type, null];
}

if ($constantType === null) {
$filterKey = new ConstantStringType('filter');
if (!$type->hasOffsetValueType($filterKey)->yes()) {
return [$type, null];
}

$filterOffsetType = $type->getOffsetValueType($filterKey);
$filterType = null;
foreach ($constantType->getKeyTypes() as $keyType) {
if ($keyType->getValue() === 'filter') {
$filterType = $constantType->getOffsetValueType($keyType)->getConstantScalarTypes()[0] ?? null;
break;
}

if (count($filterOffsetType->getConstantScalarTypes()) > 0) {
$filterType = TypeCombinator::union(...$filterOffsetType->getConstantScalarTypes());
}

return [$filterType, $constantType];
return [$filterType, $type];
}

}

0 comments on commit cd1adfd

Please sign in to comment.