Skip to content

Commit 97ce822

Browse files
authored
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
2 parents 298da73 + 7d376f8 commit 97ce822

File tree

4 files changed

+91
-29
lines changed

4 files changed

+91
-29
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.inc renamed to WordPress/Tests/WP/EnqueuedResourceParametersUnitTest.1.inc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,14 @@ wp_register_script( 'someScript-js', $url, [], 0_0.0_0, true ); // Error - 0, fa
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.
8686

87-
// Live coding/parse error.
88-
wp_register_style( src: 'https://example.com/someScript.js', ver: /*to do*/, handle: 'someScript-js', );
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.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
// Live coding/parse error.
4+
// This should be the only test in the file.
5+
wp_register_style( src: 'https://example.com/someScript.js', ver: /*to do*/, handle: 'someScript-js', );

WordPress/Tests/WP/EnqueuedResourceParametersUnitTest.php

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,65 @@ final class EnqueuedResourceParametersUnitTest extends AbstractSniffUnitTest {
2323
/**
2424
* Returns the lines where errors should occur.
2525
*
26+
* @param string $testFile The name of the file being tested.
27+
*
2628
* @return array<int, int> Key is the line number, value is the number of expected errors.
2729
*/
28-
public function getErrorList() {
29-
return array(
30-
6 => 1,
31-
9 => 1,
32-
10 => 1,
33-
12 => 1,
34-
13 => 1,
35-
14 => 1,
36-
22 => 1,
37-
54 => 1,
38-
57 => 1,
39-
61 => 1,
40-
82 => 1,
41-
85 => 1,
42-
88 => 1,
43-
);
30+
public function getErrorList( $testFile = '' ) {
31+
switch ( $testFile ) {
32+
case 'EnqueuedResourceParametersUnitTest.1.inc':
33+
return array(
34+
6 => 1,
35+
9 => 1,
36+
10 => 1,
37+
12 => 1,
38+
13 => 1,
39+
14 => 1,
40+
22 => 1,
41+
54 => 1,
42+
57 => 1,
43+
61 => 1,
44+
82 => 1,
45+
85 => 1,
46+
89 => 1,
47+
92 => 1,
48+
95 => 1,
49+
97 => 1,
50+
);
51+
52+
case 'EnqueuedResourceParametersUnitTest.2.inc':
53+
return array(
54+
5 => 1,
55+
);
56+
57+
default:
58+
return array();
59+
}
4460
}
4561

4662
/**
4763
* Returns the lines where warnings should occur.
4864
*
65+
* @param string $testFile The name of the file being tested.
66+
*
4967
* @return array<int, int> Key is the line number, value is the number of expected warnings.
5068
*/
51-
public function getWarningList() {
52-
return array(
53-
3 => 2,
54-
11 => 1,
55-
32 => 1,
56-
39 => 2,
57-
42 => 1,
58-
45 => 1,
59-
66 => 2,
60-
77 => 1,
61-
);
69+
public function getWarningList( $testFile = '' ) {
70+
switch ( $testFile ) {
71+
case 'EnqueuedResourceParametersUnitTest.1.inc':
72+
return array(
73+
3 => 2,
74+
11 => 1,
75+
32 => 1,
76+
39 => 2,
77+
42 => 1,
78+
45 => 1,
79+
66 => 2,
80+
77 => 1,
81+
);
82+
83+
default:
84+
return array();
85+
}
6286
}
6387
}

0 commit comments

Comments
 (0)