Skip to content

Commit

Permalink
Merge pull request #1457 from WordPress-Coding-Standards/feature/1395…
Browse files Browse the repository at this point in the history
…-detect-variable-overrides-in-global-namespace

GlobalVariablesOverride: detect variable overrides in global namespace
  • Loading branch information
JDGrimes authored Sep 7, 2018
2 parents 206b30c + d6e4782 commit 784fefe
Show file tree
Hide file tree
Showing 7 changed files with 482 additions and 150 deletions.
35 changes: 35 additions & 0 deletions WordPress/Sniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -2710,4 +2710,39 @@ public function is_use_of_global_constant( $stackPtr ) {
return true;
}

/**
* Determine if a variable is in the `as $key => $value` part of a foreach condition.
*
* @since 1.0.0
* @since 1.1.0 Moved from the PrefixAllGlobals sniff to the Sniff base class.
*
* @param int $stackPtr Pointer to the variable.
*
* @return bool True if it is. False otherwise.
*/
protected function is_foreach_as( $stackPtr ) {
if ( ! isset( $this->tokens[ $stackPtr ]['nested_parenthesis'] ) ) {
return false;
}

$nested_parenthesis = $this->tokens[ $stackPtr ]['nested_parenthesis'];
$close_parenthesis = end( $nested_parenthesis );
$open_parenthesis = key( $nested_parenthesis );
if ( ! isset( $this->tokens[ $close_parenthesis ]['parenthesis_owner'] ) ) {
return false;
}

if ( \T_FOREACH !== $this->tokens[ $this->tokens[ $close_parenthesis ]['parenthesis_owner'] ]['code'] ) {
return false;
}

$as_ptr = $this->phpcsFile->findNext( \T_AS, ( $open_parenthesis + 1 ), $close_parenthesis );
if ( false === $as_ptr ) {
// Should never happen.
return false;
}

return ( $stackPtr > $as_ptr );
}

}
32 changes: 0 additions & 32 deletions WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -805,38 +805,6 @@ private function variable_prefixed_or_whitelisted( $stackPtr, $name ) {
return $this->is_prefixed( $stackPtr, $name );
}

/**
* Determine if a variable is in the `as $key => $value` part of a foreach condition.
*
* @param int $stackPtr Pointer to the variable.
*
* @return bool True if it is. False otherwise.
*/
private function is_foreach_as( $stackPtr ) {
if ( ! isset( $this->tokens[ $stackPtr ]['nested_parenthesis'] ) ) {
return false;
}

$nested_parenthesis = $this->tokens[ $stackPtr ]['nested_parenthesis'];
$close_parenthesis = end( $nested_parenthesis );
$open_parenthesis = key( $nested_parenthesis );
if ( ! isset( $this->tokens[ $close_parenthesis ]['parenthesis_owner'] ) ) {
return false;
}

if ( \T_FOREACH !== $this->tokens[ $this->tokens[ $close_parenthesis ]['parenthesis_owner'] ]['code'] ) {
return false;
}

$as_ptr = $this->phpcsFile->findNext( \T_AS, ( $open_parenthesis + 1 ), $close_parenthesis );
if ( false === $as_ptr ) {
// Should ever happen.
return false;
}

return ( $stackPtr > $as_ptr );
}

/**
* Validate an array of prefixes as passed through a custom property or via the command line.
*
Expand Down
Loading

0 comments on commit 784fefe

Please sign in to comment.