Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 40 additions & 16 deletions src/Type/Php/MinMaxFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,32 +52,40 @@ 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) {
$argType0 = $scope->getType($args[0]->value);
$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()) {
Comment thread
staabm marked this conversation as resolved.
Resolved
// integers are totally ordered and equal integers are the same value
return $type;
}

return TypeCombinator::union($type, $scope->getType($rewrites[1]));
}
}

Expand Down Expand Up @@ -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));
}

}
144 changes: 144 additions & 0 deletions tests/PHPStan/Analyser/nsrt/minmax-nan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php declare(strict_types = 1);

namespace MinMaxNan;

use function PHPStan\Testing\assertType;

class Foo
{

public const NEG = -1;

public function floats(float $f, float $g): void
{
assertType('NAN|float<0.0, max>', max(0.0, $f));
assertType('NAN|float<0.0, max>', max($f, 0.0));
assertType('NAN|float<min, 1.0>', min(1.0, $f));
assertType('NAN|float<min, 1.0>', 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, 20.0>', min($high, $f));
assertType('NAN|float<min, 20.0>', 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, 10>', 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<min, 100.0>|int<min, 100>', min($n, 100));
assertType('NAN|float<min, 100.0>|int<min, 100>', 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<min, 1.0>', call_user_func('min', 1.0, $f));
assertType('NAN|float<min, 1.0>', call_user_func('min', $f, 1.0));
assertType('non-empty-list<NAN|float<0.0, max>>', array_map('max', [0.0], [$f]));
assertType('non-empty-list<NAN|float<0.0, max>>', array_map('max', [$f], [0.0]));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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'], []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1287,4 +1287,9 @@ public function testBug15432(): void
]);
}

public function testMinMaxNan(): void
{
$this->analyse([__DIR__ . '/data/minmax-nan.php'], []);
}

}
24 changes: 24 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/minmax-nan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types = 1);

namespace MinMaxNanComparison;

class Foo
{

public function max(float $f): bool
{
return is_nan(max(0.0, $f));
}

public function clamp(float $f): bool
{
return is_nan(\max(0.0, \min(1.0, $f)));
}

/** @param int|float $n */
public function min($n): bool
{
return \min($n, 100) === 100.0;
}

}
Loading