You are here

protected function VariableAnalysisSniff::checkForPassByReferenceFunctionCall in Coder 8.2

Check if this is a "&" function call.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile:

int $stackPtr:

string $varName:

string $currScope:

Return value

bool

1 call to VariableAnalysisSniff::checkForPassByReferenceFunctionCall()
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 1824

Class

VariableAnalysisSniff
Checks the for undefined function variables.

Namespace

DrupalPractice\Sniffs\CodeAnalysis

Code

protected function checkForPassByReferenceFunctionCall(File $phpcsFile, $stackPtr, $varName, $currScope) {
  $tokens = $phpcsFile
    ->getTokens();
  $token = $tokens[$stackPtr];

  // Are we pass-by-reference to known pass-by-reference function?
  if (($functionPtr = $this
    ->findFunctionCall($phpcsFile, $stackPtr)) === false) {
    return false;
  }

  // Is our function a known pass-by-reference function?
  $functionName = $tokens[$functionPtr]['content'];
  if (isset($this->_passByRefFunctions[$functionName]) === false) {
    return false;
  }
  $refArgs = $this->_passByRefFunctions[$functionName];
  if (($argPtrs = $this
    ->findFunctionCallArguments($phpcsFile, $stackPtr)) === false) {
    return false;
  }

  // We're within a function call arguments list, find which arg we are.
  $argPos = false;
  foreach ($argPtrs as $idx => $ptrs) {
    if (in_array($stackPtr, $ptrs) === true) {
      $argPos = $idx + 1;
      break;
    }
  }
  if ($argPos === false) {
    return false;
  }
  if (in_array($argPos, $refArgs) === false) {

    // Our arg wasn't mentioned explicitly, are we after an elipsis catch-all?
    if (($elipsis = array_search('...', $refArgs)) === false) {
      return false;
    }
    if ($argPos < $refArgs[$elipsis - 1]) {
      return false;
    }
  }

  // Our argument position matches that of a pass-by-ref argument,
  // check that we're the only part of the argument expression.
  foreach ($argPtrs[$argPos - 1] as $ptr) {
    if ($ptr === $stackPtr) {
      continue;
    }
    if ($tokens[$ptr]['code'] !== T_WHITESPACE) {
      return false;
    }
  }

  // Just us, we can mark it as a write.
  $this
    ->markVariableAssignment($varName, $stackPtr, $currScope);

  // It's a read as well for purposes of used-variables.
  $this
    ->markVariableRead($varName, $stackPtr, $currScope);
  return true;
}