-
Notifications
You must be signed in to change notification settings - Fork 10.7k
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
Add an array_sum utility method to enhance PHP 8.3+ compatibility #47595
Conversation
Test using WordPress PlaygroundThe changes in this pull request can be previewed and tested using a WordPress Playground instance. Test this pull request with WordPress Playground. Note that this URL is valid for 30 days from when this comment was last updated. You can update it by closing/reopening the PR or pushing a new commit. |
Hi @Konamiman, Apart from reviewing the code changes, please make sure to review the testing instructions as well. You can follow this guide to find out what good testing instructions should look like: |
1 similar comment
Hi @Konamiman, Apart from reviewing the code changes, please make sure to review the testing instructions as well. You can follow this guide to find out what good testing instructions should look like: |
Changes proposed in this Pull Request:
In #40660 several people are reporting warnings such as the following when running on PHP 8.3:
This seems to happen when for some reason a non-numeric value makes it into an array that should be all integers and floats, possibly due to legacy data that hasn't been sanitized correctly or extensions that inject incorrect data types.
Rather than fixing each case of this separately, this PR introduces a new
array_sum
method to theNumberUtil
class that takes a similar approach to theround
method that is already in that class. It acts as a wrapper around the built-inarray_sum
, while adding a sanitization step to ensure that all the values in the array are numeric.Note that there are over 100 uses of
array_sum
in the WooCommerce codebase. Rather than updating every usage to the new method, which would cause this PR to have dozens of changed files and be difficult to test, as well as troubleshoot if something goes wrong, this simply fixes the usage cases that have already been reported to be problematic, as well as a few that seem especially likely to have the same issue.Fixes #40660
How to test the changes in this Pull Request:
Code review, and executing the test suite under PHP 8.3, is probably the best way to assess this change. Some manual steps that can be performed for added confidence:
WP_DEBUG_LOG
set to true.WP_DEBUG_LOG
. This is generally found at wp-content/debug.log. Ideally the file won't be there, because no PHP warning was logged. But if the file exists, check to make sure it does not have an entry like the one listed at the top of this PR.