Skip to content

[Php86] Require constant ordered bounds in MinMaxToClampRector - #9047

Closed
TomasVotruba wants to merge 1 commit into
mainfrom
min-max-to-clamp-constant-bounds
Closed

TomasVotruba wants to merge 1 commit into
mainfrom
min-max-to-clamp-constant-bounds

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

clamp() is not a drop-in replacement for nested max()/min() - per the clamp RFC it throws ValueError when $min > $max or when a bound is NAN, while max($min, min($max, $value)) just returns a value.

The rule converted dynamic bounds:

fn (int $value, int $min) => max($min, min(100, $value));
-max($min, min(100, $value));
+clamp($value, $min, 100);

With $min = 150 the original returns 150, the result throws ValueError: clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max). Same for literal bounds in the wrong order, e.g. max(100, min(0, $value)).

The fix

Convert only when both bounds are constant expressions (literals, constants, constant-folded expressions like -10 or 10 * 2) and every possible min value is <= every possible max value (covers platform-dependent PHP_INT_MIN/PHP_INT_MAX), and neither is NAN.

Variable bounds ($min, $limit) are now skipped, outer_variable_inner_literal and the $limit cases of known_variable_and_constant_bounds became skip fixtures, plus a new skip_min_greater_than_max fixture.

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