switched to use short arrays in generated code

This commit is contained in:
Fabien Potencier
2019-01-08 09:28:14 +01:00
parent 637028117a
commit 8758b570b8
11 changed files with 21 additions and 20 deletions
+1
View File
@@ -1,5 +1,6 @@
* 1.37.0 (2019-XX-XX)
* switched generated code to use the PHP short array notation
* dropped PHP 5.3 support
* added a second argument to the join filter (last separator configuration)
+2 -2
View File
@@ -185,7 +185,7 @@ class Twig_Compiler implements Twig_CompilerInterface
} elseif (is_bool($value)) {
$this->raw($value ? 'true' : 'false');
} elseif (is_array($value)) {
$this->raw('array(');
$this->raw('[');
$first = true;
foreach ($value as $key => $v) {
if (!$first) {
@@ -196,7 +196,7 @@ class Twig_Compiler implements Twig_CompilerInterface
$this->raw(' => ');
$this->repr($v);
}
$this->raw(')');
$this->raw(']');
} else {
$this->string($value);
}
+2 -2
View File
@@ -62,7 +62,7 @@ class Twig_Node_Expression_Array extends Twig_Node_Expression
public function compile(Twig_Compiler $compiler)
{
$compiler->raw('array(');
$compiler->raw('[');
$first = true;
foreach ($this->getKeyValuePairs() as $pair) {
if (!$first) {
@@ -76,7 +76,7 @@ class Twig_Node_Expression_Array extends Twig_Node_Expression
->subcompile($pair['value'])
;
}
$compiler->raw(')');
$compiler->raw(']');
}
}
+2 -2
View File
@@ -69,7 +69,7 @@ class Twig_Node_Set extends Twig_Node implements Twig_NodeCaptureInterface
$compiler->raw(' = ');
if (count($this->getNode('names')) > 1) {
$compiler->write('array(');
$compiler->write('[');
foreach ($this->getNode('values') as $idx => $value) {
if ($idx) {
$compiler->raw(', ');
@@ -77,7 +77,7 @@ class Twig_Node_Set extends Twig_Node implements Twig_NodeCaptureInterface
$compiler->subcompile($value);
}
$compiler->raw(')');
$compiler->raw(']');
} else {
if ($this->getAttribute('safe')) {
$compiler
@@ -31,7 +31,7 @@ class Twig_Tests_Node_Expression_ArrayTest extends Twig_Test_NodeTestCase
$node = new Twig_Node_Expression_Array($elements, 1);
return array(
array($node, 'array("foo" => "bar", "bar" => "foo")'),
array($node, '["foo" => "bar", "bar" => "foo"]'),
);
}
}
@@ -85,7 +85,7 @@ class Twig_Tests_Node_Expression_FilterTest extends Twig_Test_NodeTestCase
$tests[] = array($node, 'twig_tests_filter_barbar($context, "abc")', $environment);
$node = $this->createFilter($string, 'barbar', array('foo' => new Twig_Node_Expression_Constant('bar', 1)));
$tests[] = array($node, 'twig_tests_filter_barbar($context, "abc", null, null, array("foo" => "bar"))', $environment);
$tests[] = array($node, 'twig_tests_filter_barbar($context, "abc", null, null, ["foo" => "bar"])', $environment);
$node = $this->createFilter($string, 'barbar', array('arg2' => new Twig_Node_Expression_Constant('bar', 1)));
$tests[] = array($node, 'twig_tests_filter_barbar($context, "abc", null, "bar")', $environment);
@@ -96,7 +96,7 @@ class Twig_Tests_Node_Expression_FilterTest extends Twig_Test_NodeTestCase
new Twig_Node_Expression_Constant('3', 1),
'foo' => new Twig_Node_Expression_Constant('bar', 1),
));
$tests[] = array($node, 'twig_tests_filter_barbar($context, "abc", "1", "2", array(0 => "3", "foo" => "bar"))', $environment);
$tests[] = array($node, 'twig_tests_filter_barbar($context, "abc", "1", "2", [0 => "3", "foo" => "bar"])', $environment);
return $tests;
}
@@ -68,7 +68,7 @@ class Twig_Tests_Node_Expression_FunctionTest extends Twig_Test_NodeTestCase
$tests[] = array($node, 'twig_tests_function_barbar()', $environment);
$node = $this->createFunction('barbar', array('foo' => new Twig_Node_Expression_Constant('bar', 1)));
$tests[] = array($node, 'twig_tests_function_barbar(null, null, array("foo" => "bar"))', $environment);
$tests[] = array($node, 'twig_tests_function_barbar(null, null, ["foo" => "bar"])', $environment);
$node = $this->createFunction('barbar', array('arg2' => new Twig_Node_Expression_Constant('bar', 1)));
$tests[] = array($node, 'twig_tests_function_barbar(null, "bar")', $environment);
@@ -79,7 +79,7 @@ class Twig_Tests_Node_Expression_FunctionTest extends Twig_Test_NodeTestCase
new Twig_Node_Expression_Constant('3', 1),
'foo' => new Twig_Node_Expression_Constant('bar', 1),
));
$tests[] = array($node, 'twig_tests_function_barbar("1", "2", array(0 => "3", "foo" => "bar"))', $environment);
$tests[] = array($node, 'twig_tests_function_barbar("1", "2", [0 => "3", "foo" => "bar"])', $environment);
// function as an anonymous function
if (PHP_VERSION_ID >= 50300) {
@@ -34,16 +34,16 @@ class Twig_Tests_Node_Expression_GetAttrTest extends Twig_Test_NodeTestCase
$attr = new Twig_Node_Expression_Constant('bar', 1);
$args = new Twig_Node_Expression_Array(array(), 1);
$node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Template::ANY_CALL, 1);
$tests[] = array($node, sprintf('%s%s, "bar", array())', $this->getAttributeGetter(), $this->getVariableGetter('foo', 1)));
$tests[] = array($node, sprintf('%s%s, "bar", [])', $this->getAttributeGetter(), $this->getVariableGetter('foo', 1)));
$node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Template::ARRAY_CALL, 1);
$tests[] = array($node, sprintf('%s%s, "bar", array(), "array")', $this->getAttributeGetter(), $this->getVariableGetter('foo', 1)));
$tests[] = array($node, sprintf('%s%s, "bar", [], "array")', $this->getAttributeGetter(), $this->getVariableGetter('foo', 1)));
$args = new Twig_Node_Expression_Array(array(), 1);
$args->addElement(new Twig_Node_Expression_Name('foo', 1));
$args->addElement(new Twig_Node_Expression_Constant('bar', 1));
$node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Template::METHOD_CALL, 1);
$tests[] = array($node, sprintf('%s%s, "bar", array(0 => %s, 1 => "bar"), "method")', $this->getAttributeGetter(), $this->getVariableGetter('foo', 1), $this->getVariableGetter('foo')));
$tests[] = array($node, sprintf('%s%s, "bar", [0 => %s, 1 => "bar"], "method")', $this->getAttributeGetter(), $this->getVariableGetter('foo', 1), $this->getVariableGetter('foo')));
return $tests;
}
+2 -2
View File
@@ -46,7 +46,7 @@ class Twig_Tests_Node_Expression_TestTest extends Twig_Test_NodeTestCase
$tests[] = array($node, 'twig_tests_test_barbar("abc")', $environment);
$node = $this->createTest($string, 'barbar', array('foo' => new Twig_Node_Expression_Constant('bar', 1)));
$tests[] = array($node, 'twig_tests_test_barbar("abc", null, null, array("foo" => "bar"))', $environment);
$tests[] = array($node, 'twig_tests_test_barbar("abc", null, null, ["foo" => "bar"])', $environment);
$node = $this->createTest($string, 'barbar', array('arg2' => new Twig_Node_Expression_Constant('bar', 1)));
$tests[] = array($node, 'twig_tests_test_barbar("abc", null, "bar")', $environment);
@@ -57,7 +57,7 @@ class Twig_Tests_Node_Expression_TestTest extends Twig_Test_NodeTestCase
new Twig_Node_Expression_Constant('3', 1),
'foo' => new Twig_Node_Expression_Constant('bar', 1),
));
$tests[] = array($node, 'twig_tests_test_barbar("abc", "1", "2", array(0 => "3", "foo" => "bar"))', $environment);
$tests[] = array($node, 'twig_tests_test_barbar("abc", "1", "2", [0 => "3", "foo" => "bar"])', $environment);
return $tests;
}
+3 -3
View File
@@ -56,14 +56,14 @@ EOF
$node = new Twig_Node_Include($expr, $vars, false, false, 1);
$tests[] = array($node, <<<EOF
// line 1
\$this->loadTemplate("foo.twig", null, 1)->display(array_merge(\$context, array("foo" => true)));
\$this->loadTemplate("foo.twig", null, 1)->display(array_merge(\$context, ["foo" => true]));
EOF
);
$node = new Twig_Node_Include($expr, $vars, true, false, 1);
$tests[] = array($node, <<<EOF
// line 1
\$this->loadTemplate("foo.twig", null, 1)->display(array("foo" => true));
\$this->loadTemplate("foo.twig", null, 1)->display(["foo" => true]);
EOF
);
@@ -71,7 +71,7 @@ EOF
$tests[] = array($node, <<<EOF
// line 1
try {
\$this->loadTemplate("foo.twig", null, 1)->display(array("foo" => true));
\$this->loadTemplate("foo.twig", null, 1)->display(["foo" => true]);
} catch (Twig_Error_Loader \$e) {
// ignore missing template
}
+1 -1
View File
@@ -60,7 +60,7 @@ EOF
$node = new Twig_Node_Set(false, $names, $values, 1);
$tests[] = array($node, <<<EOF
// line 1
list(\$context["foo"], \$context["bar"]) = array("foo", {$this->getVariableGetter('bar')});
list(\$context["foo"], \$context["bar"]) = ["foo", {$this->getVariableGetter('bar')}];
EOF
);