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

GlobalVariablesOverride: detect variable overrides in global namespace #1457

Merged
35 changes: 35 additions & 0 deletions WordPress/Sniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -2676,4 +2676,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 @@ -795,38 +795,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