Skip to content

Commit 19bb299

Browse files
Add support for union of constant integer for filter_var
1 parent 5139fc8 commit 19bb299

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/Type/Php/FilterFunctionReturnTypeHelper.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public function getType(Type $inputType, ?Type $filterType, ?Type $flagsType): T
191191
}
192192

193193
if ($exactType === null || $hasOptions->maybe() || (!$inputType->equals($type) && $inputType->isSuperTypeOf($type)->yes())) {
194-
if ($defaultType->isSuperTypeOf($type)->no()) {
194+
if (!$defaultType->isSuperTypeOf($type)->yes()) {
195195
$type = TypeCombinator::union($type, $defaultType);
196196
}
197197
}
@@ -495,11 +495,17 @@ private function hasFlag(string $flagName, ?Type $flagsType): TrinaryLogic
495495
}
496496

497497
$type = $this->getFlagsValue($flagsType);
498-
if (!$type instanceof ConstantIntegerType) {
498+
$scalarValues = $type->getConstantScalarValues();
499+
if ($scalarValues === []) {
499500
return TrinaryLogic::createMaybe();
500501
}
501502

502-
return TrinaryLogic::createFromBoolean(($type->getValue() & $flag) === $flag);
503+
return TrinaryLogic::lazyExtremeIdentity(
504+
$scalarValues,
505+
static fn (bool|string|int|float|null $scalar): TrinaryLogic => TrinaryLogic::createFromBoolean(
506+
(((int) $scalar) & $flag) === $flag,
507+
),
508+
);
503509
}
504510

505511
private function getFlagsValue(Type $exprType): Type

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,15 @@ public function scalars(bool $bool, float $float, int $int, string $string, int
169169
assertType("''", filter_var(null));
170170
}
171171

172+
public function randomFlag($mixed, bool $bool) {
173+
174+
assertType('int|false|null', filter_var($mixed, FILTER_VALIDATE_INT, [
175+
'flags' => $bool ? FILTER_NULL_ON_FAILURE : FILTER_FLAG_NONE,
176+
]));
177+
178+
assertType('bool|null', filter_var($mixed, FILTER_VALIDATE_BOOLEAN, [
179+
'flags' => $bool ? FILTER_NULL_ON_FAILURE : FILTER_FLAG_NONE,
180+
]));
181+
}
182+
172183
}

0 commit comments

Comments
 (0)