Fixed determining the necessary arguments for callable functions

This commit is contained in:
Martin Hasoň
2014-10-15 08:42:40 +02:00
parent 5f4377b863
commit e9b8bccec3
2 changed files with 17 additions and 1 deletions
+6 -1
View File
@@ -143,6 +143,7 @@ abstract class Twig_Node_Expression_Call extends Twig_Node_Expression
$arguments = array();
$names = array();
$missingArguments = array();
$optionalArguments = array();
$pos = 0;
foreach ($definition as $param) {
$names[] = $name = $this->normalizeName($param->name);
@@ -159,14 +160,18 @@ abstract class Twig_Node_Expression_Call extends Twig_Node_Expression
);
}
$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;
@@ -70,6 +70,17 @@ class Twig_Tests_Node_Expression_CallTest extends PHPUnit_Framework_TestCase
$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