You are here

protected function VariableAnalysisSniff::processVariable in Coder 8.2

Called to process normal member vars.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this: token was found.

int $stackPtr The position where the token was found.:

Return value

void

1 call to VariableAnalysisSniff::processVariable()
VariableAnalysisSniff::process in coder_sniffer/DrupalPractice/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
Processes this test, when one of its tokens is encountered.

File

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

Class

VariableAnalysisSniff
Checks the for undefined function variables.

Namespace

DrupalPractice\Sniffs\CodeAnalysis

Code

protected function processVariable(File $phpcsFile, $stackPtr) {
  $tokens = $phpcsFile
    ->getTokens();
  $token = $tokens[$stackPtr];
  $varName = $this
    ->normalizeVarName($token['content']);
  if (($currScope = $this
    ->findVariableScope($phpcsFile, $stackPtr)) === false) {
    return;
  }

  // Debug code.
  // static $dump_token = false;
  // if ($varName == 'property') {
  // $dump_token = true;
  // }
  // if ($dump_token) {
  // echo "Found variable {$varName} on line {$token['line']} in scope {$currScope}.\n" . print_r($token, true);
  // echo "Prev:\n" . print_r($tokens[$stackPtr - 1], true);
  // }
  // Determine if variable is being assigned or read.
  // Read methods that preempt assignment:
  // Are we a $object->$property type symbolic reference?
  // Possible assignment methods:
  // Is a mandatory function/closure parameter
  // Is an optional function/closure parameter with non-null value
  // Is closure use declaration of a variable defined within containing scope
  // catch (...) block start
  // $this within a class (but not within a closure).
  // $GLOBALS, $_REQUEST, etc superglobals.
  // $var part of class::$var static member
  // Assignment via =
  // Assignment via list (...) =
  // Declares as a global
  // Declares as a static
  // Assignment via foreach (... as ...) { }
  // Pass-by-reference to known pass-by-reference function
  // Are we a $object->$property type symbolic reference?
  if ($this
    ->checkForSymbolicObjectProperty($phpcsFile, $stackPtr, $varName, $currScope) === true) {
    return;
  }

  // Are we a function or closure parameter?
  if ($this
    ->checkForFunctionPrototype($phpcsFile, $stackPtr, $varName, $currScope) === true) {
    return;
  }

  // Are we a catch parameter?
  if ($this
    ->checkForCatchBlock($phpcsFile, $stackPtr, $varName, $currScope) === true) {
    return;
  }

  // Are we $this within a class?
  if ($this
    ->checkForThisWithinClass($phpcsFile, $stackPtr, $varName, $currScope) === true) {
    return;
  }

  // Are we a $GLOBALS, $_REQUEST, etc superglobal?
  if ($this
    ->checkForSuperGlobal($phpcsFile, $stackPtr, $varName, $currScope) === true) {
    return;
  }

  // $var part of class::$var static member
  if ($this
    ->checkForStaticMember($phpcsFile, $stackPtr, $varName, $currScope) === true) {
    return;
  }

  // Is the next non-whitespace an assignment?
  if ($this
    ->checkForAssignment($phpcsFile, $stackPtr, $varName, $currScope) === true) {
    return;
  }

  // OK, are we within a list (...) = construct?
  if ($this
    ->checkForListAssignment($phpcsFile, $stackPtr, $varName, $currScope) === true) {
    return;
  }

  // Are we a global declaration?
  if ($this
    ->checkForGlobalDeclaration($phpcsFile, $stackPtr, $varName, $currScope) === true) {
    return;
  }

  // Are we a static declaration?
  if ($this
    ->checkForStaticDeclaration($phpcsFile, $stackPtr, $varName, $currScope) === true) {
    return;
  }

  // Are we a foreach loopvar?
  if ($this
    ->checkForForeachLoopVar($phpcsFile, $stackPtr, $varName, $currScope) === true) {
    return;
  }

  // Are we pass-by-reference to known pass-by-reference function?
  if ($this
    ->checkForPassByReferenceFunctionCall($phpcsFile, $stackPtr, $varName, $currScope) === true) {
    return;
  }

  // OK, we don't appear to be a write to the var, assume we're a read.
  $this
    ->markVariableReadAndWarnIfUndefined($phpcsFile, $varName, $stackPtr, $currScope);
}