Skip to content

Commit cd1adfd

Browse files
committed
Improve code in FilterVarArrayDynamicReturnTypeExtension
1 parent 0a39cca commit cd1adfd

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

src/Type/Php/FilterVarArrayDynamicReturnTypeExtension.php

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\Type\ArrayType;
1212
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
1313
use PHPStan\Type\Constant\ConstantIntegerType;
14+
use PHPStan\Type\Constant\ConstantStringType;
1415
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
1516
use PHPStan\Type\MixedType;
1617
use PHPStan\Type\NeverType;
@@ -45,8 +46,6 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
4546

4647
$functionName = strtolower($functionReflection->getName());
4748
$inputArgType = $scope->getType($functionCall->getArgs()[0]->value);
48-
49-
$inputArrayType = $inputArgType->getArrays()[0] ?? null;
5049
$inputConstantArrayType = null;
5150
if ($functionName === 'filter_var_array') {
5251
if ($inputArgType->isArray()->no()) {
@@ -69,14 +68,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
6968

7069
// Pragmatical solution since global expressions are not passed through the scope for performance reasons
7170
// See https://github.com/phpstan/phpstan-src/pull/2012 for details
72-
$inputArrayType = new ArrayType(new StringType(), new MixedType());
73-
}
74-
75-
if ($inputArrayType === null) {
76-
$inputArrayType = new ArrayType(new MixedType(), new MixedType());
77-
if ($inputArgType->isSuperTypeOf($inputArrayType)->no()) {
78-
return null;
79-
}
71+
$inputArgType = new ArrayType(new StringType(), new MixedType());
8072
}
8173

8274
$filterArgType = $scope->getType($functionCall->getArgs()[1]->value);
@@ -90,11 +82,11 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
9082
if ($inputConstantArrayType === null) {
9183
$isList = $inputArgType->isList()->yes();
9284
$valueType = $this->filterFunctionReturnTypeHelper->getType(
93-
$inputArrayType->getItemType(),
85+
$inputArgType->getIterableValueType(),
9486
$filterArgType,
9587
null,
9688
);
97-
$arrayType = new ArrayType($inputArrayType->getKeyType(), $valueType);
89+
$arrayType = new ArrayType($inputArgType->getIterableKeyType(), $valueType);
9890

9991
return $isList ? AccessoryArrayListType::intersectWith($arrayType) : $arrayType;
10092
}
@@ -116,11 +108,11 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
116108
}
117109
} elseif ($filterConstantArrayType === null) {
118110
if ($inputConstantArrayType === null) {
119-
$isList = $inputArrayType->isList()->yes();
120-
$valueType = $this->filterFunctionReturnTypeHelper->getType($inputArrayType, $filterArgType, null);
111+
$isList = $inputArgType->isList()->yes();
112+
$valueType = $this->filterFunctionReturnTypeHelper->getType($inputArgType, $filterArgType, null);
121113

122114
$arrayType = new ArrayType(
123-
$inputArrayType->getKeyType(),
115+
$inputArgType->getIterableKeyType(),
124116
$addEmpty ? TypeCombinator::addNull($valueType) : $valueType,
125117
);
126118

@@ -148,7 +140,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
148140
}
149141
} else {
150142
$optionalKeys = $filterKeysList;
151-
$inputTypesMap = array_fill_keys($optionalKeys, $inputArrayType->getItemType());
143+
$inputTypesMap = array_fill_keys($optionalKeys, $inputArgType->getIterableValueType());
152144
}
153145
}
154146

@@ -184,21 +176,23 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
184176
/** @return array{?Type, ?Type} */
185177
public function fetchFilter(Type $type): array
186178
{
187-
$constantType = $type->getConstantArrays()[0] ?? null;
179+
if (!$type->isArray()->yes()) {
180+
return [$type, null];
181+
}
188182

189-
if ($constantType === null) {
183+
$filterKey = new ConstantStringType('filter');
184+
if (!$type->hasOffsetValueType($filterKey)->yes()) {
190185
return [$type, null];
191186
}
192187

188+
$filterOffsetType = $type->getOffsetValueType($filterKey);
193189
$filterType = null;
194-
foreach ($constantType->getKeyTypes() as $keyType) {
195-
if ($keyType->getValue() === 'filter') {
196-
$filterType = $constantType->getOffsetValueType($keyType)->getConstantScalarTypes()[0] ?? null;
197-
break;
198-
}
190+
191+
if (count($filterOffsetType->getConstantScalarTypes()) > 0) {
192+
$filterType = TypeCombinator::union(...$filterOffsetType->getConstantScalarTypes());
199193
}
200194

201-
return [$filterType, $constantType];
195+
return [$filterType, $type];
202196
}
203197

204198
}

0 commit comments

Comments
 (0)