Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions WordPress/Sniffs/WP/AlternativeFunctionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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 ) {
Expand Down
11 changes: 11 additions & 0 deletions WordPress/Tests/WP/AlternativeFunctionsUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,14 @@ 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().
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests cover the change, but might be good to vary the constants/functions being referenced in the tests ? And for the functions to include some case variation ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure about this. I understand your point, but at the same time, to me, it is easier to understand the tests if there is no variation in parts of the code that are irrelevant to what is being tested. That is why I prefer to use the same constants/functions and all lowercase letters. There are already other tests that cover those variations for this specific part of the code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but depending on the sniff, some functions may have special handling, while others do not. If all "namespaced name" tests only relate to the one function, it can easily be missed that special handling for another function is broken in the context of namespaced names (typically FQN global function).

I'm not trying to make your life more difficult, I'm trying to make it easier by having tests which will actually show what needs fixing for PHPCS 4.0.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is fair. I went ahead and added variation for the tests that 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(). I did not do the same for the tests added in the other commit, as I imagine they will be removed from this PR pending what we decide in the other comment thread in this PR. I do plan to add variation to those tests as well.

file_get_contents(MyClass::wp_upload_dir() . 'subdir/file.inc');
file_get_contents($this->wp_upload_dir() . 'subdir/file.inc');
file_get_contents($this?->wp_upload_dir() . 'subdir/file.inc');
file_get_contents(MyClass::ABSPATH . 'subdir/file.inc');
file_get_contents($this->ABSPATH . 'subdir/file.inc');
file_get_contents($this?->ABSPATH . 'subdir/file.inc');
6 changes: 6 additions & 0 deletions WordPress/Tests/WP/AlternativeFunctionsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ public function getWarningList() {
142 => 1,
146 => 1,
154 => 1,
179 => 1,
180 => 1,
181 => 1,
182 => 1,
183 => 1,
184 => 1,
);
}
}
Loading