You are here

protected function VariableAnalysisSniff::checkForGlobalDeclaration in Coder 8.2

Check if this variable is declared globally.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile:

int $stackPtr:

string $varName:

string $currScope:

Return value

bool

1 call to VariableAnalysisSniff::checkForGlobalDeclaration()
VariableAnalysisSniff::processVariable in coder_sniffer/DrupalPractice/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
Called to process normal member vars.

File

coder_sniffer/DrupalPractice/Sniffs/CodeAnalysis/VariableAnalysisSniff.php, line 1635

Class

VariableAnalysisSniff
Checks the for undefined function variables.

Namespace

DrupalPractice\Sniffs\CodeAnalysis

Code

protected function checkForGlobalDeclaration(File $phpcsFile, $stackPtr, $varName, $currScope) {
  $tokens = $phpcsFile
    ->getTokens();

  // Are we a global declaration?
  // Search backwards for first token that isn't whitespace, comma or variable.
  $globalPtr = $phpcsFile
    ->findPrevious(array(
    T_WHITESPACE,
    T_VARIABLE,
    T_COMMA,
  ), $stackPtr - 1, null, true, null, true);
  if ($globalPtr === false || $tokens[$globalPtr]['code'] !== T_GLOBAL) {
    return false;
  }

  // It's a global declaration.
  $this
    ->markVariableDeclaration($varName, 'global', null, $stackPtr, $currScope);

  // Also mark this variable as being a reference, so that we don't get
  // unused variable warnings if it is never read.
  $varInfo = $this
    ->getVariableInfo($varName, $currScope);
  $varInfo->passByReference = true;
  return true;
}