Skip to content

Commit c675689

Browse files
authored
Merge pull request #41 from woocommerce/fix/REDTWOO-72-php84-compat
REDTWOO-72: Ensure PHP 8.4 Compatibility
2 parents 7082669 + 34a4237 commit c675689

File tree

4 files changed

+121
-3
lines changed

4 files changed

+121
-3
lines changed

.github/workflows/php-coding-standards.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ on:
77
- develop
88
paths:
99
- "**.php"
10+
- composer.json # Required for phpcompat sniffs.
11+
- composer.lock # Required for phpcompat sniffs.
1012
- .github/workflows/php-coding-standards.yml
1113
pull_request:
1214
paths:
1315
- "**.php"
16+
- composer.json # Required for phpcompat sniffs.
17+
- composer.lock # Required for phpcompat sniffs.
1418
- .github/workflows/php-coding-standards.yml
1519

1620
concurrency:
@@ -36,3 +40,47 @@ jobs:
3640
- name: Run PHPCS on all files
3741
run: vendor/bin/phpcs ./* -q --report=checkstyle | cs2pr
3842

43+
phpcompat:
44+
name: PHP compatibility
45+
runs-on: ubuntu-latest
46+
steps:
47+
- name: Checkout repository
48+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
49+
50+
- name: Prepare PHP
51+
uses: woocommerce/grow/prepare-php@da5279aa4d76745ef32970e49b5ef7903a3b457a # v2.2.2
52+
with:
53+
tools: cs2pr
54+
install-deps: no
55+
56+
- name: Prepare node
57+
uses: woocommerce/grow/prepare-node@da5279aa4d76745ef32970e49b5ef7903a3b457a # v2.2.2
58+
with:
59+
node-version-file: ".nvmrc"
60+
ignore-scripts: "no"
61+
62+
# The use of WP-Scripts results in PHP files being generated during the npm build step.
63+
# In order to ensure PHP Compatibility, the build step must be run in order to ensure all
64+
# files in the production release of the plugin are compatible with the target PHP versions.
65+
- name: Build production bundle
66+
run: |
67+
echo "::group::Build log"
68+
npm run build
69+
echo "::endgroup::"
70+
71+
# The PHP Compatibility sniffs need to include the vendor directory to ensure all files
72+
# in the production release of the plugin are compatible with the target PHP versions.
73+
- name: Install composer production dependencies
74+
run: composer install --no-dev --prefer-dist --optimize-autoloader
75+
76+
# Installed globally to ensure dev dependencies are not included in compatibility sniffs.
77+
# At the time of writing no production version of PHPCompatibility/PHP-Compatibility supports
78+
# PHP 8.4 so a dev version is used in order to ensure the sniffs that have been created are
79+
# included in the checks.
80+
- name: Install PHP Compatibility dev-develop globally
81+
run: |
82+
composer global config --no-plugins allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
83+
composer global require --dev phpcompatibility/php-compatibility:dev-develop#8daeec54772a592ad369be23ae02ed593c71e7f1
84+
85+
- name: Run PHPCompatibility on all files
86+
run: ~/.composer/vendor/bin/phpcs . --standard=.php-compat.xml.dist -q --report=checkstyle | cs2pr

.php-compat.xml.dist

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHPCompatibilityWP" xsi:noNamespaceSchemaLocation="https://schema.phpcodesniffer.com/phpcs.xsd">
3+
4+
<description>WordPress specific ruleset which checks for PHP cross version compatibility.</description>
5+
<!-- Once PHPCompatibility/PHPCompatibilityWP supports the targeted versions of PHP, this config file can be updated accordingly. -->
6+
7+
<!-- Test PHP 7.4 thru 8.4 -->
8+
<config name="testVersion" value="7.4-8.4"/>
9+
<file>.</file>
10+
<arg name="extensions" value="php,inc" />
11+
12+
<rule ref="PHPCompatibility">
13+
<!--
14+
Contained in /wp-includes/compat.php.
15+
16+
History of the polyfills in WP:
17+
* hash_hmac(): since WP 3.2.0.
18+
* json_encode() and json_decode(): since unknown.
19+
* hash_equals(): since WP 3.9.2.
20+
* JSON_PRETTY_PRINT: since WP 4.1.0.
21+
* json_last_error_msg(): since WP 4.4.0.
22+
* JsonSerializable: since WP 4.4.0.
23+
* array_replace_recursive(): since WP 4.5.3 up to 5.2.x. The polyfill was removed in WP 5.3.
24+
* is_iterable(): since WP 4.9.6
25+
* is_countable(): since WP 4.9.6
26+
* IMAGETYPE_WEBP and IMG_WEBP: since WP 5.8.0.
27+
* array_key_first(): since WP 5.9.0
28+
* array_key_last(): since WP 5.9.0
29+
* str_contains(): since WP 5.9.0
30+
* str_starts_with(): since WP 5.9.0
31+
* str_ends_with(): since WP 5.9.0
32+
* array_is_list(): since WP 6.5.0
33+
-->
34+
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.hash_hmacFound"/>
35+
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.json_encodeFound"/>
36+
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.json_decodeFound"/>
37+
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.hash_equalsFound"/>
38+
<exclude name="PHPCompatibility.Constants.NewConstants.json_pretty_printFound"/>
39+
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.json_last_error_msgFound"/>
40+
<exclude name="PHPCompatibility.Interfaces.NewInterfaces.jsonserializableFound"/>
41+
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.array_replace_recursiveFound"/>
42+
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.is_iterableFound"/>
43+
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.is_countableFound"/>
44+
<exclude name="PHPCompatibility.Constants.NewConstants.imagetype_webpFound"/>
45+
<exclude name="PHPCompatibility.Constants.NewConstants.img_webpFound"/>
46+
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.array_key_firstFound"/>
47+
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.array_key_lastFound"/>
48+
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.str_containsFound"/>
49+
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.str_starts_withFound"/>
50+
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.str_ends_withFound"/>
51+
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.array_is_listFound"/>
52+
53+
<!--
54+
Contained in /wp-includes/spl-autoload-compat.php.
55+
56+
History of the polyfills in WP:
57+
* spl_autoload_register(), spl_autoload_unregister() and spl_autoload_functions() were
58+
introduced in WP 4.6.0 and available up to WP 5.2.x. The polyfills were removed in WP 5.3.
59+
-->
60+
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.spl_autoload_registerFound"/>
61+
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.spl_autoload_unregisterFound"/>
62+
<exclude name="PHPCompatibility.FunctionUse.NewFunctions.spl_autoload_functionsFound"/>
63+
</rule>
64+
65+
</ruleset>

tests/phpunit/Unit/Admin/Export/Service/ProductExportServiceTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,12 @@ public function test_handle_batch_first_batch_sets_file_path_and_schedules_next(
150150

151151
$this->assertFileExists( $file_path );
152152

153-
$csv = array_map( 'str_getcsv', file( $file_path ) );
153+
$csv = array_map(
154+
static function( $line ) {
155+
return str_getcsv( $line, ',', '"', '\\' );
156+
},
157+
file( $file_path )
158+
);
154159

155160
$this->assertEquals(
156161
array( 'id', 'title', 'description', 'link', 'image_link', 'price', 'sale_price', 'gtin', 'availability' ),

tests/phpunit/Unit/Admin/Export/Writer/CsvExportWriterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ public function test_append_row_writes_valid_csv_line() {
5959
$this->writer->append_row( $file, $data );
6060

6161
$handle = fopen( $file, 'r' );
62-
$header = fgetcsv( $handle );
63-
$row = fgetcsv( $handle );
62+
$header = fgetcsv( $handle, null, ',', '"', '\\' );
63+
$row = fgetcsv( $handle, null, ',', '"', '\\' );
6464
fclose( $handle );
6565

6666
$this->assertSame( array_keys( $data ), $header );

0 commit comments

Comments
 (0)