Skip to content

Commit ee40a69

Browse files
committed
Improve handling of input type for filter_var with option FILTER_FLAG_EMPTY_STRING_NULL.
1 parent 5e185c3 commit ee40a69

2 files changed

Lines changed: 93 additions & 8 deletions

File tree

‎src/Type/Php/FilterFunctionReturnTypeHelper.php‎

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -389,18 +389,45 @@ private function determineExactType(Type $in, int $filterValue, Type $defaultTyp
389389
}
390390

391391
if ($filterValue === $this->getConstant('FILTER_DEFAULT')) {
392-
if ($this->hasFlag('FILTER_FLAG_EMPTY_STRING_NULL', $flagsType)->yes() && $in->isString()->yes()) {
393-
if ($in->isNonEmptyString()->yes()) {
394-
return $in;
392+
if ($this->hasFlag('FILTER_FLAG_EMPTY_STRING_NULL', $flagsType)->yes()) {
393+
if ($in->isFalse()->yes() || $in->isNull()->yes()) {
394+
return new NullType();
395395
}
396396

397-
if ($in->isNonEmptyString()->maybe()) {
398-
return new UnionType([new AccessoryNonEmptyStringType(), new NullType()]);
397+
if ($in->isFloat()->yes() || $in->isInteger()->yes() || $in->isTrue()->yes()) {
398+
return $in->toString();
399399
}
400400

401-
if ($in->isNonEmptyString()->no()) {
402-
return new NullType();
401+
if ($in->isString()->yes()) {
402+
if ($in->isNonEmptyString()->yes()) {
403+
return $in;
404+
}
405+
406+
if ($in->isNonEmptyString()->maybe()) {
407+
return new UnionType([new AccessoryNonEmptyStringType(), new NullType()]);
408+
}
409+
410+
if ($in->isNonEmptyString()->no()) {
411+
return new NullType();
412+
}
403413
}
414+
415+
$inString = $in->toString();
416+
if ($inString instanceof UnionType) {
417+
$inStringTypes = $inString->getTypes();
418+
// Replace ConstantStringType of value === "" by NullType.
419+
$types = array_filter(
420+
$inStringTypes,
421+
static fn (Type $type): bool => $type instanceof ConstantStringType && $type->getValue() !== '',
422+
);
423+
if ($types !== $inStringTypes) {
424+
$types[] = new NullType();
425+
}
426+
427+
return new UnionType($types);
428+
}
429+
430+
return new UnionType([new AccessoryNonEmptyStringType(), new NullType()]);
404431
}
405432

406433
if ($this->canStringBeSanitized($filterValue, $flagsType)->no() && $in->isString()->yes()) {

‎tests/PHPStan/Analyser/nsrt/filter-var-returns-non-empty-string.php‎

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,23 @@ class Foo
99
/**
1010
* @param non-empty-string $str
1111
* @param string $maybe_empty_string
12+
* @param null|string $nullable_string
13+
* @param null|non-empty-string $nullable_non_empty_string
14+
* @param int $int
1215
* @param positive-int $positive_int
1316
* @param negative-int $negative_int
17+
* @param bool $bool
1418
*/
15-
public function run(string $str, string $maybe_empty_string, int $int, int $positive_int, int $negative_int): void
19+
public function run(
20+
string $str,
21+
string $maybe_empty_string,
22+
null|string $nullable_string,
23+
null|string $nullable_non_empty_string,
24+
int $int,
25+
int $positive_int,
26+
int $negative_int,
27+
bool $bool,
28+
): void
1629
{
1730
assertType('non-empty-string', $str);
1831

@@ -139,5 +152,50 @@ public function run(string $str, string $maybe_empty_string, int $int, int $posi
139152

140153
$return = filter_var('0x10', FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_HEX);
141154
assertType('16', $return);
155+
156+
$return = filter_var(true, options: FILTER_FLAG_EMPTY_STRING_NULL);
157+
assertType("'1'", $return);
158+
159+
$return = filter_var(false, options: FILTER_FLAG_EMPTY_STRING_NULL);
160+
assertType('null', $return);
161+
162+
$return = filter_var($bool, options: FILTER_FLAG_EMPTY_STRING_NULL);
163+
assertType("'1'|null", $return);
164+
165+
$return = filter_var(0.0, options: FILTER_FLAG_EMPTY_STRING_NULL);
166+
assertType("'-0'|'0'", $return);
167+
168+
$return = filter_var(0, options: FILTER_FLAG_EMPTY_STRING_NULL);
169+
assertType("'0'", $return);
170+
171+
$return = filter_var(null, options: FILTER_FLAG_EMPTY_STRING_NULL);
172+
assertType('null', $return);
173+
174+
$return = filter_var($nullable_string, options: FILTER_FLAG_EMPTY_STRING_NULL);
175+
assertType('non-empty-string|null', $return);
176+
177+
$return = filter_var($nullable_non_empty_string, options: FILTER_FLAG_EMPTY_STRING_NULL);
178+
assertType('non-empty-string|null', $return);
179+
180+
$return = filter_var($this->anyOf(0.0, true), options: FILTER_FLAG_EMPTY_STRING_NULL);
181+
assertType("'-0'|'0'|'1'", $return);
182+
183+
$return = filter_var($this->anyOf($bool, $maybe_empty_string), options: FILTER_FLAG_EMPTY_STRING_NULL);
184+
assertType("non-empty-string|null", $return);
185+
186+
$return = filter_var($this->anyOf(0, null), options: FILTER_FLAG_EMPTY_STRING_NULL);
187+
assertType("'0'|null", $return);
188+
}
189+
190+
/**
191+
* @template T
192+
* @template U
193+
* @param T $a
194+
* @param U $b
195+
* @return T|U
196+
*/
197+
private function anyOf(mixed $a, mixed $b): mixed
198+
{
199+
return random_int(0, 1) ? $a : $b;
142200
}
143201
}

0 commit comments

Comments
 (0)