Skip to content

Commit 8b36ae3

Browse files
Fix throw on failure with require/force array (#5349)
1 parent 1920006 commit 8b36ae3

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/Type/Php/FilterFunctionReturnTypeHelper.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,9 @@ public function getType(Type $inputType, ?Type $filterType, ?Type $flagsType): T
151151

152152
$inputIsArray = $inputType->isArray();
153153
$hasRequireArrayFlag = $this->hasFlag('FILTER_REQUIRE_ARRAY', $flagsType);
154+
$hasThrowOnFailureFlag = $this->hasFlag('FILTER_THROW_ON_FAILURE', $flagsType);
154155
if ($inputIsArray->no() && $hasRequireArrayFlag->yes()) {
155-
if ($this->hasFlag('FILTER_THROW_ON_FAILURE', $flagsType)->yes()) {
156+
if ($hasThrowOnFailureFlag->yes()) {
156157
return new ErrorType();
157158
}
158159

@@ -197,9 +198,13 @@ public function getType(Type $inputType, ?Type $filterType, ?Type $flagsType): T
197198
}
198199
}
199200

201+
if ($hasThrowOnFailureFlag->yes()) {
202+
$type = TypeCombinator::remove($type, $defaultType);
203+
}
204+
200205
if ($hasRequireArrayFlag->yes()) {
201206
$type = new ArrayType($inputArrayKeyType ?? $mixedType, $type);
202-
if (!$inputIsArray->yes()) {
207+
if (!$hasThrowOnFailureFlag->yes() && !$inputIsArray->yes()) {
203208
$type = TypeCombinator::union($type, $defaultType);
204209
}
205210
}
@@ -208,10 +213,6 @@ public function getType(Type $inputType, ?Type $filterType, ?Type $flagsType): T
208213
return new ArrayType($inputArrayKeyType ?? $mixedType, $type);
209214
}
210215

211-
if ($this->hasFlag('FILTER_THROW_ON_FAILURE', $flagsType)->yes()) {
212-
$type = TypeCombinator::remove($type, $defaultType);
213-
}
214-
215216
return $type;
216217
}
217218

tests/PHPStan/Analyser/nsrt/filter-var-php85.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@ public function doFoo($mixed): void
2323
assertType('int', filter_var($mixed, FILTER_VALIDATE_INT, FILTER_THROW_ON_FAILURE));
2424
assertType('int', filter_var($mixed, FILTER_VALIDATE_INT, ['flags' => FILTER_THROW_ON_FAILURE]));
2525
}
26+
27+
public function more($mixed): void
28+
{
29+
assertType('array<int>', filter_var($mixed, FILTER_VALIDATE_INT, FILTER_FORCE_ARRAY|FILTER_THROW_ON_FAILURE));
30+
assertType('array<int>', filter_var($mixed, FILTER_VALIDATE_INT, FILTER_REQUIRE_ARRAY|FILTER_THROW_ON_FAILURE));
31+
}
2632
}

0 commit comments

Comments
 (0)