Opened 7 weeks ago
Last modified 6 weeks ago
#65143 reviewing enhancement
Add polyfill for PHP 8.6's clamp() function
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 7.1 | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | General | Keywords: | has-patch |
| Focuses: | php-compatibility | Cc: |
Description
PHP 8.6 introduces a new built-in clamp() function (RFC) that clamps a value within a given range. If the value is within bounds, it is returned as-is; otherwise, the nearest bound is returned.
Since WordPress supports PHP 7.4 and above a polyfill is needed so WordPress and plugins can safely use clamp() without checking the PHP version.
Proposed Behavior
- If
$valueis within[$min, $max], returns$value. - If
$value > $max, returns$max. - If
$value < $min, returns$min. - Throws
ValueError(PHP >= 8.0) orInvalidArgumentException(PHP 7.x) if$min > $maxor if$min/$maxisNAN.
The polyfill relies on a new private helper _wp_throw_value_error(), which conditionally throws ValueError on PHP >= 8.0 and falls back to InvalidArgumentException on PHP 7.x — consistent with how Symfony handles the same compatibility problem.
Usage Examples
clamp( 5, 0, 100 ); // 5
clamp( -5, 0, 100 ); // 0
clamp( 105, 0, 100 ); // 100
clamp( 3.14, 0, 20 ); // 3.14
clamp( 3.14, 10, 20 ); // 10
clamp( 'P', 'A', 'Z' ); // "P"
clamp( 'P', 'X', 'Z' ); // "X"
clamp( new DateTimeImmutable('2026-01-08'), new DateTimeImmutable('2026-01-01'), new DateTimeImmutable('2026-12-31') );
// DateTimeImmutable('2026-01-08')
clamp( 4, NAN, 6 ); // ValueError: clamp(): Argument #2 ($min) cannot be NAN
clamp( 4, 8, 6 ); // ValueError: clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)
Changes
src/wp-includes/compat.php: Add_wp_throw_value_error()helper andclamp()polyfill.
References
- PHP RFC: https://wiki.php.net/rfc/clamp
- PHP implementation PR: https://github.com/php/php-src/pull/19434
- Symfony polyfill reference: https://github.com/symfony/polyfill-php86
Change History (5)
This ticket was mentioned in PR #11669 on WordPress/wordpress-develop by @Soean.
7 weeks ago
#1
- Keywords has-patch added
#2
@
7 weeks ago
This is the correct RFC: https://wiki.php.net/rfc/clamp_v2
#3
@
7 weeks ago
Are there places where clamp() should be used in core? I think generally a polyfill would be added if it is actually used.
This pull request adds a polyfill for the new PHP 8.6
clamp()function to ensure compatibility with older PHP versions. It also introduces a helper function to throw the appropriate error type depending on the PHP version.New function polyfills and helpers:
_wp_throw_value_error()to throw aValueErroron PHP 8.0+ or anInvalidArgumentExceptionon PHP 7.x, providing consistent error handling for polyfills.clamp()function, which restricts a value within a specified minimum and maximum, including input validation and appropriate error throwing for invalid arguments.Trac ticket: https://core.trac.wordpress.org/ticket/65143
## Use of AI Tools
AI assistance: Yes
Tool(s): GitHub Copilot
Model(s): Claude Opus 4.6
Used for: Code docs.