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.1] Fix code that throws deprecation notices in tests in PHP 8.1 #31333
[PHP 8.1] Fix code that throws deprecation notices in tests in PHP 8.1 #31333
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for the monstrous delay! Thanks, overall it's looking pretty good! Great work! I have a couple of questions/suggestions in the comments below.
@@ -368,7 +368,7 @@ protected function read_product_data( &$product ) { | |||
$set_props['category_ids'] = $this->get_term_ids( $product, 'product_cat' ); | |||
$set_props['tag_ids'] = $this->get_term_ids( $product, 'product_tag' ); | |||
$set_props['shipping_class_id'] = current( $this->get_term_ids( $product, 'product_shipping_class' ) ); | |||
$set_props['gallery_image_ids'] = array_filter( explode( ',', $set_props['gallery_image_ids'] ) ); | |||
$set_props['gallery_image_ids'] = array_filter( explode( ',', $set_props['gallery_image_ids'] ?? '' ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're fixing this, I think there are probably more places in code where the second argument may be null, e.g. maybe over here: https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/includes/class-wc-tax.php#L51
or here
https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/includes/wc-core-functions.php#L984
Do you think we should fix them proactively or ad-hoc?
I suppose the situation is similar with a bunch of other functions like str_replace
, class_exists
or urldecode
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the current point, with WordPress itself not being yet compatible with PHP 8.1, my bet is to keep a "reactive" approach and fix things as they appear. This also gives more time to the static analysis tools to become better.
@@ -119,6 +119,9 @@ protected function read_product_data( &$product ) { | |||
public function read_children( &$product, $force_read = false ) { | |||
$children_transient_name = 'wc_product_children_' . $product->get_id(); | |||
$children = get_transient( $children_transient_name ); | |||
if(false === $children) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if(false === $children) { | |
if ( false === $children ) { |
Side note: it might be worth rebasing this to make testing a bit easier, because only god knows how to build this now, I used |
The deprecation notices are about: - Various "passing null to parameter (...) is deprecated" - Usage of strftime - Using "false" as if it was an empty array - Mismatching return type of implemented interfaces, that's fixed by adding #[\ReturnTypeWillChange]
0bc7bd4
to
de5f2e3
Compare
any update on this @Konamiman. Would solve a lot php notices we got ;) |
📊 Test reports for this pull request have been published and are accessible through the following links:
Latest commit referenced in the reports: Visit the WooCommerce Test Reports homepage to view all published reports. See the FAQs page if you're having problems accessing them. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been a bit more involved than I hoped.
Testing this in PHP 8.1 now with WP 6.0, I got a bunch of errors. After cleaning up some WC Admin code and some code of ours and applying a bunch of fixes to WP core while testing (adding several #[\ReturnTypeWillChange]
statements to wp-includes/Requests/Cookie/Jar.php (related issue) and wp-includes/Requests/Utility/CaseInsensitiveDictionary.php), I'm getting just 2 errors and 1 failure:
There were 2 errors:
1) WC_Admin_Tests_API_Reports_Customers::test_user_creation
PHPUnit\Framework\Exception: PHP Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /private/var/folders/rh/s6k2nr3s6qnc7bt1zz0glxwr0000gn/T/wordpress/wp-includes/pluggable.php on line 2481
2) WC_Admin_Tests_API_Reports_Customers::test_customer_user_profile_name
PHPUnit\Framework\Exception: PHP Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /private/var/folders/rh/s6k2nr3s6qnc7bt1zz0glxwr0000gn/T/wordpress/wp-includes/pluggable.php on line 2481
--
There was 1 failure:
1) WC_Tests_Product_CSV_Importer::test_server_file
WP_Error Object (...) does not match expected type "string".
/Users/peter/Documents/GitHub/woocommerce/plugins/woocommerce/tests/legacy/unit-tests/importer/product.php:180
Just retested with PHP 8.0 and got just one failure:
So I think after this one failure gets fixed and the 2 PRs above are merged, this should be good to go. |
I'm not getting this error, neither in |
So I retested this on a clean repo clone again to make sure my attempt to rebase won't interfere. I see no deprecation notices from PHP 8 or 8.1 anymore, so we can ignore those 2 PRs I opened, but the 1 failure is still there: PHP 8.0
PHP 8.1
Will try to reinstall the testing suite to make sure my changes to WP aren't causing this, but I only added a couple of |
So the test that fails here will most likely fail also in PHP 7. It's not really related to the PHP 8 change, I think it was introduced as part of the changes in the downloadable files code (over here). Either way, maybe your setup returns different paths for this test. For me,
thus the check over here fails. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After a re-test, this works fine, so approving. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Hi @peterfabian, thanks for merging this pull request. Please take a look at these follow-up tasks you may need to perform:
|
All Submissions:
Changes proposed in this Pull Request:
Do the necessary code fixes so that the WooCommerce unit tests don't throw deprecation notices in PHP 8.1. The deprecation notices fixed are:
strftime
.false
as if it was an empty array.#[\ReturnTypeWillChange]
attributes.Note that this pull request alone is not enough to fix all the tests:
skip_on_php_8_1
method introduced inclass-wc-unit-test-case.php
), only in PHP 8.1, until that's no longer the case.Once Blocks and Action Scheduler have got the required fixes we could extend the GitHub action that run the tests to include PHP 8.1, keeping the WordPress-offending ones disabled for the time being.
Closes: #31270
Closes: #31386
NOTE: A lot of code formatting changes were needed for the code sniffer to be happy. These are in a separate commit for easier review.
How to test the changes in this Pull Request:
Other information:
Changelog entry
FOR PR REVIEWER ONLY: