You are here

protected function Twig_Node_Expression_Call::getArguments in Translation template extractor 6.3

Same name and namespace in other branches
  1. 7.3 vendor/Twig/Node/Expression/Call.php \Twig_Node_Expression_Call::getArguments()
1 call to Twig_Node_Expression_Call::getArguments()
Twig_Node_Expression_Call::compileArguments in vendor/Twig/Node/Expression/Call.php

File

vendor/Twig/Node/Expression/Call.php, line 91

Class

Twig_Node_Expression_Call

Code

protected function getArguments($callable, $arguments) {
  $parameters = array();
  $named = false;
  foreach ($arguments as $name => $node) {
    if (!is_int($name)) {
      $named = true;
      $name = $this
        ->normalizeName($name);
    }
    elseif ($named) {
      throw new Twig_Error_Syntax(sprintf('Positional arguments cannot be used after named arguments for %s "%s".', $this
        ->getAttribute('type'), $this
        ->getAttribute('name')));
    }
    $parameters[$name] = $node;
  }
  if (!$named) {
    return $parameters;
  }
  if (!$callable) {
    throw new LogicException(sprintf('Named arguments are not supported for %s "%s".', $this
      ->getAttribute('type'), $this
      ->getAttribute('name')));
  }

  // manage named arguments
  if (is_array($callable)) {
    $r = new ReflectionMethod($callable[0], $callable[1]);
  }
  elseif (is_object($callable) && !$callable instanceof Closure) {
    $r = new ReflectionObject($callable);
    $r = $r
      ->getMethod('__invoke');
  }
  else {
    $r = new ReflectionFunction($callable);
  }
  $definition = $r
    ->getParameters();
  if ($this
    ->hasNode('node')) {
    array_shift($definition);
  }
  if ($this
    ->hasAttribute('needs_environment') && $this
    ->getAttribute('needs_environment')) {
    array_shift($definition);
  }
  if ($this
    ->hasAttribute('needs_context') && $this
    ->getAttribute('needs_context')) {
    array_shift($definition);
  }
  if ($this
    ->hasAttribute('arguments') && null !== $this
    ->getAttribute('arguments')) {
    foreach ($this
      ->getAttribute('arguments') as $argument) {
      array_shift($definition);
    }
  }
  $arguments = array();
  $pos = 0;
  foreach ($definition as $param) {
    $name = $this
      ->normalizeName($param->name);
    if (array_key_exists($name, $parameters)) {
      if (array_key_exists($pos, $parameters)) {
        throw new Twig_Error_Syntax(sprintf('Argument "%s" is defined twice for %s "%s".', $name, $this
          ->getAttribute('type'), $this
          ->getAttribute('name')));
      }
      $arguments[] = $parameters[$name];
      unset($parameters[$name]);
    }
    elseif (array_key_exists($pos, $parameters)) {
      $arguments[] = $parameters[$pos];
      unset($parameters[$pos]);
      ++$pos;
    }
    elseif ($param
      ->isDefaultValueAvailable()) {
      $arguments[] = new Twig_Node_Expression_Constant($param
        ->getDefaultValue(), -1);
    }
    elseif ($param
      ->isOptional()) {
      break;
    }
    else {
      throw new Twig_Error_Syntax(sprintf('Value for argument "%s" is required for %s "%s".', $name, $this
        ->getAttribute('type'), $this
        ->getAttribute('name')));
    }
  }
  if (!empty($parameters)) {
    throw new Twig_Error_Syntax(sprintf('Unknown argument%s "%s" for %s "%s".', count($parameters) > 1 ? 's' : '', implode('", "', array_keys($parameters)), $this
      ->getAttribute('type'), $this
      ->getAttribute('name')));
  }
  return $arguments;
}