Skip to content

Commit

Permalink
Drop wrong float comparison for filter_var()
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored and ondrejmirtes committed Sep 26, 2024
1 parent ae53f11 commit fd304ca
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/Type/Php/FilterFunctionReturnTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
use function octdec;
use function preg_match;
use function sprintf;
use const PHP_FLOAT_EPSILON;

final class FilterFunctionReturnTypeHelper
{
Expand Down Expand Up @@ -293,7 +292,7 @@ private function determineExactType(Type $in, int $filterValue, Type $defaultTyp
}

if ($in instanceof ConstantFloatType) {
return $in->getValue() - (int) $in->getValue() <= PHP_FLOAT_EPSILON
return $in->getValue() - (int) $in->getValue() === 0.0
? $in->toInteger()
: $defaultType;
}
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Analyser/nsrt/filter-var.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,14 @@ public function scalars(bool $bool, float $float, int $int, string $string, int
assertType('bool|null', filter_var($float, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
assertType('bool|null', filter_var(17.0, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)); // could be null
assertType('bool|null', filter_var(17.1, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)); // could be null
assertType('bool|null', filter_var(1e-50, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)); // could be null
assertType('bool|null', filter_var($int, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
assertType('bool|null', filter_var($intRange, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
assertType('bool|null', filter_var(17, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)); // could be null
assertType('bool|null', filter_var($string, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
assertType('bool|null', filter_var($nonEmptyString, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
assertType('bool|null', filter_var('17', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)); // could be null
assertType('bool|null', filter_var('17.0', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)); // could be null
assertType('bool|null', filter_var('17.1', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)); // could be null
assertType('null', filter_var(null, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));

Expand All @@ -121,12 +123,14 @@ public function scalars(bool $bool, float $float, int $int, string $string, int
assertType('float', filter_var($float, FILTER_VALIDATE_FLOAT));
assertType('17.0', filter_var(17.0, FILTER_VALIDATE_FLOAT));
assertType('17.1', filter_var(17.1, FILTER_VALIDATE_FLOAT));
assertType('1.0E-50', filter_var(1e-50, FILTER_VALIDATE_FLOAT));
assertType('float', filter_var($int, FILTER_VALIDATE_FLOAT));
assertType('float', filter_var($intRange, FILTER_VALIDATE_FLOAT));
assertType('17.0', filter_var(17, FILTER_VALIDATE_FLOAT));
assertType('float|false', filter_var($string, FILTER_VALIDATE_FLOAT));
assertType('float|false', filter_var($nonEmptyString, FILTER_VALIDATE_FLOAT));
assertType('float|false', filter_var('17', FILTER_VALIDATE_FLOAT)); // could be 17.0
assertType('float|false', filter_var('17.0', FILTER_VALIDATE_FLOAT)); // could be 17.0
assertType('float|false', filter_var('17.1', FILTER_VALIDATE_FLOAT)); // could be 17.1
assertType('false', filter_var(null, FILTER_VALIDATE_FLOAT));

Expand All @@ -136,12 +140,14 @@ public function scalars(bool $bool, float $float, int $int, string $string, int
assertType('int|false', filter_var($float, FILTER_VALIDATE_INT));
assertType('17', filter_var(17.0, FILTER_VALIDATE_INT));
assertType('false', filter_var(17.1, FILTER_VALIDATE_INT));
assertType('false', filter_var(1e-50, FILTER_VALIDATE_INT));
assertType('int', filter_var($int, FILTER_VALIDATE_INT));
assertType('int<0, 9>', filter_var($intRange, FILTER_VALIDATE_INT));
assertType('17', filter_var(17, FILTER_VALIDATE_INT));
assertType('int|false', filter_var($string, FILTER_VALIDATE_INT));
assertType('int|false', filter_var($nonEmptyString, FILTER_VALIDATE_INT));
assertType('17', filter_var('17', FILTER_VALIDATE_INT));
assertType('false', filter_var('17.0', FILTER_VALIDATE_INT));
assertType('false', filter_var('17.1', FILTER_VALIDATE_INT));
assertType('false', filter_var(null, FILTER_VALIDATE_INT));

Expand All @@ -151,12 +157,14 @@ public function scalars(bool $bool, float $float, int $int, string $string, int
assertType('numeric-string', filter_var($float));
assertType("'17'", filter_var(17.0));
assertType("'17.1'", filter_var(17.1));
assertType("'1.0E-50'", filter_var(1e-50));
assertType('numeric-string', filter_var($int));
assertType('numeric-string', filter_var($intRange));
assertType("'17'", filter_var(17));
assertType('string', filter_var($string));
assertType('non-empty-string', filter_var($nonEmptyString));
assertType("'17'", filter_var('17'));
assertType("'17.0'", filter_var('17.0'));
assertType("'17.1'", filter_var('17.1'));
assertType("''", filter_var(null));
}
Expand Down

0 comments on commit fd304ca

Please sign in to comment.