Skip to content

Add a dynamic return type extension for clamp() - #6684

Open
Amoifr wants to merge 1 commit into
phpstan:2.3.xfrom
Amoifr:feature-15366-clamp-return-type
Open

Amoifr wants to merge 1 commit into
phpstan:2.3.xfrom
Amoifr:feature-15366-clamp-return-type

Conversation

@Amoifr

@Amoifr Amoifr commented Oct 5, 2026

Copy link
Copy Markdown

Closes phpstan/phpstan#15366

PHP 8.6 adds clamp(mixed $value, mixed $min, mixed $max): mixed. php_math_clamp() returns a copy of one of its three arguments: $max when $max < $value, otherwise $min when $value < $min, otherwise $value.

The new extension rewrites the call into that ternary, with AlwaysRememberedExpr, the way MinMaxFunctionReturnTypeExtension handles min($a, $b) and max($a, $b). So clamp($int, 1, 10) is int<1, 10>, clamp($int, 1.0, 10.0) is 1.0|10.0|int<1, 10>, and constant arguments give a constant result. Arrays, which PHP compares by size first, and unpacked arguments keep the native mixed.

The type inference test needs clamp() at runtime, so it is gated with // lint >= 8.6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add dynamic return type extension for clamp function

1 participant