mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-16 20:36:33 +00:00
Fixed determining the necessary arguments for callable functions
This commit is contained in:
@@ -143,6 +143,7 @@ abstract class Twig_Node_Expression_Call extends Twig_Node_Expression
|
|||||||
$arguments = array();
|
$arguments = array();
|
||||||
$names = array();
|
$names = array();
|
||||||
$missingArguments = array();
|
$missingArguments = array();
|
||||||
|
$optionalArguments = array();
|
||||||
$pos = 0;
|
$pos = 0;
|
||||||
foreach ($definition as $param) {
|
foreach ($definition as $param) {
|
||||||
$names[] = $name = $this->normalizeName($param->name);
|
$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];
|
$arguments[] = $parameters[$name];
|
||||||
unset($parameters[$name]);
|
unset($parameters[$name]);
|
||||||
|
$optionalArguments = array();
|
||||||
} elseif (array_key_exists($pos, $parameters)) {
|
} elseif (array_key_exists($pos, $parameters)) {
|
||||||
|
$arguments = array_merge($arguments, $optionalArguments);
|
||||||
$arguments[] = $parameters[$pos];
|
$arguments[] = $parameters[$pos];
|
||||||
unset($parameters[$pos]);
|
unset($parameters[$pos]);
|
||||||
|
$optionalArguments = array();
|
||||||
++$pos;
|
++$pos;
|
||||||
} elseif ($param->isDefaultValueAvailable()) {
|
} elseif ($param->isDefaultValueAvailable()) {
|
||||||
$arguments[] = new Twig_Node_Expression_Constant($param->getDefaultValue(), -1);
|
$optionalArguments[] = new Twig_Node_Expression_Constant($param->getDefaultValue(), -1);
|
||||||
} elseif ($param->isOptional()) {
|
} elseif ($param->isOptional()) {
|
||||||
if (empty($parameters)) {
|
if (empty($parameters)) {
|
||||||
break;
|
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 = 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));
|
$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
|
class Twig_Tests_Node_Expression_Call extends Twig_Node_Expression_Call
|
||||||
|
|||||||
Reference in New Issue
Block a user