Skip to content

PrefixAllGlobalsSniff: extend blocklist #2481

Open
@swissspidy

Description

@swissspidy

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:

/**
* Prefix blocklist.
*
* @since 0.12.0
* @since 3.0.0 Renamed from `$prefix_blacklist` to `$prefix_blocklist`.
*
* @var array<string, true> Key is prefix, value irrelevant.
*/
protected $prefix_blocklist = array(
'wordpress' => true,
'wp' => true,
'_' => true,
'php' => true, // See #1728, the 'php' prefix is reserved by PHP itself.
);

In our use case, it would be helpful if we could customize the blocklist.

The counter argument was:

Making it customizable would allow for people to clear out the list, resulting in the opposite effect.

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 or plugin.

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)

  • I intend to create a pull request to implement this feature.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions