enforced usage of named arguments after positional ones

This commit is contained in:
Fabien Potencier
2013-05-01 08:13:09 +02:00
parent 2acd63ae31
commit 135b618ff9
8 changed files with 61 additions and 11 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
* 1.12.4 (2013-XX-XX) * 1.13.0 (2013-XX-XX)
* n/a * enforced usage of named arguments after positional ones
* 1.12.3 (2013-04-08) * 1.12.3 (2013-04-08)
+1 -1
View File
@@ -25,7 +25,7 @@
}, },
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-master": "1.12-dev" "dev-master": "1.13-dev"
} }
} }
} }
+2 -4
View File
@@ -227,14 +227,12 @@ to change the default value:
{# or skip the format value by using a named argument for the timezone #} {# or skip the format value by using a named argument for the timezone #}
{{ "now"|date(timezone="Europe/Paris") }} {{ "now"|date(timezone="Europe/Paris") }}
You can also use both positional and named arguments in one call, which is not You can also use both positional and named arguments in one call, in which
recommended as it can be confusing: case positional arguments must always come before named arguments:
.. code-block:: jinja .. code-block:: jinja
{# both work #}
{{ "now"|date('d/m/Y H:i', timezone="Europe/Paris") }} {{ "now"|date('d/m/Y H:i', timezone="Europe/Paris") }}
{{ "now"|date(timezone="Europe/Paris", 'd/m/Y H:i') }}
.. tip:: .. tip::
+1 -1
View File
@@ -15,7 +15,7 @@
#ifndef PHP_TWIG_H #ifndef PHP_TWIG_H
#define PHP_TWIG_H #define PHP_TWIG_H
#define PHP_TWIG_VERSION "1.12.4-DEV" #define PHP_TWIG_VERSION "1.13.0-DEV"
#include "php.h" #include "php.h"
+1 -1
View File
@@ -16,7 +16,7 @@
*/ */
class Twig_Environment class Twig_Environment
{ {
const VERSION = '1.12.4-DEV'; const VERSION = '1.13.0-DEV';
protected $charset; protected $charset;
protected $loader; protected $loader;
+7
View File
@@ -98,7 +98,10 @@ abstract class Twig_Node_Expression_Call extends Twig_Node_Expression
if (!is_int($name)) { if (!is_int($name)) {
$named = true; $named = true;
$name = $this->normalizeName($name); $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; $parameters[$name] = $node;
} }
@@ -142,6 +145,10 @@ abstract class Twig_Node_Expression_Call extends Twig_Node_Expression
$name = $this->normalizeName($param->name); $name = $this->normalizeName($param->name);
if (array_key_exists($name, $parameters)) { if (array_key_exists($name, $parameters)) {
if (array_key_exists($pos, $parameters)) {
throw new Twig_Error_Syntax(sprintf('Arguments "%s" is defined twice for %s "%s".', $name, $this->getAttribute('type'), $this->getAttribute('name')));
}
$arguments[] = $parameters[$name]; $arguments[] = $parameters[$name];
unset($parameters[$name]); unset($parameters[$name]);
} elseif (array_key_exists($pos, $parameters)) { } elseif (array_key_exists($pos, $parameters)) {
@@ -3,7 +3,6 @@
--TEMPLATE-- --TEMPLATE--
{{ date|date(format='d/m/Y H:i:s P', timezone='America/Chicago') }} {{ date|date(format='d/m/Y H:i:s P', timezone='America/Chicago') }}
{{ date|date(timezone='America/Chicago', format='d/m/Y H:i:s P') }} {{ date|date(timezone='America/Chicago', format='d/m/Y H:i:s P') }}
{{ date|date(timezone='America/Chicago', 'd/m/Y H:i:s P') }}
{{ date|date('d/m/Y H:i:s P', timezone='America/Chicago') }} {{ date|date('d/m/Y H:i:s P', timezone='America/Chicago') }}
--DATA-- --DATA--
date_default_timezone_set('UTC'); date_default_timezone_set('UTC');
@@ -12,4 +11,3 @@ return array('date' => mktime(13, 45, 0, 10, 4, 2010))
04/10/2010 08:45:00 -05:00 04/10/2010 08:45:00 -05:00
04/10/2010 08:45:00 -05:00 04/10/2010 08:45:00 -05:00
04/10/2010 08:45:00 -05:00 04/10/2010 08:45:00 -05:00
04/10/2010 08:45:00 -05:00
@@ -0,0 +1,47 @@
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Tests_Node_Expression_CallTest extends PHPUnit_Framework_TestCase
{
public function testGetArguments()
{
$node = new Twig_Tests_Node_Expression_Call(array(), array('type' => 'function', 'name' => 'date'));
$this->assertEquals(array('U'), $node->getArguments('date', array('format' => 'U')));
}
/**
* @expectedException Twig_Error_Syntax
* @expectedExceptionMessage Positional arguments cannot be used after named arguments for function "date".
*/
public function testGetArgumentsWhenPositionalArgumentsAfterNamedArguments()
{
$node = new Twig_Tests_Node_Expression_Call(array(), array('type' => 'function', 'name' => 'date'));
$node->getArguments('date', array('timestamp' => 123456, 'Y-m-d'));
}
/**
* @expectedException Twig_Error_Syntax
* @expectedExceptionMessage Arguments "format" is defined twice for function "date".
*/
public function testGetArgumentsWhenArgumentIsDefinedTwice()
{
$node = new Twig_Tests_Node_Expression_Call(array(), array('type' => 'function', 'name' => 'date'));
$node->getArguments('date', array('Y-m-d', 'format' => 'U'));
}
}
class Twig_Tests_Node_Expression_Call extends Twig_Node_Expression_Call
{
public function getArguments($callable, $arguments)
{
return parent::getArguments($callable, $arguments);
}
}