Skip to content

Commit 8fe987c

Browse files
authored
Merge pull request #2630 from rodrigoprimo/enqueued-resource-parameters-case-insensitive-null
WP/EnqueuedResourceParameters: fix handling of non-lowercased `null` and fully qualified `\null` and `\false`
2 parents bb822fe + e0dd5d5 commit 8fe987c

File tree

5 files changed

+56
-36
lines changed

5 files changed

+56
-36
lines changed

.github/CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ When you introduce new `public` sniff properties, or your sniff extends a class
6262

6363
### Pre-requisites
6464
* WordPress-Coding-Standards
65-
* PHP_CodeSniffer 3.13.0 or higher
65+
* PHP_CodeSniffer 3.13.4 or higher
6666
* PHPCSUtils 1.1.0 or higher
6767
* PHPCSExtra 1.4.0 or higher
6868
* PHPUnit 8.x - 9.x

WordPress/Sniffs/WP/EnqueuedResourceParametersSniff.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
use WordPressCS\WordPress\AbstractFunctionParameterSniff;
1616

1717
/**
18-
* This checks the enqueued 4th and 5th parameters to make sure the version and in_footer are set.
18+
* This checks that the 4th ($ver) parameter is set for all enqueued resources and that the 5th ($in_footer) parameter
19+
* is set for wp_register_script() and wp_enqueue_script().
1920
*
2021
* If a source ($src) value is passed, then version ($ver) needs to have non-falsy value.
21-
* If a source ($src) value is passed a check for in footer ($in_footer), warn the user if the value is falsy.
22+
* If a source ($src) value is passed, then it is recommended to explicitly set the $in_footer parameter.
2223
*
2324
* @link https://developer.wordpress.org/reference/functions/wp_register_script/
2425
* @link https://developer.wordpress.org/reference/functions/wp_enqueue_script/
@@ -53,20 +54,21 @@ final class EnqueuedResourceParametersSniff extends AbstractFunctionParameterSni
5354
);
5455

5556
/**
56-
* False + the empty tokens array.
57+
* False + T_NS_SEPARATOR + the empty tokens array.
5758
*
5859
* This array is enriched with the $emptyTokens array in the register() method.
5960
*
6061
* @var array<int|string, int|string>
6162
*/
6263
private $false_tokens = array(
63-
\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).
6466
);
6567

6668
/**
6769
* Token codes which are "safe" to accept to determine whether a version would evaluate to `false`.
6870
*
69-
* This array is enriched with the several of the PHPCS token arrays in the register() method.
71+
* This array is enriched with several of the PHPCS token arrays in the register() method.
7072
*
7173
* @var array<int|string, int|string>
7274
*/
@@ -88,7 +90,8 @@ final class EnqueuedResourceParametersSniff extends AbstractFunctionParameterSni
8890
/**
8991
* Returns an array of tokens this test wants to listen for.
9092
*
91-
* Overloads and calls the parent method to allow for adding additional tokens to the $safe_tokens property.
93+
* Overloads and calls the parent method to allow for adding additional tokens to the
94+
* $false_tokens and $safe_tokens properties.
9295
*
9396
* @return array
9497
*/
@@ -139,7 +142,7 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
139142
}
140143
}
141144

142-
if ( false === $version_param || 'null' === $version_param['clean'] ) {
145+
if ( false === $version_param || strtolower( ltrim( $version_param['clean'], '\\' ) ) === 'null' ) {
143146
$type = 'script';
144147
if ( strpos( $matched_content, '_style' ) !== false ) {
145148
$type = 'style';
@@ -165,8 +168,8 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
165168
/*
166169
* In footer Check
167170
*
168-
* Check to make sure that $in_footer is set to true.
169-
* It will warn the user to make sure it is intended.
171+
* Check to make sure that $in_footer is explicitly set.
172+
* Warn the user if it is not set.
170173
*
171174
* Only wp_register_script and wp_enqueue_script need this check,
172175
* as this parameter is not available to wp_register_style and wp_enqueue_style.

WordPress/Tests/WP/EnqueuedResourceParametersUnitTest.1.inc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array
2929
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), function() { }, true ); // OK.
3030
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), $version, true ); // OK.
3131

32-
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), '1.1.0' ); // Warning - In Footer is set to a falsy (default) value.
32+
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), '1.1.0' ); // Warning - $in_footer is not explicitly set.
3333
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), '1.1.0', false ); // OK.
3434
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), '1.1.0', null ); // OK.
3535
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), '1.1.0', 0 ); // OK.
@@ -95,3 +95,16 @@ wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array
9595
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (double) 0, true ); // Error - 0, false or NULL are not allowed.
9696

9797
wp_register_script( 'someScript-js', 'https://example.com/someScript.js' , array( 'jquery' ), (binary) 0, true ); // Error - 0, false or NULL are not allowed.
98+
99+
// Safeguard handling of non-lowercase `null`.
100+
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: 28 additions & 24 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':
@@ -70,14 +71,17 @@ public function getWarningList( $testFile = '' ) {
7071
switch ( $testFile ) {
7172
case 'EnqueuedResourceParametersUnitTest.1.inc':
7273
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,
74+
3 => 2,
75+
11 => 1,
76+
32 => 1,
77+
39 => 2,
78+
42 => 1,
79+
45 => 1,
80+
66 => 2,
81+
77 => 1,
82+
100 => 1,
83+
107 => 1,
84+
108 => 1,
8185
);
8286

8387
default:

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"ext-libxml": "*",
2222
"ext-tokenizer": "*",
2323
"ext-xmlreader": "*",
24-
"squizlabs/php_codesniffer": "^3.13.0",
24+
"squizlabs/php_codesniffer": "^3.13.4",
2525
"phpcsstandards/phpcsutils": "^1.1.0",
2626
"phpcsstandards/phpcsextra": "^1.4.0"
2727
},

0 commit comments

Comments
 (0)