Skip to content

Commit 9ad756f

Browse files
herndlmondrejmirtes
authored andcommitted
Simplify default type handling in FilterFunctionReturnTypeHelper
1 parent 8bf26a5 commit 9ad756f

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

Diff for: src/Type/Php/FilterFunctionReturnTypeHelper.php

+15-21
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,20 @@ public function getTypeFromFunctionCall(Type $inputType, ?Type $filterType, ?Typ
6565
$flagsType = new ConstantIntegerType(0);
6666
}
6767

68-
$defaultType = $this->hasFlag($this->getConstant('FILTER_NULL_ON_FAILURE'), $flagsType)
68+
$hasOptions = $this->hasOptions($flagsType);
69+
$options = $hasOptions->yes() ? $this->getOptions($flagsType, $filterValue) : [];
70+
71+
$defaultType = $options['default'] ?? ($this->hasFlag($this->getConstant('FILTER_NULL_ON_FAILURE'), $flagsType)
6972
? new NullType()
70-
: new ConstantBooleanType(false);
73+
: new ConstantBooleanType(false));
7174

7275
if ($inputType->isScalar()->no() && $inputType->isNull()->no()) {
7376
return $defaultType;
7477
}
7578

7679
$exactType = $this->determineExactType($inputType, $filterValue, $defaultType, $flagsType);
7780
$type = $exactType ?? $this->getFilterTypeMap()[$filterValue] ?? $mixedType;
78-
79-
$hasOptions = $this->hasOptions($flagsType);
80-
$options = $hasOptions->yes() ? $this->getOptions($flagsType, $filterValue) : [];
81-
$otherTypes = $this->getOtherTypes($flagsType, $options, $defaultType);
81+
$otherTypes = $this->getOtherTypes($flagsType, $options);
8282

8383
if ($inputType->isNonEmptyString()->yes()
8484
&& $type->isString()->yes()
@@ -93,21 +93,17 @@ public function getTypeFromFunctionCall(Type $inputType, ?Type $filterType, ?Typ
9393
if (isset($otherTypes['range'])) {
9494
if ($type instanceof ConstantScalarType) {
9595
if ($otherTypes['range']->isSuperTypeOf($type)->no()) {
96-
$type = $otherTypes['default'];
96+
$type = $defaultType;
9797
}
98-
99-
unset($otherTypes['default']);
10098
} else {
10199
$type = $otherTypes['range'];
102100
}
103101
}
104102

105-
if ($exactType !== null && !$hasOptions->maybe() && ($inputType->equals($type) || !$inputType->isSuperTypeOf($type)->yes())) {
106-
unset($otherTypes['default']);
107-
}
108-
109-
if (isset($otherTypes['default']) && $otherTypes['default']->isSuperTypeOf($type)->no()) {
110-
$type = TypeCombinator::union($type, $otherTypes['default']);
103+
if ($exactType === null || $hasOptions->maybe() || (!$inputType->equals($type) && $inputType->isSuperTypeOf($type)->yes())) {
104+
if ($defaultType->isSuperTypeOf($type)->no()) {
105+
$type = TypeCombinator::union($type, $defaultType);
106+
}
111107
}
112108

113109
if ($this->hasFlag($this->getConstant('FILTER_FORCE_ARRAY'), $flagsType)) {
@@ -252,17 +248,15 @@ private function determineExactType(Type $in, int $filterValue, Type $defaultTyp
252248

253249
/**
254250
* @param array<string, ?Type> $typeOptions
255-
* @return array{default: Type, range?: Type}
251+
* @return array{range?: Type}
256252
*/
257-
private function getOtherTypes(?Type $flagsType, array $typeOptions, Type $defaultType): array
253+
private function getOtherTypes(?Type $flagsType, array $typeOptions): array
258254
{
259-
$falseType = new ConstantBooleanType(false);
260255
if ($flagsType === null) {
261-
return ['default' => $falseType];
256+
return [];
262257
}
263258

264-
$defaultType = $typeOptions['default'] ?? $defaultType;
265-
$otherTypes = ['default' => $defaultType];
259+
$otherTypes = [];
266260
$range = [];
267261
if (isset($typeOptions['min_range'])) {
268262
if ($typeOptions['min_range'] instanceof ConstantScalarType) {

Diff for: tests/PHPStan/Analyser/data/filter-var.php

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function invalidInput(array $arr, object $object, $resource): void
2626
assertType('false', filter_var($object));
2727
assertType('false', filter_var($resource));
2828
assertType('null', filter_var(new stdClass(), FILTER_DEFAULT, FILTER_NULL_ON_FAILURE));
29+
assertType("'invalid'", filter_var(new stdClass(), FILTER_DEFAULT, ['options' => ['default' => 'invalid']]));
2930
}
3031

3132
public function intToInt(int $int, array $options): void

0 commit comments

Comments
 (0)