Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add is_class_property() utility method and implement use of this and the other two new utility methods #1092

Merged
merged 1 commit into from
Aug 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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