Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/wp-includes/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -1461,12 +1461,34 @@ function is_multisite() {
* Converts a value to non-negative integer.
*
* @since 2.5.0
* @since 7.2.0 The `int` return type was added. Values beyond the integer
* range are now capped at `PHP_INT_MAX` rather than overflowing.
*
* @param mixed $maybeint Data you wish to have converted to a non-negative integer.
* @return int A non-negative integer.
Comment thread
josephscott marked this conversation as resolved.
* @phpstan-return non-negative-int
*/
function absint( $maybeint ) {
return abs( (int) $maybeint );
function absint( $maybeint ): int {
if ( is_float( $maybeint ) ) {
if ( ! is_finite( $maybeint ) ) {
// Casting `NAN` or `INF` to int has produced `0` since PHP 7.0.
return 0;
}

if ( $maybeint <= (float) PHP_INT_MIN || $maybeint >= (float) PHP_INT_MAX ) {
// Casting a float beyond the integer range is unreliable and warns as of PHP 8.5.
return PHP_INT_MAX;
}
}

$maybeint = (int) $maybeint;
Comment thread
josephscott marked this conversation as resolved.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If $maybeint is an object, then PHP issues a warning like:

Object of class stdClass could not be converted to int

This is probably desired, however.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both the original and PR versions of absint() return 1 in that case - https://3v4l.org/nvmke

It looks like an object is always going to be 1, so we could add a simple case for that - https://3v4l.org/EMGrO


if ( PHP_INT_MIN === $maybeint ) {
// `abs( PHP_INT_MIN )` overflows to a float, as `PHP_INT_MAX` is one less than `-PHP_INT_MIN`.
return PHP_INT_MAX;
}

return abs( $maybeint );
}

/**
Expand Down
Loading
Loading