From 8ef92fc6b50aa7e5e39a6e024b51bdfaaf6a6733 Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Fri, 13 Nov 2026 10:03:43 +0900 Subject: [PATCH 1/6] Type min() / max() of two arguments as the union of both comparison orders MinMaxFunctionReturnTypeExtension typed min($a, $b) as $a < $b ? $a : $b and max($a, $b) as $a < $b ? $b : $a. Which argument PHP returns when the two compare as equal or one of them is NAN depends on the PHP version and on how the call was compiled. PHP 8.4 added a frameless handler for direct calls, which disagrees with the function body that call_user_func(), array_map() and unpacked calls use. A direct call also falls back to the function body under Xdebug and for unqualified nested calls inside a namespace. PHPStan cannot tell these apart. Since float ranges, the comparison in the rewrite narrows floats, so picking one order dropped values PHP returns: max(0.0, $float) was float<0.0, max> although max(0.0, NAN) is NAN on PHP 7.4 to 8.2 and for direct calls on 8.4+. Regardless of float ranges, min(0, 0.0) was 0.0 although it is 0 on PHP 8.3 and older. For two arguments that cannot be arrays, return the union of both rewrites: $a < $b ? $a : $b and $b < $a ? $b : $a for min(), and $a < $b ? $b : $a and $a >= $b ? $a : $b for max(). Integers are totally ordered and equal integers are the same value, so for two integers the first rewrite is enough. --- .../Php/MinMaxFunctionReturnTypeExtension.php | 41 ++++--- tests/PHPStan/Analyser/nsrt/minmax-nan.php | 107 ++++++++++++++++++ 2 files changed, 132 insertions(+), 16 deletions(-) create mode 100644 tests/PHPStan/Analyser/nsrt/minmax-nan.php diff --git a/src/Type/Php/MinMaxFunctionReturnTypeExtension.php b/src/Type/Php/MinMaxFunctionReturnTypeExtension.php index cf068f4a0d6..daea8a666f5 100644 --- a/src/Type/Php/MinMaxFunctionReturnTypeExtension.php +++ b/src/Type/Php/MinMaxFunctionReturnTypeExtension.php @@ -2,6 +2,7 @@ namespace PHPStan\Type\Php; +use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual; use PhpParser\Node\Expr\BinaryOp\Smaller; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Ternary; @@ -49,7 +50,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 +62,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 = new AlwaysRememberedExpr($args[0]->value, $argType0, $scope->getNativeType($args[0]->value)); + $right = new AlwaysRememberedExpr($args[1]->value, $argType1, $scope->getNativeType($args[1]->value)); 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])); } } diff --git a/tests/PHPStan/Analyser/nsrt/minmax-nan.php b/tests/PHPStan/Analyser/nsrt/minmax-nan.php new file mode 100644 index 00000000000..7a756b96afe --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/minmax-nan.php @@ -0,0 +1,107 @@ +', 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 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])); + } + +} From eefc51e588150834fd5ba82a9bf051bb98a12dee Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Fri, 13 Nov 2026 16:01:55 +0900 Subject: [PATCH 2/6] Test min() / max() with NAN and int|float arguments in the comparison rules Kill the mutants of the integer shortcut in MinMaxFunctionReturnTypeExtension. --- tests/PHPStan/Analyser/nsrt/minmax-nan.php | 10 ++++++++ ...mpossibleCheckTypeFunctionCallRuleTest.php | 5 ++++ ...rictComparisonOfDifferentTypesRuleTest.php | 5 ++++ .../Rules/Comparison/data/minmax-nan.php | 23 +++++++++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 tests/PHPStan/Rules/Comparison/data/minmax-nan.php diff --git a/tests/PHPStan/Analyser/nsrt/minmax-nan.php b/tests/PHPStan/Analyser/nsrt/minmax-nan.php index 7a756b96afe..e3bbeedc531 100644 --- a/tests/PHPStan/Analyser/nsrt/minmax-nan.php +++ b/tests/PHPStan/Analyser/nsrt/minmax-nan.php @@ -94,6 +94,16 @@ public function ints(int $i, int $j, int $k): void assertType('1', min(1, 2)); } + public function intOrFloat(int|float $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)); 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..31a51635022 --- /dev/null +++ b/tests/PHPStan/Rules/Comparison/data/minmax-nan.php @@ -0,0 +1,23 @@ + Date: Fri, 13 Nov 2026 16:36:25 +0900 Subject: [PATCH 3/6] Declare int|float in the min() / max() fixtures with PHPDoc so they lint on PHP 7.4 --- tests/PHPStan/Analyser/nsrt/minmax-nan.php | 3 ++- tests/PHPStan/Rules/Comparison/data/minmax-nan.php | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/PHPStan/Analyser/nsrt/minmax-nan.php b/tests/PHPStan/Analyser/nsrt/minmax-nan.php index e3bbeedc531..0d2b3a2af76 100644 --- a/tests/PHPStan/Analyser/nsrt/minmax-nan.php +++ b/tests/PHPStan/Analyser/nsrt/minmax-nan.php @@ -94,7 +94,8 @@ public function ints(int $i, int $j, int $k): void assertType('1', min(1, 2)); } - public function intOrFloat(int|float $n, int $i): void + /** @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)); diff --git a/tests/PHPStan/Rules/Comparison/data/minmax-nan.php b/tests/PHPStan/Rules/Comparison/data/minmax-nan.php index 31a51635022..797ff55b0ae 100644 --- a/tests/PHPStan/Rules/Comparison/data/minmax-nan.php +++ b/tests/PHPStan/Rules/Comparison/data/minmax-nan.php @@ -15,7 +15,8 @@ public function clamp(float $f): bool return is_nan(\max(0.0, \min(1.0, $f))); } - public function min(int|float $n): bool + /** @param int|float $n */ + public function min($n): bool { return \min($n, 100) === 100.0; } From 021679f85c15aee9fcab69384db341d0c9d8b344 Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Fri, 13 Nov 2026 21:40:53 +0900 Subject: [PATCH 4/6] Do not narrow constant arguments of min() / max() by the comparison in the rewrite The comparison with NAN narrowed a constant such as -1 to never in the branch that returns it. --- src/Type/Php/MinMaxFunctionReturnTypeExtension.php | 11 +++++++++-- tests/PHPStan/Analyser/nsrt/minmax-nan.php | 8 ++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Type/Php/MinMaxFunctionReturnTypeExtension.php b/src/Type/Php/MinMaxFunctionReturnTypeExtension.php index daea8a666f5..de9ba76b23a 100644 --- a/src/Type/Php/MinMaxFunctionReturnTypeExtension.php +++ b/src/Type/Php/MinMaxFunctionReturnTypeExtension.php @@ -9,6 +9,7 @@ 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; @@ -62,8 +63,14 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, $argType1 = $scope->getType($args[1]->value); if ($argType0->isArray()->no() && $argType1->isArray()->no()) { - $left = new AlwaysRememberedExpr($args[0]->value, $argType0, $scope->getNativeType($args[0]->value)); - $right = new AlwaysRememberedExpr($args[1]->value, $argType1, $scope->getNativeType($args[1]->value)); + // the comparison of a constant must not narrow the constant in the branches, + // as the narrowing by a comparison with NAN would turn it into never + $left = $argType0->isConstantScalarValue()->yes() + ? new TypeExpr($argType0) + : new AlwaysRememberedExpr($args[0]->value, $argType0, $scope->getNativeType($args[0]->value)); + $right = $argType1->isConstantScalarValue()->yes() + ? new TypeExpr($argType1) + : new AlwaysRememberedExpr($args[1]->value, $argType1, $scope->getNativeType($args[1]->value)); if ($functionName === 'min') { $rewrites = [ diff --git a/tests/PHPStan/Analyser/nsrt/minmax-nan.php b/tests/PHPStan/Analyser/nsrt/minmax-nan.php index 0d2b3a2af76..45b549dd03b 100644 --- a/tests/PHPStan/Analyser/nsrt/minmax-nan.php +++ b/tests/PHPStan/Analyser/nsrt/minmax-nan.php @@ -94,6 +94,14 @@ public function ints(int $i, int $j, int $k): void 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)); + } + /** @param int|float $n */ public function intOrFloat($n, int $i): void { From 14e39d654c5e9e52b90934a58140c9a549848e87 Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Fri, 13 Nov 2026 22:29:56 +0900 Subject: [PATCH 5/6] Keep literal arguments of min() / max() out of AlwaysRememberedExpr Narrowing skips literals, so a comparison with NAN no longer turns one into never, and an argument whose type is a union of constants is narrowed again. --- .../Php/MinMaxFunctionReturnTypeExtension.php | 25 ++++++++++++------- tests/PHPStan/Analyser/nsrt/minmax-nan.php | 9 +++++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/Type/Php/MinMaxFunctionReturnTypeExtension.php b/src/Type/Php/MinMaxFunctionReturnTypeExtension.php index de9ba76b23a..912bf003d05 100644 --- a/src/Type/Php/MinMaxFunctionReturnTypeExtension.php +++ b/src/Type/Php/MinMaxFunctionReturnTypeExtension.php @@ -2,14 +2,16 @@ 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 PhpParser\Node\Expr\UnaryMinus; +use PhpParser\Node\Scalar; 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; @@ -63,14 +65,10 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, $argType1 = $scope->getType($args[1]->value); if ($argType0->isArray()->no() && $argType1->isArray()->no()) { - // the comparison of a constant must not narrow the constant in the branches, - // as the narrowing by a comparison with NAN would turn it into never - $left = $argType0->isConstantScalarValue()->yes() - ? new TypeExpr($argType0) - : new AlwaysRememberedExpr($args[0]->value, $argType0, $scope->getNativeType($args[0]->value)); - $right = $argType1->isConstantScalarValue()->yes() - ? new TypeExpr($argType1) - : new AlwaysRememberedExpr($args[1]->value, $argType1, $scope->getNativeType($args[1]->value)); + // a literal in the comparison must stay a literal: narrowing skips those, + // and a comparison with NAN would otherwise turn the literal into never + $left = $this->rememberType($args[0]->value, $argType0, $scope); + $right = $this->rememberType($args[1]->value, $argType1, $scope); if ($functionName === 'min') { $rewrites = [ @@ -266,4 +264,13 @@ private function compareTypes( return null; } + private function rememberType(Expr $expr, Type $type, Scope $scope): Expr + { + if ($expr instanceof Scalar || ($expr instanceof UnaryMinus && $expr->expr instanceof Scalar)) { + return $expr; + } + + 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 index 45b549dd03b..0e3e99614ae 100644 --- a/tests/PHPStan/Analyser/nsrt/minmax-nan.php +++ b/tests/PHPStan/Analyser/nsrt/minmax-nan.php @@ -102,6 +102,15 @@ public function negativeConstants(): void assertType('-1|NAN', min(-1, NAN)); } + /** + * @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 { From 48e94fcef4149e7e84aa373de920b10398b9caf9 Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Sat, 14 Nov 2026 00:15:46 +0900 Subject: [PATCH 6/6] Decide the comparison operands of min() / max() by the type, not the node A constant that is not written as a literal, such as a class constant, went through AlwaysRememberedExpr and the comparison with NAN narrowed it to never. --- .../Php/MinMaxFunctionReturnTypeExtension.php | 19 ++++++++++--------- tests/PHPStan/Analyser/nsrt/minmax-nan.php | 9 +++++++++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Type/Php/MinMaxFunctionReturnTypeExtension.php b/src/Type/Php/MinMaxFunctionReturnTypeExtension.php index 912bf003d05..077007377c8 100644 --- a/src/Type/Php/MinMaxFunctionReturnTypeExtension.php +++ b/src/Type/Php/MinMaxFunctionReturnTypeExtension.php @@ -7,11 +7,10 @@ use PhpParser\Node\Expr\BinaryOp\Smaller; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Ternary; -use PhpParser\Node\Expr\UnaryMinus; -use PhpParser\Node\Scalar; 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; @@ -65,10 +64,8 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, $argType1 = $scope->getType($args[1]->value); if ($argType0->isArray()->no() && $argType1->isArray()->no()) { - // a literal in the comparison must stay a literal: narrowing skips those, - // and a comparison with NAN would otherwise turn the literal into never - $left = $this->rememberType($args[0]->value, $argType0, $scope); - $right = $this->rememberType($args[1]->value, $argType1, $scope); + $left = $this->comparisonOperand($args[0]->value, $argType0, $scope); + $right = $this->comparisonOperand($args[1]->value, $argType1, $scope); if ($functionName === 'min') { $rewrites = [ @@ -264,10 +261,14 @@ private function compareTypes( return null; } - private function rememberType(Expr $expr, Type $type, Scope $scope): Expr + /** + * 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 ($expr instanceof Scalar || ($expr instanceof UnaryMinus && $expr->expr instanceof Scalar)) { - return $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 index 0e3e99614ae..0385e113f70 100644 --- a/tests/PHPStan/Analyser/nsrt/minmax-nan.php +++ b/tests/PHPStan/Analyser/nsrt/minmax-nan.php @@ -7,6 +7,8 @@ class Foo { + public const NEG = -1; + public function floats(float $f, float $g): void { assertType('NAN|float<0.0, max>', max(0.0, $f)); @@ -102,6 +104,13 @@ public function negativeConstants(): void 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 */