Skip to content

[Php86] Fold to clamp() only when min <= max is proven in MinMaxToClampRector - #9058

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-min-max-to-clamp-bounds
Nov 24, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
fix-min-max-to-clamp-bounds

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

clamp() throws ValueError when $min > $max (or a bound is NAN), while nested min()/max() just returns a value. The rule folded without comparing the bounds:

max(10, min(5, $value));            // always 10
// became
clamp($value, 10, 5);               // ValueError

fn (int $value, int $min) => max($min, min(100, $value));
// became
fn (int $value, int $min) => clamp($value, $min, 100); // ValueError when $min > 100

Fix: fold only when both bounds resolve to constant scalar values (literals, PHP_INT_MIN/PHP_INT_MAX, a known constant-valued variable), none is NAN, and every possible min value is <= every possible max value. Cases already proven (max(0, min(100, $value)), PHP_INT_MIN/PHP_INT_MAX, 'a'/'z') keep folding.

  • outer_variable_inner_literal fixture becomes skip_outer_variable_inner_literal, as an int $min param can be greater than 100
  • added skip_inverted_bounds and skip_nan fixtures

@TomasVotruba
TomasVotruba merged commit b05ab6e into main Nov 24, 2026
51 checks passed
@TomasVotruba
TomasVotruba deleted the fix-min-max-to-clamp-bounds branch November 24, 2026 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant