Skip to content

Commit

Permalink
Do not loose string type on filter_var() with default filter and no…
Browse files Browse the repository at this point in the history
… flags
  • Loading branch information
herndlm committed Dec 28, 2022
1 parent 6e5c75f commit a7ec9e0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/Type/Php/FilterVarDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ private function determineExactType(Type $in, int $filterValue, Type $defaultTyp
}

if ($filterValue === $this->getConstant('FILTER_DEFAULT')) {
if (!$this->canStringBeSanitized($filterValue, $flagsType) && $in->isString()->yes()) {
return $in;
}

if ($in->isBoolean()->yes() || $in->isFloat()->yes() || $in->isInteger()->yes() || $in->isNull()->yes()) {
return $in->toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function run(string $str, int $int, int $positive_int, int $negative_int)
assertType('non-empty-string', $str);

$return = filter_var($str, FILTER_DEFAULT);
assertType('non-empty-string|false', $return);
assertType('non-empty-string', $return);

$return = filter_var($str, FILTER_DEFAULT, FILTER_FLAG_STRIP_LOW);
assertType('string|false', $return);
Expand Down Expand Up @@ -98,7 +98,7 @@ public function run(string $str, int $int, int $positive_int, int $negative_int)

$str2 = '';
$return = filter_var($str2, FILTER_DEFAULT);
assertType('string|false', $return);
assertType("''", $return);

$return = filter_var($str2, FILTER_VALIDATE_URL);
assertType('string|false', $return);
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Analyser/data/filter-var.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ public function scalars(bool $bool, float $float, int $int, string $string, int
assertType('numeric-string', filter_var($int));
assertType('numeric-string', filter_var($intRange));
assertType("'17'", filter_var(17));
assertType('string|false', filter_var($string));
assertType('non-empty-string|false', filter_var($nonEmptyString)); // could be non-empty-string
assertType('non-falsy-string|false', filter_var('17')); // could be '17'
assertType('string', filter_var($string));
assertType('non-empty-string', filter_var($nonEmptyString));
assertType("'17'", filter_var('17'));
assertType("''", filter_var(null));
}

Expand Down

0 comments on commit a7ec9e0

Please sign in to comment.