Skip to content

Commit

Permalink
Fixed determining the necessary arguments for callable functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hason committed Jan 22, 2015
1 parent 5f4377b commit e9b8bcc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/Twig/Node/Expression/Call.php
Expand Up @@ -143,6 +143,7 @@ protected function getArguments($callable, $arguments)
$arguments = array();
$names = array();
$missingArguments = array();
$optionalArguments = array();
$pos = 0;
foreach ($definition as $param) {
$names[] = $name = $this->normalizeName($param->name);
Expand All @@ -159,14 +160,18 @@ protected function getArguments($callable, $arguments)
);
}

$arguments = array_merge($arguments, $optionalArguments);
$arguments[] = $parameters[$name];
unset($parameters[$name]);
$optionalArguments = array();
} elseif (array_key_exists($pos, $parameters)) {
$arguments = array_merge($arguments, $optionalArguments);
$arguments[] = $parameters[$pos];
unset($parameters[$pos]);
$optionalArguments = array();
++$pos;
} elseif ($param->isDefaultValueAvailable()) {
$arguments[] = new Twig_Node_Expression_Constant($param->getDefaultValue(), -1);
$optionalArguments[] = new Twig_Node_Expression_Constant($param->getDefaultValue(), -1);
} elseif ($param->isOptional()) {
if (empty($parameters)) {
break;
Expand Down
11 changes: 11 additions & 0 deletions test/Twig/Tests/Node/Expression/CallTest.php
Expand Up @@ -70,6 +70,17 @@ public function testResolveArgumentsWithMissingValueForOptionalArgument()
$node = new Twig_Tests_Node_Expression_Call(array(), array('type' => 'function', 'name' => 'substr_compare'));
$node->getArguments('substr_compare', array('abcd', 'bc', 'offset' => 1, 'case_sensitivity' => true));
}

public function testResolveArgumentsOnlyNecessaryArgumentsForCustomFunction()
{
$node = new Twig_Tests_Node_Expression_Call(array(), array('type' => 'function', 'name' => 'custom_function'));

$this->assertEquals(array('arg1'), $node->getArguments(array($this, 'customFunction'), array('arg1' => 'arg1')));
}

public function customFunction($arg1, $arg2 = 'default', $arg3 = array())
{
}
}

class Twig_Tests_Node_Expression_Call extends Twig_Node_Expression_Call
Expand Down

0 comments on commit e9b8bcc

Please sign in to comment.