Fixed : Inconsistent behaviour when zero bytes are passed in size_format() - #7248
Fixed : Inconsistent behaviour when zero bytes are passed in size_format()#7248narenin wants to merge 3 commits into
zero bytes are passed in size_format()#7248Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
| ); | ||
|
|
||
| if ( 0 === $bytes ) { | ||
| if ( 0 === $bytes || '0' === $bytes ) { |
There was a problem hiding this comment.
I think a better approach would be to convert the $bytes variable to float and keep the previous logic.
This would also prevent repetitive conversion to float in the foreach loop on line 491.
Would you please extend the test cases in Tests_Functions_SizeFormat::data_size_format() with this edge case? It would also be nice to cover other string values, e.g. '5', '374647' etc. These should be handled correctly as well.
There was a problem hiding this comment.
@martinkrcho Thanks for the feedback if I will added $bytes = (float) $bytes; before line number 485 then the test case will failed in case of array( 0, 0, '0 B' ), because of strict typecasting.
It is working fine in case of e.g. '5', '374647' etc.
Please let me know if I am missing anything.
There was a problem hiding this comment.
You're right, this would break the existing handling for zero value.
Let's consider couple of edge cases to make sure no one else need to get back to this. What would you do for values such as: "0", "0.0", 0.0e3 and other strange representations of zero?
There was a problem hiding this comment.
@martinkrcho Thanks for looking into this, I have made changes to cover up these cases, could you please give your valuable suggestions on this.
There was a problem hiding this comment.
I like your approach with the is_numeric check. I think this solution is much more robust. Thank you for sticking with this.
There was a problem hiding this comment.
Wouldn't it make sense to check for numeric input right at the start and return false for non-numeric values immediately? Currently, a non-numeric value makes it through the foreach loop and gets cast to 0 on each iteration, which will never meet the condition and ultimately results in returning false anyway -- except for non-numeric values that start with a numeric character.
Something like this might work:
function size_format( $bytes, $decimals = 0 ) {
if (! is_numeric($bytes) && ! (is_string($bytes) && is_numeric($bytes[0]))) {
return false;
}
$quant = array( /* array items */ );
$bytes = (float) $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 ( $bytes >= $mag ) {
return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;
}
}
return false;
}There was a problem hiding this comment.
In https://core.trac.wordpress.org/ticket/36635 it return 0B instead of false so your suggestion re-open that issue.
There was a problem hiding this comment.
@mukeshpanchal27 All tests pass using the code from my previous comment, except for the empty string, which would have to be handled separately, e.g. by adding it to the if condition or by starting with if ('' === $bytes) { return false; }.
There was a problem hiding this comment.
However, I would argue that allowing '1 KB' to return '1 B' isn't the desired behavior, so a simple if (! is_numeric($bytes)) { return false; } would be sufficient.
There was a problem hiding this comment.
Refactoring @IanDelMar solution What if we only check if it's numeric and bail early if it isn't, and then cast it to float? That is sufficient to cover also '0' string and avoid duplication of casting. @mukeshpanchal27
function size_format( $bytes, $decimals = 0 ) {
if ( ! is_numeric($bytes) ) {
return false;
}
$quant = array( /* array items */ );
$bytes = (float) $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 ( $bytes >= $mag ) {
return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;
}
}
return false;
}| $bytes = is_numeric( $bytes ) ? (float) $bytes : $bytes; | ||
|
|
||
| if ( 0.0 === $bytes ) { |
There was a problem hiding this comment.
Why float?
| $bytes = is_numeric( $bytes ) ? (float) $bytes : $bytes; | |
| if ( 0.0 === $bytes ) { | |
| $bytes = (int) $bytes; | |
| if ( 0 === $bytes ) { |
There was a problem hiding this comment.
Let's also make it clear what string is expected.
| * @return string|false Number string on success, false on failure. | |
| * @phpstan-param int|numeric-string $bytes |
Trac ticket: https://core.trac.wordpress.org/ticket/61930
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.