-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Simplified improvement for absint() that always returns an int #12944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
87227df
905cbdc
d580a24
7a83559
e2ecfa3
29b6dad
5143333
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
| * @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; | ||
|
josephscott marked this conversation as resolved.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If
This is probably desired, however.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both the original and PR versions of It looks like an object is always going to be
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, good idea to add a test case for all possible input types, so we can codify the existing behavior (including expected warnings).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added tests for more types. |
||
|
|
||
| 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 ); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php | ||
| /** | ||
| * Enum fixtures for the absint() tests. | ||
| * | ||
| * Enum syntax requires PHP 8.1, so these are kept out of the test class file, | ||
| * which must remain parseable on the minimum supported PHP version. | ||
| * | ||
| * @see Tests_Functions_Absint | ||
| */ | ||
|
|
||
| enum Absint_Test_Pure_Enum { | ||
| case Hearts; | ||
| } | ||
|
|
||
| enum Absint_Test_Backed_Enum: int { | ||
| case Ace = 1; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.