Skip to content

Commit

Permalink
Add is_class_property() utility method and implement use of this an…
Browse files Browse the repository at this point in the history
…d the other two new utility methods.

PR 1089 introduced two new utility methods to the `WordPress\Sniff` class: `is_class_constant()` and `valid_direct_scope()`.

This PR introduces one additional utility method `is_class_property()` and implements the use of this and the other two methods in other places in the code where appropriate (other than where they were originally introduced for).
  • Loading branch information
jrfnl committed Aug 5, 2017
1 parent 5a3a696 commit 1686dd2
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 1686dd2

Please sign in to comment.