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.
This commit is contained in:
jrfnl 2025-08-13 02:45:52 +02:00
parent afb6fb0e82
commit 7d376f8c69
No known key found for this signature in database
GPG key ID: 88BCD0973A23BCC6
3 changed files with 40 additions and 0 deletions

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

@ -83,3 +83,15 @@ 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.
// 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

@ -43,6 +43,10 @@ final class EnqueuedResourceParametersUnitTest extends AbstractSniffUnitTest {
61 => 1,
82 => 1,
85 => 1,
89 => 1,
92 => 1,
95 => 1,
97 => 1,
);
case 'EnqueuedResourceParametersUnitTest.2.inc':