Skip to content

Commit

Permalink
Merge pull request #1278 from WordPress-Coding-Standards/feature/1239…
Browse files Browse the repository at this point in the history
…-prefix-all-globals-code-style-independence

PrefixAllGlobals: make the prefix comparison more code style independent
  • Loading branch information
GaryJones authored Jan 4, 2018
2 parents 4290baf + 6ce4927 commit 18e6653
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
25 changes: 8 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 Expand Up @@ -760,6 +750,7 @@ private function validate_prefixes() {
array( $prefix )
);
unset( $this->prefixes[ $key ] );
continue;
}

// Lowercase the prefix to allow for direct compare.
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 @@ -33,7 +33,6 @@ public function getErrorList( $testFile = 'PrefixAllGlobalsUnitTest.inc' ) {
case 'PrefixAllGlobalsUnitTest.inc':
return array(
1 => 2, // 2 x error for incorrect prefix passed.
10 => 1,
18 => 1,
21 => 1,
22 => 1,
Expand All @@ -51,7 +50,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 18e6653

Please sign in to comment.