Skip to content
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

[PHP 8] Add a custom round method to workaround an incompatibility of the buil-in function #27830

Merged
merged 3 commits into from
Oct 9, 2020

Conversation

Konamiman
Copy link
Contributor

@Konamiman Konamiman commented Oct 1, 2020

All Submissions:

Changes proposed in this Pull Request:

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('') 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 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 right away, otherwise it applies floatval to the value before passing it to round. And all the calls to 'round' in the codebase are replaced with NumberUtil::round.

How to test the changes in this Pull Request:

Try to create a new order from the admin area.

  • In PHP 7, it works with and without this change.
  • In PHP 8, it will throw an error, which is fixed with this change.

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully run tests with your changes locally?

Changelog entry

Dev - Add a NumberUtil::round method to workaround a breaking change in the buil-in round function in PHP8.

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'.
@Konamiman Konamiman self-assigned this Oct 1, 2020
@Konamiman Konamiman mentioned this pull request Oct 1, 2020
7 tasks
/**
* A class of utilities for dealing with numbers.
*/
final class NumberUtil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just Number since it's under the Utilities namespace?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Contributor

@vedanshujain vedanshujain left a comment

Choose a reason for hiding this comment

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

Left a comment, other than that looks good!

src/Utilities/NumberUtil.php Outdated Show resolved Hide resolved
- 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
Copy link
Contributor

@vedanshujain vedanshujain left a comment

Choose a reason for hiding this comment

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

LGTM!

@Konamiman Konamiman merged commit c15488d into master Oct 9, 2020
@Konamiman Konamiman deleted the php8/fix-round-function-with-non-numeric-argument branch October 9, 2020 06:01
@woocommercebot woocommercebot added release: add changelog Mark all PRs that have not had their changelog entries added. [auto] release: add testing instructions PRs that have not had testing instructions added to the wiki. [auto] labels Oct 9, 2020
@Konamiman Konamiman added this to the 4.7.0 milestone Oct 9, 2020
@claudiosanches claudiosanches removed the release: add testing instructions PRs that have not had testing instructions added to the wiki. [auto] label Oct 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release: add changelog Mark all PRs that have not had their changelog entries added. [auto]
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants