Skip to content

Commit e0dd5d5

Browse files
committed
WP/EnqueuedResourceParameters: handle fully qualified \false and \null correctly
Before this change passing `\false` or `\null` as the `$ver` parameter would result in a false negative.
1 parent 668eed5 commit e0dd5d5

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

WordPress/Sniffs/WP/EnqueuedResourceParametersSniff.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ final class EnqueuedResourceParametersSniff extends AbstractFunctionParameterSni
5454
);
5555

5656
/**
57-
* False + the empty tokens array.
57+
* False + T_NS_SEPARATOR + the empty tokens array.
5858
*
5959
* This array is enriched with the $emptyTokens array in the register() method.
6060
*
6161
* @var array<int|string, int|string>
6262
*/
6363
private $false_tokens = array(
64-
\T_FALSE => \T_FALSE,
64+
\T_FALSE => \T_FALSE,
65+
\T_NS_SEPARATOR => \T_NS_SEPARATOR, // Needed to handle fully qualified \false (PHPCS 3.x).
6566
);
6667

6768
/**
@@ -141,7 +142,7 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
141142
}
142143
}
143144

144-
if ( false === $version_param || 'null' === strtolower( $version_param['clean'] ) ) {
145+
if ( false === $version_param || strtolower( ltrim( $version_param['clean'], '\\' ) ) === 'null' ) {
145146
$type = 'script';
146147
if ( strpos( $matched_content, '_style' ) !== false ) {
147148
$type = 'style';

WordPress/Tests/WP/EnqueuedResourceParametersUnitTest.1.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,13 @@ wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array
9898

9999
// Safeguard handling of non-lowercase `null`.
100100
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), NULL, true ); // Warning - 0, false or NULL are not allowed.
101+
102+
/*
103+
* Safeguard handling of fully qualified \true, \false and \null.
104+
* Also safeguard that adding T_NS_SEPARATOR to $false_tokens doesn't cause false positives due to problems in is_falsy().
105+
*/
106+
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), \FALSE, \true ); // Error - 0, false or NULL are not allowed.
107+
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), \null, \TRUE ); // Warning - 0, false or NULL are not allowed.
108+
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), \Null, true ); // Warning - 0, false or NULL are not allowed.
109+
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), \true, \False ); // Ok.
110+
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), \get_version(), \null ); // OK.

WordPress/Tests/WP/EnqueuedResourceParametersUnitTest.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,23 @@ public function getErrorList( $testFile = '' ) {
3131
switch ( $testFile ) {
3232
case 'EnqueuedResourceParametersUnitTest.1.inc':
3333
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,
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+
106 => 1,
5051
);
5152

5253
case 'EnqueuedResourceParametersUnitTest.2.inc':
@@ -79,6 +80,8 @@ public function getWarningList( $testFile = '' ) {
7980
66 => 2,
8081
77 => 1,
8182
100 => 1,
83+
107 => 1,
84+
108 => 1,
8285
);
8386

8487
default:

0 commit comments

Comments
 (0)