Skip to content

Commit 9675cc8

Browse files
Add support for union of constant integer for filter_var
1 parent d1f76fc commit 9675cc8

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Type/Php/FilterFunctionReturnTypeHelper.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,11 @@ 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+
172179
}

0 commit comments

Comments
 (0)