Conversation
There's a number of places in the WooCommerce codebase where the
built-in function 'round' is executed passing a non-numeric value
(not a number and not a string that can be parsed as a number),
for example round(''). In PHP 7 this yields a value of 0, but in
PHP 8 this throws an error.
This commit adds a 'NumberUtil' class with a static 'round' method,
this method checks if the passed value is numeric and if so it just
executes the built-in function, otherwise it returns 0. And all the
calls to 'round' in the codebase are replaced with 'NumberUtil::round'.
| /** | ||
| * A class of utilities for dealing with numbers. | ||
| */ | ||
| final class NumberUtil { |
Contributor
There was a problem hiding this comment.
Why not just Number since it's under the Utilities namespace?
Contributor
Author
There was a problem hiding this comment.
I think we already discussed this at some point. It's better to have explicit class names for readability, and the name Number seems to suggest that the class represents a number, which isn't the case. Also StringUtil already exists, and in that particular case String couldn't be used because it's a PHP reserved word.
vedanshujain
suggested changes
Oct 8, 2020
Contributor
vedanshujain
left a comment
There was a problem hiding this comment.
Left a comment, other than that looks good!
- Passing a string that represents a number but has spaces (e.g. ' 1 ') now works as expected (the number is properly interpreted) - Passing the boolean true now returns 1, not 0 - Passing an object throws an error, instead of returning 0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
All Submissions:
Changes proposed in this Pull Request:
There's a number of places in the WooCommerce codebase where the built-in function
roundis executed passing a non-numeric value (not a number and not a string that can be parsed as a number), for exampleround('')when passing the value of a setting that has not been initialized. In PHP 7 this yields a value of 0, but in PHP 8 this throws an error.This commit adds a
NumberUtilclass with a staticroundmethod, this method checks if the passed value is numeric and if so it just executes the built-in function right away, otherwise it appliesfloatvalto the value before passing it toround. And all the calls to 'round' in the codebase are replaced withNumberUtil::round.How to test the changes in this Pull Request:
Try to create a new order from the admin area.
Other information:
Changelog entry