Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Type/Php/FilterFunctionReturnTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function getType(Type $inputType, ?Type $filterType, ?Type $flagsType): T
}

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

$type = $this->getFlagsValue($flagsType);
if (!$type instanceof ConstantIntegerType) {
$scalarValues = $type->getConstantScalarValues();
if ($scalarValues === []) {
return TrinaryLogic::createMaybe();
}

return TrinaryLogic::createFromBoolean(($type->getValue() & $flag) === $flag);
return TrinaryLogic::lazyExtremeIdentity(
$scalarValues,
static fn (bool|string|int|float|null $scalar): TrinaryLogic => TrinaryLogic::createFromBoolean(
(((int) $scalar) & $flag) === $flag,
),
);
}

private function getFlagsValue(Type $exprType): Type
Expand Down
11 changes: 11 additions & 0 deletions tests/PHPStan/Analyser/nsrt/filter-var.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,15 @@ public function scalars(bool $bool, float $float, int $int, string $string, int
assertType("''", filter_var(null));
}

public function randomFlag($mixed, bool $bool) {

assertType('int|false|null', filter_var($mixed, FILTER_VALIDATE_INT, [
'flags' => $bool ? FILTER_NULL_ON_FAILURE : FILTER_FLAG_NONE,
]));

assertType('bool|null', filter_var($mixed, FILTER_VALIDATE_BOOLEAN, [
'flags' => $bool ? FILTER_NULL_ON_FAILURE : FILTER_FLAG_NONE,
]));
}

}
Loading