You are here

function VariableAnalysisSniff::findVariableScope in Coder 8.2

Find the scope the given pointer is in.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile:

int $stackPtr:

Return value

int|false

3 calls to VariableAnalysisSniff::findVariableScope()
VariableAnalysisSniff::processCompact in coder_sniffer/DrupalPractice/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
Called to process variables named in a call to compact().
VariableAnalysisSniff::processVariable in coder_sniffer/DrupalPractice/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
Called to process normal member vars.
VariableAnalysisSniff::processVariableInString in coder_sniffer/DrupalPractice/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
Called to process variables found in double quoted strings.

File

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

Class

VariableAnalysisSniff
Checks the for undefined function variables.

Namespace

DrupalPractice\Sniffs\CodeAnalysis

Code

function findVariableScope(File $phpcsFile, $stackPtr) {
  $tokens = $phpcsFile
    ->getTokens();
  $token = $tokens[$stackPtr];
  $in_class = false;
  if (empty($token['conditions']) === false) {
    foreach (array_reverse($token['conditions'], true) as $scopePtr => $scopeCode) {
      if ($scopeCode === T_FUNCTION || $scopeCode === T_CLOSURE) {
        return $scopePtr;
      }
      if ($scopeCode === T_CLASS || $scopeCode === T_INTERFACE || $scopeCode === T_TRAIT) {
        $in_class = true;
      }
    }
  }
  if (($scopePtr = $this
    ->findFunctionPrototype($phpcsFile, $stackPtr)) !== false) {
    return $scopePtr;
  }
  if ($in_class === true) {

    // Member var of a class, we don't care.
    return false;
  }

  // File scope, hmm, lets use first token of file?
  return 0;
}