Opened 5 months ago
Closed 3 months ago
#65143 closed enhancement (fixed)
Add polyfill for PHP 8.6's clamp() function
| Reported by: | Soean | Owned by: | westonruter |
|---|---|---|---|
| Priority: | normal | Milestone: | 7.1 |
| Component: | General | Version: | |
| Severity: | normal | Keywords: | has-patch |
| Cc: | Focuses: | php-compatibility |
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 (9)
This ticket was mentioned in PR #11669 on WordPress/wordpress-develop by @Soean.
5 months ago
#1
- Keywords has-patch added
#2
@
5 months ago
This is the correct RFC: https://wiki.php.net/rfc/clamp_v2
#3
@
5 months ago
Are there places where clamp() should be used in core? I think generally a polyfill would be added if it is actually used.
#4
@
5 months ago
@westonruter I added two places to the PR, where we can use it in Core.
Providing a polyfill in WordPress Core ensures that plugin and theme developers can use it consistently across all supported PHP versions without needing to implement their own.
#5
@
5 months ago
- Milestone Awaiting Review → 7.1
- Owner set to
- Status new → reviewing
Great. Let's get this in the queue for 7.1.
#6
@
3 months ago
As the release of 7.1 Beta1 is approaching, I would like to check the status of this ticket. What is the status of the attached PR?
@westonruter commented on PR #11669:
3 months ago
#8
- Added
_wp_throw_value_error()to throw aValueErroron PHP 8.0+ or anInvalidArgumentExceptionon PHP 7.x, providing consistent error handling for polyfills.
In d33db07, I opted to move this into a closure since it was only being used in one function. We can re-extract if we need to use it in another polyfill.
I added the necessary PHPDoc to ensure the clamp() function doesn't introduce PHPStan errors due to the mixed return type.
I also fixed the PHPStan issues with wp_get_typography_value_and_unit() which were causing PHPStan errors with the new clamp() usage. Similarly, I fixed the PHPStan errors in the other clamp() usage in embed.php by providing a type definition for the variable being clamped.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
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.