Skip to content

Commit a787feb

Browse files
committed
WP/AlternativeFunctions: improve regex to distinguish global WP constants/functions from non-WP class-based ones
This commit changes two regexes used to identify global WP constants and functions to prevent the sniff from incorrectly identifying class constants/methods with the same names as WordPress globals as being WordPress globals. This is done by adding a negative lookbehind to ensure the searched strings are not preceded by an object operator, null-safe object operator, or scope resolution operator. Fixes part of 2603
1 parent 064da4c commit a787feb

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

WordPress/Sniffs/WP/AlternativeFunctionsSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public function process_matched_token( $stackPtr, $group_name, $matched_content
283283
}
284284

285285
$contains_wp_path_constant = preg_match(
286-
'`\b(?:ABSPATH|WP_(?:CONTENT|PLUGIN)_DIR|WPMU_PLUGIN_DIR|TEMPLATEPATH|STYLESHEETPATH|(?:MU)?PLUGINDIR)\b`',
286+
'`(?<!\?->|->|::)\b(?:ABSPATH|WP_(?:CONTENT|PLUGIN)_DIR|WPMU_PLUGIN_DIR|TEMPLATEPATH|STYLESHEETPATH|(?:MU)?PLUGINDIR)\b`',
287287
$filename_param['clean']
288288
);
289289
if ( 1 === $contains_wp_path_constant ) {
@@ -292,7 +292,7 @@ public function process_matched_token( $stackPtr, $group_name, $matched_content
292292
}
293293

294294
$contains_wp_path_function_call = preg_match(
295-
'`(?:get_home_path|plugin_dir_path|get_(?:stylesheet|template)_directory|wp_upload_dir)\s*\(`i',
295+
'`(?<!\?->|->|::)(?:get_home_path|plugin_dir_path|get_(?:stylesheet|template)_directory|wp_upload_dir)\s*\(`i',
296296
$filename_param['clean']
297297
);
298298
if ( 1 === $contains_wp_path_function_call ) {

WordPress/Tests/WP/AlternativeFunctionsUnitTest.inc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,14 @@ file_get_contents(\ABSPATH . 'wp-admin/css/some-file.css');
171171
file_get_contents(MyNamespace\ABSPATH . 'wp-admin/css/some-file.css');
172172
file_get_contents(\MyNamespace\ABSPATH . 'wp-admin/css/some-file.css');
173173
file_get_contents(namespace\ABSPATH . 'wp-admin/css/some-file.css');
174+
175+
/*
176+
* Safeguard that the sniff does not incorrectly ignore class methods/constants with the same
177+
* name as WordPress global functions/constants when used in file_get_contents().
178+
*/
179+
file_get_contents(MyClass::wp_upload_dir() . 'subdir/file.inc');
180+
file_get_contents($this->wp_upload_dir() . 'subdir/file.inc');
181+
file_get_contents($this?->wp_upload_dir() . 'subdir/file.inc');
182+
file_get_contents(MyClass::ABSPATH . 'subdir/file.inc');
183+
file_get_contents($this->ABSPATH . 'subdir/file.inc');
184+
file_get_contents($this?->ABSPATH . 'subdir/file.inc');

WordPress/Tests/WP/AlternativeFunctionsUnitTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ public function getWarningList() {
9090
142 => 1,
9191
146 => 1,
9292
154 => 1,
93+
179 => 1,
94+
180 => 1,
95+
181 => 1,
96+
182 => 1,
97+
183 => 1,
98+
184 => 1,
9399
);
94100
}
95101
}

0 commit comments

Comments
 (0)