diff --git a/rules-tests/Php86/Rector/FuncCall/MinMaxToClampRector/Fixture/outer_variable_inner_literal.php.inc b/rules-tests/Php86/Rector/FuncCall/MinMaxToClampRector/Fixture/outer_variable_inner_literal.php.inc deleted file mode 100644 index 618d00b524d..00000000000 --- a/rules-tests/Php86/Rector/FuncCall/MinMaxToClampRector/Fixture/outer_variable_inner_literal.php.inc +++ /dev/null @@ -1,17 +0,0 @@ - max($min, min(100, $value)); -fn (int $value, int $max) => min($max, max(0, $value)); - -?> ------ - clamp($value, $min, 100); -fn (int $value, int $max) => clamp($value, 0, $max); - -?> diff --git a/rules-tests/Php86/Rector/FuncCall/MinMaxToClampRector/Fixture/skip_inverted_bounds.php.inc b/rules-tests/Php86/Rector/FuncCall/MinMaxToClampRector/Fixture/skip_inverted_bounds.php.inc new file mode 100644 index 00000000000..ec2c7064fdb --- /dev/null +++ b/rules-tests/Php86/Rector/FuncCall/MinMaxToClampRector/Fixture/skip_inverted_bounds.php.inc @@ -0,0 +1,6 @@ + max(10, min(5, $value)); +fn (int $value) => min(5, max(10, $value)); diff --git a/rules-tests/Php86/Rector/FuncCall/MinMaxToClampRector/Fixture/skip_nan.php.inc b/rules-tests/Php86/Rector/FuncCall/MinMaxToClampRector/Fixture/skip_nan.php.inc new file mode 100644 index 00000000000..3c4fd360325 --- /dev/null +++ b/rules-tests/Php86/Rector/FuncCall/MinMaxToClampRector/Fixture/skip_nan.php.inc @@ -0,0 +1,6 @@ + max(NAN, min(5.0, $value)); +fn (float $value) => max(0.0, min(NAN, $value)); diff --git a/rules-tests/Php86/Rector/FuncCall/MinMaxToClampRector/Fixture/skip_outer_variable_inner_literal.php.inc b/rules-tests/Php86/Rector/FuncCall/MinMaxToClampRector/Fixture/skip_outer_variable_inner_literal.php.inc new file mode 100644 index 00000000000..eb1be83004f --- /dev/null +++ b/rules-tests/Php86/Rector/FuncCall/MinMaxToClampRector/Fixture/skip_outer_variable_inner_literal.php.inc @@ -0,0 +1,6 @@ + max($min, min(100, $value)); +fn (int $value, int $max) => min($max, max(0, $value)); diff --git a/rules/Php86/Rector/FuncCall/MinMaxToClampRector.php b/rules/Php86/Rector/FuncCall/MinMaxToClampRector.php index aef0937d0e8..f7414558f1a 100644 --- a/rules/Php86/Rector/FuncCall/MinMaxToClampRector.php +++ b/rules/Php86/Rector/FuncCall/MinMaxToClampRector.php @@ -122,10 +122,47 @@ private function createClampFuncCall( [$valueExpr, $innerBoundExpr] = $valueAndBound; if ($this->isName($outerFuncCall, 'max')) { - return $this->nodeFactory->createFuncCall('clamp', [$valueExpr, $outerBoundExpr, $innerBoundExpr]); + [$minExpr, $maxExpr] = [$outerBoundExpr, $innerBoundExpr]; + } else { + [$minExpr, $maxExpr] = [$innerBoundExpr, $outerBoundExpr]; } - return $this->nodeFactory->createFuncCall('clamp', [$valueExpr, $innerBoundExpr, $outerBoundExpr]); + // clamp() throws ValueError when min > max, while nested min()/max() returns a value + if (! $this->isProvenMinLowerOrEqualMax($minExpr, $maxExpr)) { + return null; + } + + return $this->nodeFactory->createFuncCall('clamp', [$valueExpr, $minExpr, $maxExpr]); + } + + private function isProvenMinLowerOrEqualMax(Expr $minExpr, Expr $maxExpr): bool + { + $minValues = $this->getNativeType($minExpr) + ->getConstantScalarValues(); + $maxValues = $this->getNativeType($maxExpr) + ->getConstantScalarValues(); + + if ($minValues === [] || $maxValues === []) { + return false; + } + + // clamp() throws ValueError on NAN bounds as well + foreach ([...$minValues, ...$maxValues] as $boundValue) { + if (is_float($boundValue) && is_nan($boundValue)) { + return false; + } + } + + // constants like PHP_INT_MAX resolve to several platform values, all of them must hold + foreach ($minValues as $minValue) { + foreach ($maxValues as $maxValue) { + if ($minValue > $maxValue) { + return false; + } + } + } + + return true; } private function isSupportedArg(Arg $arg): bool