Skip to content

Commit 15c8cb7

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 f51cb5d commit 15c8cb7

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
@@ -156,3 +156,14 @@ MyNamespace\parse_url( 'http://example.com/' );
156156
\MyNamespace\json_encode( $data );
157157
namespace\unlink(); // The sniff should start flagging this once it can resolve relative namespaces.
158158
namespace\Sub\strip_tags( $string );
159+
160+
/*
161+
* Safeguard that the sniff does not incorrectly ignore class methods/constants with the same
162+
* name as WordPress global functions/constants when used in file_get_contents().
163+
*/
164+
file_get_contents( MyClass::wp_upload_dir() . 'subdir/file.inc' );
165+
file_get_contents( $this->GET_HOME_PATH() . 'subdir/file.inc' );
166+
file_get_contents( $this?->plugin_dir_path() . 'subdir/file.inc' );
167+
file_get_contents( MyClass::ABSPATH . 'subdir/file.inc' );
168+
file_get_contents( $this->WPMU_PLUGIN_DIR . 'subdir/file.inc' );
169+
file_get_contents( $this?->TEMPLATEPATH . '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+
164 => 1,
94+
165 => 1,
95+
166 => 1,
96+
167 => 1,
97+
168 => 1,
98+
169 => 1,
9399
);
94100
}
95101
}

0 commit comments

Comments
 (0)