You are here

protected function VariableAnalysisSniff::processCompactArguments in Coder 8.2

Check variables in a compact() call.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile:

int $stackPtr:

array $arguments:

string $currScope:

Return value

void

1 call to VariableAnalysisSniff::processCompactArguments()
VariableAnalysisSniff::processCompact in coder_sniffer/DrupalPractice/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
Called to process variables named in a call to compact().

File

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

Class

VariableAnalysisSniff
Checks the for undefined function variables.

Namespace

DrupalPractice\Sniffs\CodeAnalysis

Code

protected function processCompactArguments(File $phpcsFile, $stackPtr, $arguments, $currScope) {
  $tokens = $phpcsFile
    ->getTokens();
  foreach ($arguments as $argumentPtrs) {
    $argumentPtrs = array_values(array_filter($argumentPtrs, function ($argumentPtr) use ($tokens) {
      return $tokens[$argumentPtr]['code'] !== T_WHITESPACE;
    }));
    if (empty($argumentPtrs) === true) {
      continue;
    }
    if (isset($tokens[$argumentPtrs[0]]) === false) {
      continue;
    }
    $argument_first_token = $tokens[$argumentPtrs[0]];
    if ($argument_first_token['code'] === T_ARRAY) {

      // It's an array argument, recurse.
      if (($array_arguments = $this
        ->findFunctionCallArguments($phpcsFile, $argumentPtrs[0])) !== false) {
        $this
          ->processCompactArguments($phpcsFile, $stackPtr, $array_arguments, $currScope);
      }
      continue;
    }
    if (count($argumentPtrs) > 1) {

      // Complex argument, we can't handle it, ignore.
      continue;
    }
    if ($argument_first_token['code'] === T_CONSTANT_ENCAPSED_STRING) {

      // Single-quoted string literal, ie compact('whatever').
      // Substr is to strip the enclosing single-quotes.
      $varName = substr($argument_first_token['content'], 1, -1);
      $this
        ->markVariableReadAndWarnIfUndefined($phpcsFile, $varName, $argumentPtrs[0], $currScope);
      continue;
    }
    if ($argument_first_token['code'] === T_DOUBLE_QUOTED_STRING) {

      // Double-quoted string literal.
      if (preg_match($this->_double_quoted_variable_regexp, $argument_first_token['content']) === 1) {

        // Bail if the string needs variable expansion, that's runtime stuff.
        continue;
      }

      // Substr is to strip the enclosing double-quotes.
      $varName = substr($argument_first_token['content'], 1, -1);
      $this
        ->markVariableReadAndWarnIfUndefined($phpcsFile, $varName, $argumentPtrs[0], $currScope);
      continue;
    }
  }

  //end foreach
}