You are here

public function Drupal_Sniffs_Semantics_FunctionCallSniff::getArgument in Coder 7.2

Returns start and end token for a given argument number.

Parameters

int $number Indicates which argument should be examined, starting with: 1 for the first argument.

Return value

array(string => int)

File

coder_sniffer/Drupal/Sniffs/Semantics/FunctionCallSniff.php, line 173

Class

Drupal_Sniffs_Semantics_FunctionCallSniff
Helper class to sniff for specific function calls.

Code

public function getArgument($number) {

  // Check if we already calculated the tokens for this argument.
  if (isset($this->arguments[$number]) === true) {
    return $this->arguments[$number];
  }
  $tokens = $this->phpcsFile
    ->getTokens();

  // Start token of the first argument.
  $start = $this->phpcsFile
    ->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $this->openBracket + 1, null, true);
  if ($start === $this->closeBracket) {

    // Function call has no arguments, so return false.
    return false;
  }

  // End token of the last argument.
  $end = $this->phpcsFile
    ->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $this->closeBracket - 1, null, true);
  $lastArgEnd = $end;
  $nextSeperator = $this->openBracket;
  $counter = 1;
  while (($nextSeperator = $this->phpcsFile
    ->findNext(T_COMMA, $nextSeperator + 1, $this->closeBracket)) !== false) {

    // Make sure the comma belongs directly to this function call,
    // and is not inside a nested function call or array.
    $brackets = $tokens[$nextSeperator]['nested_parenthesis'];
    $lastBracket = array_pop($brackets);
    if ($lastBracket !== $this->closeBracket) {
      continue;
    }

    // Update the end token of the current argument.
    $end = $this->phpcsFile
      ->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $nextSeperator - 1, null, true);

    // Save the calculated findings for the current argument.
    $this->arguments[$counter] = array(
      'start' => $start,
      'end' => $end,
    );
    if ($counter === $number) {
      break;
    }
    $counter++;
    $start = $this->phpcsFile
      ->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $nextSeperator + 1, null, true);
    $end = $lastArgEnd;
  }

  //end while

  // If the counter did not reach the passed number something is wrong.
  if ($counter !== $number) {
    return false;
  }
  $this->arguments[$counter] = array(
    'start' => $start,
    'end' => $end,
  );
  return $this->arguments[$counter];
}