Skip to content

Commit

Permalink
PrefixAllGlobals: make the prefix comparison more code style independent
Browse files Browse the repository at this point in the history
This allows for prefixed functions, classes, hooks in camelCase and other naming conventions.

Fixes 1239
  • Loading branch information
jrfnl committed Jan 4, 2018
1 parent dd3edd6 commit df05758
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
24 changes: 7 additions & 17 deletions WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class PrefixAllGlobalsSniff extends AbstractFunctionParameterSniff {
/**
* Target prefixes after validation.
*
* All prefixes are lowercased for case-insensitive compare.
*
* @since 0.12.0
*
* @var string[]
Expand Down Expand Up @@ -667,31 +669,19 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
*
* @since 0.12.0
* @since 0.14.0 Allows for other non-word characters as well as underscores to better support hook names.
* @since 1.0.0 Does not require a word seperator anymore after a prefix.
* This allows for improved code style independent checking,
* i.e. allows for camelCase naming and the likes.
*
* @param string $name Name to check for a prefix.
*
* @return bool True when the name is the prefix or starts with the prefix + a separator.
* @return bool True when the name is the prefix or starts with the prefix.
* False otherwise.
*/
private function is_prefixed( $name ) {

foreach ( $this->validated_prefixes as $prefix ) {
if ( strtolower( $name ) === $prefix ) {
// Ok, prefix *is* the hook/constant name.
if ( stripos( $name, $prefix ) === 0 ) {
return true;

} else {
$prefix_found = stripos( $name, $prefix . '_' );

if ( 0 === $prefix_found ) {
// Ok, prefix found at start of hook/constant name.
return true;
}

if ( preg_match( '`^' . preg_quote( $prefix, '`' ) . '\W`i', $name ) === 1 ) {
// Ok, prefix with other non-word character found at start of hook/constant name.
return true;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ do_action( "tgmpa_plugin_action_{$acronym_filter_var}" );
* Bad: prefix not correctly used.
*/
function abtgmpa_do_something() {} // Bad.
function tgmpacd_do_something() {} // Bad.
function tgmpacd_do_something() {} // OK.


/*
Expand Down Expand Up @@ -327,4 +327,11 @@ add_shortcode( 'acronym_hello', function( $attrs, $content = null ) { // OK. Var
// Do something.
} );

// Issue #1239 - word separator check is not the concern of this sniff.
do_action( 'acronymAction' ); // OK.
apply_filters( 'acronymFilter', $var ); // OK.

function acronymDoSomething( $param = 'default' ) {} // OK.
class AcronymExample {} // OK.

// @codingStandardsChangeSetting WordPress.NamingConventions.PrefixAllGlobals prefixes false
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public function getErrorList( $testFile = 'PrefixAllGlobalsUnitTest.inc' ) {
39 => 1,
40 => 1,
90 => 1,
91 => 1,
// Backfills.
225 => ( function_exists( '\mb_strpos' ) ) ? 0 : 1,
230 => ( function_exists( '\array_column' ) ) ? 0 : 1,
Expand Down

0 comments on commit df05758

Please sign in to comment.