diff --git a/src/Type/Php/MinMaxFunctionReturnTypeExtension.php b/src/Type/Php/MinMaxFunctionReturnTypeExtension.php index cf068f4a0d6..077007377c8 100644 --- a/src/Type/Php/MinMaxFunctionReturnTypeExtension.php +++ b/src/Type/Php/MinMaxFunctionReturnTypeExtension.php @@ -2,12 +2,15 @@ namespace PHPStan\Type\Php; +use PhpParser\Node\Expr; +use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual; use PhpParser\Node\Expr\BinaryOp\Smaller; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Ternary; use PHPStan\Analyser\Scope; use PHPStan\DependencyInjection\AutowiredService; use PHPStan\Node\Expr\AlwaysRememberedExpr; +use PHPStan\Node\Expr\TypeExpr; use PHPStan\Reflection\FunctionReflection; use PHPStan\Type\Constant\ConstantArrayType; use PHPStan\Type\Constant\ConstantBooleanType; @@ -49,7 +52,11 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, return new ErrorType(); } - // rewrite min($x, $y) as $x < $y ? $x : $y + // PHP evaluates min($x, $y) like $x < $y ? $x : $y or like $y < $x ? $y : $x, + // and max($x, $y) like $x < $y ? $y : $x or like $x >= $y ? $x : $y. + // Which one depends on the PHP version and on whether the call was compiled to a frameless call. + // They differ when the arguments compare as equal (min) or cannot be ordered (NAN), + // so the result is the union of both rewrites. // we don't handle arrays, which have different semantics $functionName = $functionReflection->getName(); if (count($args) === 2) { @@ -57,24 +64,28 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, $argType1 = $scope->getType($args[1]->value); if ($argType0->isArray()->no() && $argType1->isArray()->no()) { - $comparisonExpr = new Smaller( - new AlwaysRememberedExpr($args[0]->value, $argType0, $scope->getNativeType($args[0]->value)), - new AlwaysRememberedExpr($args[1]->value, $argType1, $scope->getNativeType($args[1]->value)), - ); + $left = $this->comparisonOperand($args[0]->value, $argType0, $scope); + $right = $this->comparisonOperand($args[1]->value, $argType1, $scope); if ($functionName === 'min') { - return $scope->getType(new Ternary( - $comparisonExpr, - $args[0]->value, - $args[1]->value, - )); - } elseif ($functionName === 'max') { - return $scope->getType(new Ternary( - $comparisonExpr, - $args[1]->value, - $args[0]->value, - )); + $rewrites = [ + new Ternary(new Smaller($left, $right), $args[0]->value, $args[1]->value), + new Ternary(new Smaller($right, $left), $args[1]->value, $args[0]->value), + ]; + } else { + $rewrites = [ + new Ternary(new Smaller($left, $right), $args[1]->value, $args[0]->value), + new Ternary(new GreaterOrEqual($left, $right), $args[0]->value, $args[1]->value), + ]; + } + + $type = $scope->getType($rewrites[0]); + if ($argType0->isInteger()->yes() && $argType1->isInteger()->yes()) { + // integers are totally ordered and equal integers are the same value + return $type; } + + return TypeCombinator::union($type, $scope->getType($rewrites[1])); } } @@ -250,4 +261,17 @@ private function compareTypes( return null; } + /** + * A single value cannot be narrowed, and narrowing it by a comparison with NAN + * would turn the branch that PHP can return into never. + */ + private function comparisonOperand(Expr $expr, Type $type, Scope $scope): Expr + { + if (count($type->getConstantScalarValues()) === 1) { + return new TypeExpr($type); + } + + return new AlwaysRememberedExpr($expr, $type, $scope->getNativeType($expr)); + } + } diff --git a/tests/PHPStan/Analyser/nsrt/minmax-nan.php b/tests/PHPStan/Analyser/nsrt/minmax-nan.php new file mode 100644 index 00000000000..0385e113f70 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/minmax-nan.php @@ -0,0 +1,144 @@ +', max(0.0, $f)); + assertType('NAN|float<0.0, max>', max($f, 0.0)); + assertType('NAN|float', min(1.0, $f)); + assertType('NAN|float', min($f, 1.0)); + assertType('float', max($f, $g)); + assertType('float', min($f, $g)); + + assertType('NAN|float<0.0, 1.0>', max(0.0, min(1.0, $f))); + assertType('NAN|float<0.0, 1.0>', min(1.0, max(0.0, $f))); + assertType('NAN|float<0.0, 1.0>', max(min($f, 1.0), 0.0)); + } + + /** + * @param float<0.0, 1.0> $unit + * @param float<10.0, 20.0> $high + */ + public function ranges(float $unit, float $high, float $f): void + { + assertType('float<10.0, 20.0>', max($unit, $high)); + assertType('float<10.0, 20.0>', max($high, $unit)); + assertType('float<0.0, 1.0>', min($unit, $high)); + assertType('float<0.0, 1.0>', min($high, $unit)); + assertType('float<0.0, 1.0>', max(0.0, min(1.0, $unit))); + + assertType('NAN|float<0.0, max>', max($unit, $f)); + assertType('NAN|float<0.0, max>', max($f, $unit)); + assertType('NAN|float', min($high, $f)); + assertType('NAN|float', min($f, $high)); + } + + public function nan(bool $b): void + { + assertType('1|NAN', max(1, NAN)); + assertType('1|NAN', max(NAN, 1)); + assertType('1|NAN', min(1, NAN)); + assertType('1|NAN', min(NAN, 1)); + assertType('NAN', max(NAN, NAN)); + + $n = $b ? NAN : 2.0; + assertType('1.0|2.0|NAN', max($n, 1.0)); + assertType('1.0|2.0|NAN', max(1.0, $n)); + assertType('2.0|3.0|NAN', min($n, 3.0)); + assertType('2.0|3.0|NAN', min(3.0, $n)); + } + + public function mixedNumbers(int $i, float $f, string $s): void + { + assertType('0|NAN|float<5.0E-324, max>', max(0, $f)); + assertType('0|NAN|float<0.0, max>', max($f, 0)); + assertType('float|int', max($i, $f)); + assertType('float|int', min($f, $i)); + assertType('float|string', max($f, $s)); + assertType("NAN|'1'", max('1', NAN)); + assertType("NAN|'1'", min(NAN, '1')); + } + + /** @param int<0, max> $count */ + public function ties(int $count): void + { + assertType('0', max(0, 0.0)); + assertType('0.0', max(0.0, 0)); + assertType('0|0.0', min(0, 0.0)); + assertType('0|0.0', min(0.0, 0)); + assertType('0.0', max(0.0, -0.0)); + assertType('-0.0|0.0', min(0.0, -0.0)); + assertType("1|'1'", min(1, '1')); + assertType('1', max(1, '1')); + assertType('0|0.0', min($count, 0.0)); + assertType('0|0.0', min(0.0, $count)); + } + + /** + * @param int<1, 10> $i + * @param int<5, 20> $j + */ + public function ints(int $i, int $j, int $k): void + { + assertType('int<5, 20>', max($i, $j)); + assertType('int<1, 10>', min($i, $j)); + assertType('int<1, max>', max($i, $k)); + assertType('int', min($i, $k)); + assertType('2', max(1, 2)); + assertType('1', min(1, 2)); + } + + public function negativeConstants(): void + { + assertType('-1|NAN', max(NAN, -1)); + assertType('-1|NAN', max(-1, NAN)); + assertType('-1|NAN', min(NAN, -1)); + assertType('-1|NAN', min(-1, NAN)); + } + + public function constantsNotWrittenAsLiterals(): void + { + assertType('-1|NAN', max(NAN, Foo::NEG)); + assertType('-1|NAN', max(Foo::NEG, NAN)); + assertType('-1|NAN', min(NAN, Foo::NEG)); + } + + /** + * @param 1|10 $x + */ + public function unionOfConstants(int $x): void + { + assertType('5|10', max($x, 5)); + assertType('1|5', min($x, 5)); + } + + /** @param int|float $n */ + public function intOrFloat($n, int $i): void + { + assertType('NAN|float|int', min($n, 100)); + assertType('NAN|float|int', min(100, $n)); + assertType('NAN|float<100.0, max>|int<100, max>', max($n, 100)); + assertType('NAN|float<100.00000000000001, max>|int<100, max>', max(100, $n)); + assertType('float|int', min($n, $i)); + assertType('float|int', min($i, $n)); + } + + public function callables(float $f): void + { + assertType('NAN|float<0.0, max>', call_user_func('max', 0.0, $f)); + assertType('NAN|float<0.0, max>', call_user_func('max', $f, 0.0)); + assertType('NAN|float', call_user_func('min', 1.0, $f)); + assertType('NAN|float', call_user_func('min', $f, 1.0)); + assertType('non-empty-list>', array_map('max', [0.0], [$f])); + assertType('non-empty-list>', array_map('max', [$f], [0.0])); + } + +} diff --git a/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php b/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php index d02b7bd2f44..1319a5ab2ce 100644 --- a/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php @@ -1295,6 +1295,11 @@ public function testFloatClassFunctions(): void ]); } + public function testMinMaxNan(): void + { + $this->analyse([__DIR__ . '/data/minmax-nan.php'], []); + } + public function testBug15147(): void { $this->analyse([__DIR__ . '/../Arrays/data/bug-15147.php'], []); diff --git a/tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php b/tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php index 3875567895f..2e6c6668c23 100644 --- a/tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php @@ -1287,4 +1287,9 @@ public function testBug15432(): void ]); } + public function testMinMaxNan(): void + { + $this->analyse([__DIR__ . '/data/minmax-nan.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Comparison/data/minmax-nan.php b/tests/PHPStan/Rules/Comparison/data/minmax-nan.php new file mode 100644 index 00000000000..797ff55b0ae --- /dev/null +++ b/tests/PHPStan/Rules/Comparison/data/minmax-nan.php @@ -0,0 +1,24 @@ +