Skip to content

Commit a3cff7f

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 28d445e commit a3cff7f

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
@@ -53,14 +53,15 @@ final class EnqueuedResourceParametersSniff extends AbstractFunctionParameterSni
5353
);
5454

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

6667
/**
@@ -140,7 +141,7 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
140141
}
141142
}
142143

143-
if ( false === $version_param || 'null' === strtolower( $version_param['clean'] ) ) {
144+
if ( false === $version_param || strtolower( ltrim( $version_param['clean'], '\\' ) ) === 'null' ) {
144145
$type = 'script';
145146
if ( strpos( $matched_content, '_style' ) !== false ) {
146147
$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 ); // Error - 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 ); // Error - 0, false or NULL are not allowed.
108+
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), \Null, true ); // Error - 0, false or NULL are not allowed.
109+
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), \true, true ); // Ok.
110+
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), \get_version(), true ); // 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)