Skip to content

Commit

Permalink
Merge pull request #1092 from WordPress-Coding-Standards/feature/use-…
Browse files Browse the repository at this point in the history
…new-scope-utility-methods

Add `is_class_property()` utility method and implement use of this and the other two new utility methods
  • Loading branch information
GaryJones authored Aug 5, 2017
2 parents 5a3a696 + 1686dd2 commit 5e185e2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
36 changes: 35 additions & 1 deletion WordPress/Sniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down Expand Up @@ -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.
Expand Down
8 changes: 2 additions & 6 deletions WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 5e185e2

Please sign in to comment.