From df05758bfe6b27811920af21417f55eea29fd76b Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 4 Jan 2018 14:14:05 +0100 Subject: [PATCH] PrefixAllGlobals: make the prefix comparison more code style independent This allows for prefixed functions, classes, hooks in camelCase and other naming conventions. Fixes 1239 --- .../PrefixAllGlobalsSniff.php | 24 ++++++------------- .../PrefixAllGlobalsUnitTest.inc | 9 ++++++- .../PrefixAllGlobalsUnitTest.php | 1 - 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php b/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php index 6eee5660c2..7a2378bb1b 100644 --- a/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php +++ b/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php @@ -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[] @@ -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; - } } } diff --git a/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.inc b/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.inc index f409580b87..271f839706 100644 --- a/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.inc +++ b/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.inc @@ -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. /* @@ -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 diff --git a/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.php b/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.php index 00798e485d..3a2a47325f 100644 --- a/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.php +++ b/WordPress/Tests/NamingConventions/PrefixAllGlobalsUnitTest.php @@ -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,