You are here

protected function VariableAnalysisSniff::checkForCatchBlock in Coder 8.2

Checks if we are in a catch() block.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile:

int $stackPtr:

string $varName:

string $currScope:

Return value

bool

1 call to VariableAnalysisSniff::checkForCatchBlock()
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 1335

Class

VariableAnalysisSniff
Checks the for undefined function variables.

Namespace

DrupalPractice\Sniffs\CodeAnalysis

Code

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

  // Are we a catch block parameter?
  if (($openPtr = $this
    ->findContainingBrackets($phpcsFile, $stackPtr)) === false) {
    return false;
  }

  // Function names are T_STRING, and return-by-reference is T_BITWISE_AND,
  // so we look backwards from the opening bracket for the first thing that
  // isn't a function name, reference sigil or whitespace and check if
  // it's a function keyword.
  $catchPtr = $phpcsFile
    ->findPrevious(T_WHITESPACE, $openPtr - 1, null, true, null, true);
  if ($catchPtr !== false && $tokens[$catchPtr]['code'] === T_CATCH) {

    // Scope of the exception var is actually the function, not just the catch block.
    // TODO: typeHint.
    $this
      ->markVariableDeclaration($varName, 'local', null, $stackPtr, $currScope, true);
    $this
      ->markVariableAssignment($varName, $stackPtr, $currScope);
    if ($this->allowUnusedCaughtExceptions !== false) {
      $varInfo = $this
        ->getVariableInfo($varName, $currScope);
      $varInfo->ignoreUnused = true;
    }
    return true;
  }
  return false;
}