You are here

public function FunctionCall::getArgument in Coder 8.3.x

Same name and namespace in other branches
  1. 8.3 coder_sniffer/Drupal/Sniffs/Semantics/FunctionCall.php \Drupal\Sniffs\Semantics\FunctionCall::getArgument()
  2. 8.2 coder_sniffer/Drupal/Sniffs/Semantics/FunctionCall.php \Drupal\Sniffs\Semantics\FunctionCall::getArgument()

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>|false

16 calls to FunctionCall::getArgument()
CheckPlainSniff::processFunctionCall in coder_sniffer/DrupalPractice/Sniffs/FunctionCalls/CheckPlainSniff.php
Processes this function call.
ConstantNameSniff::processFunctionCall in coder_sniffer/Drupal/Sniffs/Semantics/ConstantNameSniff.php
Processes this function call.
CurlSslVerifierSniff::processFunctionCall in coder_sniffer/DrupalPractice/Sniffs/FunctionCalls/CurlSslVerifierSniff.php
Processes this function call.
DbQuerySniff::processFunctionCall in coder_sniffer/DrupalPractice/Sniffs/FunctionCalls/DbQuerySniff.php
Processes this function call.
DbSelectBracesSniff::processFunctionCall in coder_sniffer/DrupalPractice/Sniffs/FunctionCalls/DbSelectBracesSniff.php
Processes this function call.

... See full list

File

coder_sniffer/Drupal/Sniffs/Semantics/FunctionCall.php, line 175

Class

FunctionCall
Helper class to sniff for specific function calls.

Namespace

Drupal\Sniffs\Semantics

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(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(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(Tokens::$emptyTokens, $nextSeperator - 1, null, true);

    // Save the calculated findings for the current argument.
    $this->arguments[$counter] = [
      'start' => $start,
      'end' => $end,
    ];
    if ($counter === $number) {
      break;
    }
    $counter++;
    $start = $this->phpcsFile
      ->findNext(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] = [
    'start' => $start,
    'end' => $end,
  ];
  return $this->arguments[$counter];
}