Skip to content

Commit 5e185c3

Browse files
committed
Fix #13963 with unit tests.
1 parent 9e9777c commit 5e185c3

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎src/Type/Php/FilterFunctionReturnTypeHelper.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,20 @@ 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;
395+
}
396+
397+
if ($in->isNonEmptyString()->maybe()) {
398+
return new UnionType([new AccessoryNonEmptyStringType(), new NullType()]);
399+
}
400+
401+
if ($in->isNonEmptyString()->no()) {
402+
return new NullType();
403+
}
404+
}
405+
392406
if ($this->canStringBeSanitized($filterValue, $flagsType)->no() && $in->isString()->yes()) {
393407
return $in;
394408
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,23 @@ class Foo
88
{
99
/**
1010
* @param non-empty-string $str
11+
* @param string $maybe_empty_string
1112
* @param positive-int $positive_int
1213
* @param negative-int $negative_int
1314
*/
14-
public function run(string $str, int $int, int $positive_int, int $negative_int): void
15+
public function run(string $str, string $maybe_empty_string, int $int, int $positive_int, int $negative_int): void
1516
{
1617
assertType('non-empty-string', $str);
1718

1819
$return = filter_var($str, FILTER_DEFAULT);
1920
assertType('non-empty-string', $return);
2021

22+
$return = filter_var($str, FILTER_DEFAULT, FILTER_FLAG_EMPTY_STRING_NULL);
23+
assertType('non-empty-string', $return);
24+
25+
$return = filter_var($maybe_empty_string, FILTER_DEFAULT, FILTER_FLAG_EMPTY_STRING_NULL);
26+
assertType('non-empty-string|null', $return);
27+
2128
$return = filter_var($str, FILTER_DEFAULT, FILTER_FLAG_STRIP_LOW);
2229
assertType('string|false', $return);
2330

@@ -100,6 +107,9 @@ public function run(string $str, int $int, int $positive_int, int $negative_int)
100107
$return = filter_var($str2, FILTER_DEFAULT);
101108
assertType("''", $return);
102109

110+
$return = filter_var('', FILTER_DEFAULT, FILTER_FLAG_EMPTY_STRING_NULL);
111+
assertType('null', $return);
112+
103113
$return = filter_var($str2, FILTER_VALIDATE_URL);
104114
assertType('non-falsy-string|false', $return);
105115

0 commit comments

Comments
 (0)