diff --git a/WordPress/Sniff.php b/WordPress/Sniff.php index a397ec60bd..f776a8f55d 100644 --- a/WordPress/Sniff.php +++ b/WordPress/Sniff.php @@ -1777,7 +1777,12 @@ protected function get_use_type( $stackPtr ) { } // USE keywords for traits. - if ( $this->phpcsFile->hasCondition( $stackPtr, array( T_CLASS, T_ANON_CLASS, T_TRAIT ) ) ) { + $valid_scopes = array( + 'T_CLASS' => true, + 'T_ANON_CLASS' => true, + 'T_TRAIT' => true, + ); + if ( true === $this->valid_direct_scope( $stackPtr, $valid_scopes ) ) { return 'trait'; } @@ -2244,6 +2249,35 @@ public function is_class_constant( $stackPtr ) { return $this->valid_direct_scope( $stackPtr, $valid_scopes ); } + /** + * Check whether a T_VARIABLE token is a class property declaration. + * + * @param int $stackPtr The position in the stack of the T_VARIABLE token to verify. + * + * @return bool + */ + public function is_class_property( $stackPtr ) { + if ( ! isset( $this->tokens[ $stackPtr ] ) || T_VARIABLE !== $this->tokens[ $stackPtr ]['code'] ) { + return false; + } + + // Note: interfaces can not declare properties. + $valid_scopes = array( + 'T_CLASS' => true, + 'T_ANON_CLASS' => true, + 'T_TRAIT' => true, + ); + + if ( $this->valid_direct_scope( $stackPtr, $valid_scopes ) ) { + // Make sure it's not a method parameter. + if ( empty( $this->tokens[ $stackPtr ]['nested_parenthesis'] ) ) { + return true; + } + } + + return false; + } + /** * Check whether the direct wrapping scope of a token is within a limited set of * acceptable tokens. diff --git a/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php b/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php index 74191df878..cce8305c6d 100644 --- a/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php +++ b/WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php @@ -270,7 +270,7 @@ public function process_token( $stackPtr ) { case 'T_CONST': // Constants in a class do not need to be prefixed. - if ( $this->phpcsFile->hasCondition( $stackPtr, array( T_CLASS, T_ANON_CLASS, T_INTERFACE, T_TRAIT ) ) === true ) { + if ( true === $this->is_class_constant( $stackPtr ) ) { return; } @@ -488,11 +488,7 @@ protected function process_variable_assignment( $stackPtr ) { } // Properties in a class do not need to be prefixed. - $conditions = array_keys( $this->tokens[ $stackPtr ]['conditions'] ); - $ptr = array_pop( $conditions ); - if ( isset( $this->tokens[ $ptr ] ) - && in_array( $this->tokens[ $ptr ]['code'], array( T_CLASS, T_ANON_CLASS, T_TRAIT ), true ) - ) { + if ( true === $this->is_class_property( $stackPtr ) ) { return; }