diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 5b59d393213ac..dd42e3ad916f6 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -460,11 +460,19 @@ function number_format_i18n( $number, $decimals = 0 ) { * @since 2.3.0 * @since 6.0.0 Support for PB, EB, ZB, and YB was added. * - * @param int|string $bytes Number of bytes. Note max integer size for integers. - * @param int $decimals Optional. Precision of number of decimal places. Default 0. + * @param int|float|string $bytes Number of bytes. Note max integer size for integers. + * @param int $decimals Optional. Precision of number of decimal places. Default 0. * @return string|false Number string on success, false on failure. + * + * @phpstan-param int|float|numeric-string $bytes */ function size_format( $bytes, $decimals = 0 ) { + if ( ! is_numeric( $bytes ) ) { + return false; + } + + $bytes = (float) $bytes; + $quant = array( /* translators: Unit symbol for yottabyte. */ _x( 'YB', 'unit symbol' ) => YB_IN_BYTES, @@ -486,13 +494,13 @@ function size_format( $bytes, $decimals = 0 ) { _x( 'B', 'unit symbol' ) => 1, ); - if ( 0 === $bytes ) { + if ( 0.0 === $bytes ) { /* translators: Unit symbol for byte. */ return number_format_i18n( 0, $decimals ) . ' ' . _x( 'B', 'unit symbol' ); } foreach ( $quant as $unit => $mag ) { - if ( (float) $bytes >= $mag ) { + if ( $bytes >= $mag ) { return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit; } } diff --git a/tests/phpstan/baselines/argument.type.neon b/tests/phpstan/baselines/argument.type.neon index fb48bd1be342a..66bfb6d6b9585 100644 --- a/tests/phpstan/baselines/argument.type.neon +++ b/tests/phpstan/baselines/argument.type.neon @@ -118,11 +118,6 @@ parameters: identifier: argument.type count: 1 path: ../../../src/wp-admin/includes/class-wp-site-health-auto-updates.php - - - message: '#^Parameter \#1 \$bytes of function size_format expects int\|string, float\|false given\.$#' - identifier: argument.type - count: 1 - path: ../../../src/wp-admin/includes/class-wp-site-health.php - message: '#^Parameter \#2 \$allowed_html of function wp_kses expects array\\|string, array\\|true\> given\.$#' identifier: argument.type diff --git a/tests/phpunit/tests/functions/sizeFormat.php b/tests/phpunit/tests/functions/sizeFormat.php index 77134188634fe..b4a038408b47b 100644 --- a/tests/phpunit/tests/functions/sizeFormat.php +++ b/tests/phpunit/tests/functions/sizeFormat.php @@ -22,12 +22,26 @@ public function data_size_format() { return array( // Invalid values. array( array(), 0, false ), + array( array( 'baba' ), 0, false ), + array( new stdClass(), 0, false ), array( 'baba', 0, false ), array( '', 0, false ), + array( '0x1A', 0, false ), + array( null, 0, false ), + array( true, 0, false ), + array( false, 0, false ), array( '-1', 0, false ), array( -1, 0, false ), - // Bytes. + array( -1.0, 0, false ), + // Zero bytes, in every numeric representation. array( 0, 0, '0 B' ), + array( 0.0, 0, '0 B' ), + array( 0.0, 2, '0.00 B' ), + array( '0', 0, '0 B' ), + array( '0.0', 0, '0 B' ), + array( 0.0e3, 0, '0 B' ), + array( -0.0, 0, '0 B' ), + // Bytes. array( 1, 0, '1 B' ), array( 1023, 0, '1,023 B' ), // Kilobytes.