diff --git a/WordPress/Sniffs/WP/AlternativeFunctionsSniff.php b/WordPress/Sniffs/WP/AlternativeFunctionsSniff.php index 40fb0c61c..eb5f8a398 100644 --- a/WordPress/Sniffs/WP/AlternativeFunctionsSniff.php +++ b/WordPress/Sniffs/WP/AlternativeFunctionsSniff.php @@ -283,7 +283,7 @@ public function process_matched_token( $stackPtr, $group_name, $matched_content } $contains_wp_path_constant = preg_match( - '`\b(?:ABSPATH|WP_(?:CONTENT|PLUGIN)_DIR|WPMU_PLUGIN_DIR|TEMPLATEPATH|STYLESHEETPATH|(?:MU)?PLUGINDIR)\b`', + '`(?|->|::)\b(?:ABSPATH|WP_(?:CONTENT|PLUGIN)_DIR|WPMU_PLUGIN_DIR|TEMPLATEPATH|STYLESHEETPATH|(?:MU)?PLUGINDIR)\b`', $filename_param['clean'] ); if ( 1 === $contains_wp_path_constant ) { @@ -292,7 +292,7 @@ public function process_matched_token( $stackPtr, $group_name, $matched_content } $contains_wp_path_function_call = preg_match( - '`(?:get_home_path|plugin_dir_path|get_(?:stylesheet|template)_directory|wp_upload_dir)\s*\(`i', + '`(?|->|::)(?:get_home_path|plugin_dir_path|get_(?:stylesheet|template)_directory|wp_upload_dir)\s*\(`i', $filename_param['clean'] ); if ( 1 === $contains_wp_path_function_call ) { diff --git a/WordPress/Tests/WP/AlternativeFunctionsUnitTest.inc b/WordPress/Tests/WP/AlternativeFunctionsUnitTest.inc index a88e5638a..c0f7be222 100644 --- a/WordPress/Tests/WP/AlternativeFunctionsUnitTest.inc +++ b/WordPress/Tests/WP/AlternativeFunctionsUnitTest.inc @@ -147,3 +147,38 @@ file_get_contents( // Not using plugin_dir_path() for reasons. $url ); // Warning. + +/* + * Safeguard correct handling of all types of namespaced function calls + */ +\curl_init(); +MyNamespace\curl_init(); +\MyNamespace\curl_init(); +namespace\curl_init(); // The sniff should start flagging this once it can resolve relative namespaces. + +/* + * Safeguard correct handling of namespaced parameters passed to file_get_contents(). + * + * Note: the sniff should flag the examples using a fully qualified namespaced name and partially + * qualified name, but currently does not. This will be addressed via + * https://github.com/WordPress/WordPress-Coding-Standards/issues/2603. + */ +file_get_contents(\wp_upload_dir()['path'] . 'subdir/file.inc'); +file_get_contents(MyNamespace\wp_upload_dir()['path'] . 'subdir/file.inc'); +file_get_contents(\MyNamespace\wp_upload_dir()['path'] . 'subdir/file.inc'); +file_get_contents(namespace\wp_upload_dir()['path'] . 'subdir/file.inc'); +file_get_contents(\ABSPATH . 'wp-admin/css/some-file.css'); +file_get_contents(MyNamespace\ABSPATH . 'wp-admin/css/some-file.css'); +file_get_contents(\MyNamespace\ABSPATH . 'wp-admin/css/some-file.css'); +file_get_contents(namespace\ABSPATH . 'wp-admin/css/some-file.css'); + +/* + * Safeguard that the sniff does not incorrectly ignore class methods/constants with the same + * name as WordPress global functions/constants when used in file_get_contents(). + */ +file_get_contents(MyClass::wp_upload_dir() . 'subdir/file.inc'); +file_get_contents($this->GET_HOME_PATH() . 'subdir/file.inc'); +file_get_contents($this?->plugin_dir_path() . 'subdir/file.inc'); +file_get_contents(MyClass::ABSPATH . 'subdir/file.inc'); +file_get_contents($this->WPMU_PLUGIN_DIR . 'subdir/file.inc'); +file_get_contents($this?->TEMPLATEPATH . 'subdir/file.inc'); diff --git a/WordPress/Tests/WP/AlternativeFunctionsUnitTest.php b/WordPress/Tests/WP/AlternativeFunctionsUnitTest.php index b0f1bf96a..fd462ce32 100644 --- a/WordPress/Tests/WP/AlternativeFunctionsUnitTest.php +++ b/WordPress/Tests/WP/AlternativeFunctionsUnitTest.php @@ -89,6 +89,13 @@ public function getWarningList() { 131 => 1, 142 => 1, 146 => 1, + 154 => 1, + 179 => 1, + 180 => 1, + 181 => 1, + 182 => 1, + 183 => 1, + 184 => 1, ); } }