Skip to content

Commit 7d376f8

Browse files
committed
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.
1 parent afb6fb0 commit 7d376f8

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

WordPress/Sniffs/WP/EnqueuedResourceParametersSniff.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,30 @@ protected function is_falsy( $start, $end ) {
227227
continue;
228228
}
229229

230+
// Make sure that when deprecated casts are used in the code under scan and the sniff is run on PHP 8.5,
231+
// the eval() won't cause a deprecation notice, borking the scan of the file.
232+
if ( \PHP_VERSION_ID >= 80500 ) {
233+
if ( \T_INT_CAST === $this->tokens[ $i ]['code'] ) {
234+
$code_string .= '(int)';
235+
continue;
236+
}
237+
238+
if ( \T_DOUBLE_CAST === $this->tokens[ $i ]['code'] ) {
239+
$code_string .= '(float)';
240+
continue;
241+
}
242+
243+
if ( \T_BOOL_CAST === $this->tokens[ $i ]['code'] ) {
244+
$code_string .= '(bool)';
245+
continue;
246+
}
247+
248+
if ( \T_BINARY_CAST === $this->tokens[ $i ]['code'] ) {
249+
$code_string .= '(string)';
250+
continue;
251+
}
252+
}
253+
230254
$code_string .= $this->tokens[ $i ]['content'];
231255
}
232256

WordPress/Tests/WP/EnqueuedResourceParametersUnitTest.1.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,15 @@ wp_register_script( 'someScript-js', $url, [], 0_0.0_0, true ); // Error - 0, fa
8383

8484
// Safeguard handling of PHP 8.1 explicit octals.
8585
wp_register_script( 'someScript-js', $url, [], 0o0, true ); // Error - 0, false or NULL are not allowed.
86+
87+
// Safeguard against PHP 8.5 deprecation of non-standard cast names.
88+
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (boolean) 1, true ); // OK.
89+
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (boolean) 0, true ); // Error - 0, false or NULL are not allowed.
90+
91+
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (integer) 1, true ); // OK.
92+
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (integer) 0, true ); // Error - 0, false or NULL are not allowed.
93+
94+
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (double) 1, true ); // OK.
95+
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (double) 0, true ); // Error - 0, false or NULL are not allowed.
96+
97+
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (binary) 0, true ); // Error - 0, false or NULL are not allowed.

WordPress/Tests/WP/EnqueuedResourceParametersUnitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public function getErrorList( $testFile = '' ) {
4343
61 => 1,
4444
82 => 1,
4545
85 => 1,
46+
89 => 1,
47+
92 => 1,
48+
95 => 1,
49+
97 => 1,
4650
);
4751

4852
case 'EnqueuedResourceParametersUnitTest.2.inc':

0 commit comments

Comments
 (0)