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

Fix initial php 8.4 compatibility #2714

Merged
merged 12 commits into from
Dec 9, 2024
Merged

Conversation

mikkamp
Copy link
Contributor

@mikkamp mikkamp commented Dec 5, 2024

Changes proposed in this Pull Request:

This PR is the initial start for resolving the issues that are in #2647

There are still a few dependent packages we are waiting for provide full compatibility:

Resolved in this PR

  1. Fix all PHP code within the extension that is not compatible with PHP 8.4
  2. Update the guzzle packages and fix namespace replacements
$ composer update guzzlehttp/guzzle --with-dependencies
   
  - Upgrading guzzlehttp/guzzle (7.8.1 => 7.9.2)
  - Upgrading guzzlehttp/promises (2.0.2 => 2.0.4)
  - Upgrading guzzlehttp/psr7 (2.6.2 => 2.7.0)
  - Upgrading psr/http-factory (1.0.2 => 1.1.0)
  1. Update the phpunit packages for testing (not bundled in release)
$ composer update phpunit/phpunit --with-dependencies
   
  - Upgrading myclabs/deep-copy (1.11.1 => 1.12.1)
  - Upgrading nikic/php-parser (v4.17.1 => v5.3.1)
  - Upgrading phar-io/manifest (2.0.3 => 2.0.4)
  - Upgrading phpunit/php-code-coverage (9.2.29 => 9.2.32)
  - Upgrading phpunit/phpunit (9.6.13 => 9.6.21)
  - Upgrading sebastian/cli-parser (1.0.1 => 1.0.2)
  - Upgrading sebastian/complexity (2.0.2 => 2.0.3)
  - Upgrading sebastian/diff (4.0.5 => 4.0.6)
  - Upgrading sebastian/exporter (4.0.5 => 4.0.6)
  - Upgrading sebastian/global-state (5.0.6 => 5.0.7)
  - Upgrading sebastian/lines-of-code (1.0.3 => 1.0.4)
  - Upgrading sebastian/resource-operations (3.0.3 => 3.0.4)
  - Upgrading theseer/tokenizer (1.2.1 => 1.2.3)
  1. Update Jetpack packages to highest available v2 package
$ composer update automattic/jetpack-connection --with-dependencies

  - Upgrading automattic/jetpack-a8c-mc-stats (v2.0.0 => v2.0.4)
  - Upgrading automattic/jetpack-admin-ui (v0.3.2 => v0.4.6)
  - Locking automattic/jetpack-assets (v2.3.14)
  - Upgrading automattic/jetpack-connection (v2.3.2 => v2.12.6)
  - Upgrading automattic/jetpack-constants (v2.0.0 => v2.0.5)
  - Upgrading automattic/jetpack-redirect (v2.0.0 => v2.0.3)
  - Upgrading automattic/jetpack-roles (v2.0.0 => v2.0.4)
  - Upgrading automattic/jetpack-status (v2.1.0 => v3.3.5)
  1. Update validator package and revert pattern to remain strict for image URL validation
$ composer update symfony/validator --with-dependencies

  - Upgrading symfony/polyfill-ctype (v1.29.0 => v1.31.0)
  - Upgrading symfony/polyfill-php73 (v1.28.0 => v1.31.0)
  - Upgrading symfony/polyfill-php80 (v1.29.0 => v1.31.0)
  - Upgrading symfony/polyfill-php81 (v1.29.0 => v1.31.0)
  - Upgrading symfony/translation-contracts (v2.5.2 => v2.5.3)
  - Upgrading symfony/validator (v5.4.30 => v5.4.47)
  1. Update league/container package and provide support for V4 (we remain using psr/container 1.1)
$ composer update league/container --with-dependencies

  - Upgrading league/container (3.4.1 => 4.2.4)

Detailed test instructions:

  1. Checkout this branch
  2. Update packages rm -rf vendor && composer install
  3. Temporarily use test packages:
composer require "google/auth:dev-1.37.x-php84 as 1.37.2"
composer require "google/gax:dev-1.29.1-php74 as 1.29.2"
  1. Create a temporary script to patch remaining packages in bin/fix-packages.php
<?php

function replace_all( $path, $search, $replace ) {
	foreach ( glob( $path ) as $file ) {
		$content = preg_replace(
			'/' . preg_quote( $search, '/' ) . '/',
			$replace,
			file_get_contents( $file )
		);
		file_put_contents( $file, $content );
	}
}

replace_all(
	'vendor/googleads/google-ads-php/src/Google/Ads/GoogleAds/V16/Services/Client/*.php',
	'string $template = null',
	'?string $template = null'
);

replace_all(
	'vendor/automattic/jetpack-connection/src/class-manager.php',
	'Jetpack_XMLRPC_Server $xmlrpc_server = null',
	'?Jetpack_XMLRPC_Server $xmlrpc_server = null'
);

replace_all(
	'vendor/google/apiclient/src/Client.php',
	'ClientInterface $authHttp = null',
	'?ClientInterface $authHttp = null'
);

replace_all(
	'vendor/google/apiclient/src/Client.php',
	'ClientInterface $http = null',
	'?ClientInterface $http = null'
);

replace_all(
	'vendor/google/apiclient/src/Service/Exception.php',
	'Exception $previous = null',
	'?Exception $previous = null'
);

  1. Patch packages php bin/fix-packages.php
  2. Setup the unit tests bin/install-wp-tests.sh <db> <db_user> <db_pass>
  3. Replace the copy of WooCommerce from /tmp/woocommerce-9.4.3/plugins/woocommerce with the contents of the zip file provided below
    woocommerce-9.4.3-patched-for-php-8-4.zip
  4. Run unit tests with php 8.4 /usr/bin/php8.4 vendor/bin/phpunit
  5. Ensure all tests complete without errors
$ /usr/bin/php8.4 vendor/bin/phpunit

Installing...
Running as single site... To run multisite, use -c tests/phpunit/multisite.xml
Installing WooCommerce...
Not running ajax tests. To execute these, use --group ajax.
Not running ms-files tests. To execute these, use --group ms-files.
Not running external-http tests. To execute these, use --group external-http.
PHPUnit 9.6.21 by Sebastian Bergmann and contributors.

.............................................................   61 / 1327 (  4%)
.............................................................  122 / 1327 (  9%)
.............................................................  183 / 1327 ( 13%)
.............................................................  244 / 1327 ( 18%)
.............................................................  305 / 1327 ( 22%)
.............................................................  366 / 1327 ( 27%)
.............................................................  427 / 1327 ( 32%)
.............................................................  488 / 1327 ( 36%)
.............................................................  549 / 1327 ( 41%)
.............................................................  610 / 1327 ( 45%)
.............................................................  671 / 1327 ( 50%)
.............................................................  732 / 1327 ( 55%)
.............................................................  793 / 1327 ( 59%)
.............................................................  854 / 1327 ( 64%)
.............................................................  915 / 1327 ( 68%)
.............................................................  976 / 1327 ( 73%)
............................................................. 1037 / 1327 ( 78%)
............................................................. 1098 / 1327 ( 82%)
............................................................. 1159 / 1327 ( 87%)
............................................................. 1220 / 1327 ( 91%)
............................................................. 1281 / 1327 ( 96%)
..............................................                1327 / 1327 (100%)

Time: 00:32.341, Memory: 217.00 MB

OK (1327 tests, 4163 assertions)
  1. Repeat same unit tests with other versions of PHP like 7.4
  2. Run smoke tests with the extensions to confirm it doesn't have any issues with the container code

Additional details:

peeuvX-1Zh-p2

Changelog entry

  • Fix - Initial php 8.4 compatibility.

@mikkamp mikkamp self-assigned this Dec 5, 2024
@github-actions github-actions bot added type: bug The issue is a confirmed bug. changelog: fix Took care of something that wasn't working. labels Dec 5, 2024
Copy link

codecov bot commented Dec 5, 2024

Codecov Report

Attention: Patch coverage is 61.36364% with 17 lines in your changes missing coverage. Please review.

Project coverage is 66.2%. Comparing base (7cd33a8) to head (a0e163d).
Report is 41 commits behind head on develop.

Files with missing lines Patch % Lines
...DependencyManagement/ThirdPartyServiceProvider.php 0.0% 6 Missing ⚠️
...ernal/DependencyManagement/CoreServiceProvider.php 0.0% 5 Missing ⚠️
src/Container.php 0.0% 2 Missing ⚠️
.../Exception/RuntimeExceptionWithMessageFunction.php 0.0% 1 Missing ⚠️
...l/DependencyManagement/AbstractServiceProvider.php 75.0% 1 Missing ⚠️
...nternal/DependencyManagement/DBServiceProvider.php 0.0% 1 Missing ⚠️
...rnal/DependencyManagement/ProxyServiceProvider.php 0.0% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##             develop   #2714      +/-   ##
============================================
+ Coverage       63.4%   66.2%    +2.8%     
- Complexity         0    4691    +4691     
============================================
  Files            340     479     +139     
  Lines           5210   19608   +14398     
  Branches        1275       0    -1275     
============================================
+ Hits            3305   12983    +9678     
- Misses          1730    6625    +4895     
+ Partials         175       0     -175     
Flag Coverage Δ
js-unit-tests ?
php-unit-tests 66.2% <61.4%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/Assets/BaseAsset.php 74.0% <ø> (ø)
src/Assets/ScriptAsset.php 21.1% <ø> (ø)
src/Assets/ScriptWithBuiltDependenciesAsset.php 77.8% <ø> (ø)
src/Assets/StyleAsset.php 12.0% <ø> (ø)
src/DB/Table/BudgetRecommendationTable.php 48.2% <100.0%> (ø)
src/Exception/ExceptionWithResponseData.php 81.8% <100.0%> (ø)
src/Google/InvalidCouponEntry.php 66.7% <ø> (ø)
src/Infrastructure/View.php 100.0% <ø> (ø)
...nal/DependencyManagement/GoogleServiceProvider.php 95.0% <100.0%> (ø)
...ternal/DependencyManagement/JobServiceProvider.php 0.0% <ø> (ø)
... and 13 more

... and 796 files with indirect coverage changes

@mikkamp mikkamp requested a review from a team December 5, 2024 17:11
@mikkamp
Copy link
Contributor Author

mikkamp commented Dec 5, 2024

This would also supersede #2663

@puntope
Copy link
Contributor

puntope commented Dec 6, 2024

I get still one error in Unit Tests

Screenshot 2024-12-06 at 12 53 57

@puntope
Copy link
Contributor

puntope commented Dec 6, 2024

Create a temporary script to patch remaining packages in bin/fix-packages.php

Why not include this as the rest of the scripts like prefix-vendor-namespace.php?

@puntope
Copy link
Contributor

puntope commented Dec 6, 2024

replace_all(
'vendor/googleads/google-ads-php/src/Google/Ads/GoogleAds/V16/Services/Client/*.php',
'string $template = null',
'?string $template = null'
);

The first time you run this. It works. But if you run it another time It will result in:

'??string $template = null'

@mikkamp
Copy link
Contributor Author

mikkamp commented Dec 6, 2024

I get still one error in Unit Tests

Is that consistently reproducible? I'm not seeing that happen here on any PHP version. Can you confirm if you run just the individual test on a specific PHP version if you get it to fail:
/usr/bin/php8.4 vendor/bin/phpunit --filter=test_add_subsize_image

Based on the results it means that it won't create a 20x20 thumbnail image. I wonder if there is a PHP image module that is different between versions of PHP. Can you also run:
diff <(/usr/bin/php8.4 -m) <(/usr/bin/php8.3 -m)

Just to see if there is a difference in PHP modules.

Why not include this as the rest of the scripts like prefix-vendor-namespace.php?

This means we are patching upstream libraries manually, it is an option, but instead we've asked them for compatible versions. Once we get the updated versions we won't need a script like that anymore. Considering that they are deprecation notices (shouldn't in theory be shown on production sites), I thought the better way was to wait till we get updated packages.

The first time you run this. It works. But if you run it another time It will result in:
'??string $template = null'

Yes, I should have mentioned that, it's a quick search/replace script so only intended for running once.

@mikkamp mikkamp requested a review from puntope December 6, 2024 12:34
@puntope
Copy link
Contributor

puntope commented Dec 9, 2024

@mikkamp Re Image test

It does fail when running the test isolatedly.

Find also the output for diff <(/usr/bin/php8.4 -m) <(/usr/bin/php8.2 -m) (Notice the 8.2)

1a2
> bcmath
12a14
> gd
16c18,20
< imap
---
> igbinary
> imagick
> intl
18d21
< ldap
20a24,26
> memcache
> memcached
> msgpack
31a38
> redis
35a43
> soap
43a52
> xdebug
45a55
> xmlrpc
52a63
> Xdebug

maybe_add_subsize_image doesn't return any error. It just doesn't set the subsize in the metadata.

Update:

@mikkamp I was doing some debugging and found the error in wp_get_image_editor inside _wp_make_subsizes ( this is being called in wp_update_image_subsizes)

Test 'Automattic\WooCommerce\GoogleListingsAndAds\Tests\Unit\Utility\ImageUtilityTest::test_add_subsize_image' ended
object(WP_Error)#7361 (3) {
  ["errors"]=>
  array(1) {
    ["image_no_editor"]=>
    array(1) {
      [0]=>
      string(28) "No editor could be selected."
    }
  }
  ["error_data"]=>
  array(0) {
  }
  ["additional_data":protected]=>
  array(0) {
  }
}

Update 2:

Solved installing php8.4-imagick (as one of the editors)

@puntope
Copy link
Contributor

puntope commented Dec 9, 2024

This is my php-v output btw

PHP 8.4.1 (cli) (built: Nov 25 2024 18:03:47) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.4.1, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.1, Copyright (c), by Zend Technologies

Copy link
Contributor

@puntope puntope left a comment

Choose a reason for hiding this comment

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

Hi @mikkamp I solved the problem with the test. See ( #2714 (comment) )

So no more blockers left.

@mikkamp
Copy link
Contributor Author

mikkamp commented Dec 9, 2024

Thanks for clarifying. Either the gd or imagick image library is needed, I assume it failed because neither was available. I thought WordPress had a fallback for those cases, but generally most servers will have at least one available.

Going to merge this, now that it's running fine.

@mikkamp mikkamp merged commit 55cd0b1 into develop Dec 9, 2024
14 checks passed
@mikkamp mikkamp deleted the fix/initial-php-8.4-compat branch December 9, 2024 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: fix Took care of something that wasn't working. type: bug The issue is a confirmed bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants