Skip to content

Commit b05ab6e

Browse files
authored
[Php86] Fold to clamp() only when min <= max is proven in MinMaxToClampRector (#9058)
1 parent da60d90 commit b05ab6e

5 files changed

Lines changed: 57 additions & 19 deletions

File tree

‎rules-tests/Php86/Rector/FuncCall/MinMaxToClampRector/Fixture/outer_variable_inner_literal.php.inc‎

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php86\Rector\FuncCall\MinMaxToClampRector\Fixture;
4+
5+
fn (int $value) => max(10, min(5, $value));
6+
fn (int $value) => min(5, max(10, $value));
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php86\Rector\FuncCall\MinMaxToClampRector\Fixture;
4+
5+
fn (float $value) => max(NAN, min(5.0, $value));
6+
fn (float $value) => max(0.0, min(NAN, $value));
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php86\Rector\FuncCall\MinMaxToClampRector\Fixture;
4+
5+
fn (int $value, int $min) => max($min, min(100, $value));
6+
fn (int $value, int $max) => min($max, max(0, $value));

‎rules/Php86/Rector/FuncCall/MinMaxToClampRector.php‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,47 @@ private function createClampFuncCall(
122122
[$valueExpr, $innerBoundExpr] = $valueAndBound;
123123

124124
if ($this->isName($outerFuncCall, 'max')) {
125-
return $this->nodeFactory->createFuncCall('clamp', [$valueExpr, $outerBoundExpr, $innerBoundExpr]);
125+
[$minExpr, $maxExpr] = [$outerBoundExpr, $innerBoundExpr];
126+
} else {
127+
[$minExpr, $maxExpr] = [$innerBoundExpr, $outerBoundExpr];
126128
}
127129

128-
return $this->nodeFactory->createFuncCall('clamp', [$valueExpr, $innerBoundExpr, $outerBoundExpr]);
130+
// clamp() throws ValueError when min > max, while nested min()/max() returns a value
131+
if (! $this->isProvenMinLowerOrEqualMax($minExpr, $maxExpr)) {
132+
return null;
133+
}
134+
135+
return $this->nodeFactory->createFuncCall('clamp', [$valueExpr, $minExpr, $maxExpr]);
136+
}
137+
138+
private function isProvenMinLowerOrEqualMax(Expr $minExpr, Expr $maxExpr): bool
139+
{
140+
$minValues = $this->getNativeType($minExpr)
141+
->getConstantScalarValues();
142+
$maxValues = $this->getNativeType($maxExpr)
143+
->getConstantScalarValues();
144+
145+
if ($minValues === [] || $maxValues === []) {
146+
return false;
147+
}
148+
149+
// clamp() throws ValueError on NAN bounds as well
150+
foreach ([...$minValues, ...$maxValues] as $boundValue) {
151+
if (is_float($boundValue) && is_nan($boundValue)) {
152+
return false;
153+
}
154+
}
155+
156+
// constants like PHP_INT_MAX resolve to several platform values, all of them must hold
157+
foreach ($minValues as $minValue) {
158+
foreach ($maxValues as $maxValue) {
159+
if ($minValue > $maxValue) {
160+
return false;
161+
}
162+
}
163+
}
164+
165+
return true;
129166
}
130167

131168
private function isSupportedArg(Arg $arg): bool

0 commit comments

Comments
 (0)