Compare commits

...

5 commits

Author SHA1 Message Date
Denis Žoljom
f8685a2866
Merge pull request #2571 from jrfnl/feature/ghactions-show-startup-errors
Some checks are pending
Basic QA checks / Run code sniffs (push) Waiting to run
Basic QA checks / XML Code style (push) Waiting to run
Basic QA checks / Ruleset test: PHP latest on PHPCS dev (push) Waiting to run
Basic QA checks / Ruleset test: PHP latest on PHPCS lowest (push) Waiting to run
Basic QA checks / Ruleset test: PHP latest on PHPCS stable (push) Waiting to run
Basic QA checks / PHPStan (push) Waiting to run
Basic QA checks / Find typos (push) Waiting to run
Quick Tests / QTest - PHP 5.4 on PHPCS lowest (push) Waiting to run
Quick Tests / QTest - PHP latest on PHPCS lowest (push) Waiting to run
Quick Tests / QTest - PHP 5.4 on PHPCS stable (push) Waiting to run
Quick Tests / QTest - PHP latest on PHPCS stable (push) Waiting to run
GH Actions: update PHP ini configuration
2025-08-24 19:58:04 +02:00
Denis Žoljom
97ce8229c0
Merge pull request #2573 from WordPress/feature/php-8.5-prevent-error-for-deprecated-type-casts
PHP 8.5 | WP/EnqueuedResourceParameters: prevent deprecation notice for non-standard casts
2025-08-24 19:49:22 +02:00
jrfnl
009086db6e
GH Actions: update PHP ini configuration
Add `display_startup_errors=On` as per the current recommendation from PHPUnit.

Ref: b3b159cbe9
2025-08-18 17:18:27 +02:00
jrfnl
7d376f8c69
PHP 8.5 | WP/EnqueuedResourceParameters: prevent deprecation notice for non-standard casts
Four non-standard type casts are going to be deprecated in PHP 8.5.

When any of these type casts would be used in the parameter this sniff examines in the "code under scan" and the sniff would be run on PHP 8.5, the non-standard type cast would be executed via the `eval()`, leading to a deprecation notice, which would stop the scan of the file.

This commit works around this by always using the "standard" type cast syntax in the code which would be passed to `eval()`.

Includes tests.
2025-08-13 02:45:52 +02:00
jrfnl
afb6fb0e82
WP/EnqueuedResourceParameters: move parse error test to its own file 2025-08-13 02:29:21 +02:00
6 changed files with 94 additions and 32 deletions

View file

@ -38,7 +38,7 @@ jobs:
php-version: ${{ matrix.php }}
# With stable PHPCS dependencies, allow for PHP deprecation notices.
# Unit tests don't need to fail on those for stable releases where those issues won't get fixed anymore.
ini-values: error_reporting=-1, display_errors=On
ini-values: error_reporting=-1, display_errors=On, display_startup_errors=On
coverage: ${{ github.ref_name == 'develop' && 'xdebug' || 'none' }}
- name: Enable creation of `composer.lock` file

View file

@ -91,9 +91,9 @@ jobs:
id: set_ini
run: |
if [ "${{ matrix.dependencies }}" != "dev" ]; then
echo 'PHP_INI=error_reporting=E_ALL & ~E_DEPRECATED, display_errors=On' >> "$GITHUB_OUTPUT"
echo 'PHP_INI=error_reporting=E_ALL & ~E_DEPRECATED, display_errors=On, display_startup_errors=On' >> "$GITHUB_OUTPUT"
else
echo 'PHP_INI=error_reporting=-1, display_errors=On' >> "$GITHUB_OUTPUT"
echo 'PHP_INI=error_reporting=-1, display_errors=On, display_startup_errors=On' >> "$GITHUB_OUTPUT"
fi
- name: Set up PHP

View file

@ -227,6 +227,30 @@ final class EnqueuedResourceParametersSniff extends AbstractFunctionParameterSni
continue;
}
// Make sure that when deprecated casts are used in the code under scan and the sniff is run on PHP 8.5,
// the eval() won't cause a deprecation notice, borking the scan of the file.
if ( \PHP_VERSION_ID >= 80500 ) {
if ( \T_INT_CAST === $this->tokens[ $i ]['code'] ) {
$code_string .= '(int)';
continue;
}
if ( \T_DOUBLE_CAST === $this->tokens[ $i ]['code'] ) {
$code_string .= '(float)';
continue;
}
if ( \T_BOOL_CAST === $this->tokens[ $i ]['code'] ) {
$code_string .= '(bool)';
continue;
}
if ( \T_BINARY_CAST === $this->tokens[ $i ]['code'] ) {
$code_string .= '(string)';
continue;
}
}
$code_string .= $this->tokens[ $i ]['content'];
}

View file

@ -84,5 +84,14 @@ wp_register_script( 'someScript-js', $url, [], 0_0.0_0, true ); // Error - 0, fa
// Safeguard handling of PHP 8.1 explicit octals.
wp_register_script( 'someScript-js', $url, [], 0o0, true ); // Error - 0, false or NULL are not allowed.
// Live coding/parse error.
wp_register_style( src: 'https://example.com/someScript.js', ver: /*to do*/, handle: 'someScript-js', );
// Safeguard against PHP 8.5 deprecation of non-standard cast names.
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (boolean) 1, true ); // OK.
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (boolean) 0, true ); // Error - 0, false or NULL are not allowed.
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (integer) 1, true ); // OK.
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (integer) 0, true ); // Error - 0, false or NULL are not allowed.
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (double) 1, true ); // OK.
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (double) 0, true ); // Error - 0, false or NULL are not allowed.
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (binary) 0, true ); // Error - 0, false or NULL are not allowed.

View file

@ -0,0 +1,5 @@
<?php
// Live coding/parse error.
// This should be the only test in the file.
wp_register_style( src: 'https://example.com/someScript.js', ver: /*to do*/, handle: 'someScript-js', );

View file

@ -23,41 +23,65 @@ final class EnqueuedResourceParametersUnitTest extends AbstractSniffUnitTest {
/**
* Returns the lines where errors should occur.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int> Key is the line number, value is the number of expected errors.
*/
public function getErrorList() {
return array(
6 => 1,
9 => 1,
10 => 1,
12 => 1,
13 => 1,
14 => 1,
22 => 1,
54 => 1,
57 => 1,
61 => 1,
82 => 1,
85 => 1,
88 => 1,
);
public function getErrorList( $testFile = '' ) {
switch ( $testFile ) {
case 'EnqueuedResourceParametersUnitTest.1.inc':
return array(
6 => 1,
9 => 1,
10 => 1,
12 => 1,
13 => 1,
14 => 1,
22 => 1,
54 => 1,
57 => 1,
61 => 1,
82 => 1,
85 => 1,
89 => 1,
92 => 1,
95 => 1,
97 => 1,
);
case 'EnqueuedResourceParametersUnitTest.2.inc':
return array(
5 => 1,
);
default:
return array();
}
}
/**
* Returns the lines where warnings should occur.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int> Key is the line number, value is the number of expected warnings.
*/
public function getWarningList() {
return array(
3 => 2,
11 => 1,
32 => 1,
39 => 2,
42 => 1,
45 => 1,
66 => 2,
77 => 1,
);
public function getWarningList( $testFile = '' ) {
switch ( $testFile ) {
case 'EnqueuedResourceParametersUnitTest.1.inc':
return array(
3 => 2,
11 => 1,
32 => 1,
39 => 2,
42 => 1,
45 => 1,
66 => 2,
77 => 1,
);
default:
return array();
}
}
}