-
-
Notifications
You must be signed in to change notification settings - Fork 490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PrefixAllGlobalsSniff: extend blocklist #2481
Comments
@swissspidy In my opinion, there are two aspects to this "ask":
namespace Whatever\You\Use\Sniffs;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHPCSUtils\BackCompat\Helper;
use WordPressCS\WordPress\Helpers\RulesetPropertyHelper;
use WordPressCS\WordPress\Sniffs\NamingConventions\PrefixAllGlobalsSniff;
class PluginTeamPrefixAllGlobals implements Sniff {
private $wpcs_sniff;
public function __construct() {
$wpcs_sniff = new PrefixAllGlobals();
}
public function register() {
return $wpcs_sniff->register();
}
public function process( File $phpcsFile, $stackPtr ) {
$prefixes = [];
// Get the prefixes in the same way as WPCS does.
// Allow overruling the prefixes set in a ruleset via the command line.
$cl_prefixes = Helper::getConfigData( 'prefixes' );
if ( ! empty( $cl_prefixes ) ) {
$cl_prefixes = trim( $cl_prefixes );
if ( '' !== $cl_prefixes ) {
$prefixes = array_filter( array_map( 'trim', explode( ',', $cl_prefixes ) ) );
}
}
$prefixes = RulesetPropertyHelper::merge_custom_array( $prefixes, array(), false );
if ( empty( $prefixes ) ) {
// No prefixes passed, nothing to do.
return;
}
// Do your own pre-validation and pass the prefixes to WPCS.
$wpcs_sniff->prefixes = $this->prevalidate_prefixes($prefixes);
return $wpcs_sniff->process_token( $phpcsFile, $stackPtr );
}
private function prevalidate_prefixes( $prefixes ) {
// Do whatever else you want to prevalidate prefixes, like flag additional prefixes which shouldn't be allowed for plugins in the plugin directory.
return $prefixes;
}
} |
|
@swissspidy I'm going to answer in reverse order
Not necessarily. This issue and #2480 address different problems.
These are both aspects of the same larger problem - preventing name collisions -, but they are distinctly different aspects of the problem.
That's a large list, so I think we need to look at each individual prefix and discuss them individually. Keep in mind: the way the blocklist in WPCS works, is that something which is on the blocklist is not allowed to be the prefix, it doesn't block a prefix starting with something on the blocklist. I'll group the prefixes now based on my opinion. I've also not done a check against existing plugins. If a popular existing plugin would be blocked from using the sniff if a certain prefix would be added to the list, that would be a good reason not to accept the suggestion, so the "Acceptable suggestions" list will still need additional verification. Already on the list
Acceptable suggestions
Invalid
Would already be blocked by the 3 character minimum for prefixes anyway, so no point in adding this
Would be blocked by the upcoming 4 character minimum for prefixes anyway, so no point in adding this
Other unacceptable suggestions
|
While I did not open this issue with this intention, I do now understand the sniff's purpose better thanks to your elaboration.
Acknowledged. As per #2480 (comment) I think we'll create a new sniff to address this for plugin reviews.
Thanks a lot for going through these & grouping them! At first glance the "Acceptable suggestions" looks good for further analysis. The plugins team could probably provide further input regarding commonly seen names potentially causing collisions.
Makes sense to me. |
Let's leave this ticket open for a few weeks to allow for other people to pitch in and give their opinion and for the plugin team to provide further input. After that, I'd welcome a PR to address this ticket. |
Is your feature request related to a problem?
This is related WordPress/plugin-check#523. In the Plugin Check plugin we need to find a way to flag the lack of prefixes for global functions.
Related:
Describe the solution you'd like
PrefixAllGlobalsSniff
has a hardcoded blocklist of disallowed prefixes:WordPress-Coding-Standards/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php
Lines 78 to 91 in 7f76630
In our use case, it would be helpful if we could customize the blocklist.
The counter argument was:
So what we could do instead is only allow extending the list, not removing items from it.
This way, in our project we can easily prevent functions starting with some words known not be unique, like e.g.
admin
orplugin
.So something like
phpcs -ps . --standard=WordPress --sniffs=WordPress.NamingConventions.PrefixAllGobals --report=full,source,info --runtime-set prefix_blocklist admin,plugin
would flag the following functions:wordpress_myfunc()
wp_myfunc()
admin_myfunc()
plugin_myfunc()
Additional context (optional)
The text was updated successfully, but these errors were encountered: