From 0446296d5fcb981b0b4b41a2e70575e98bf81620 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 8 Jan 2019 10:11:09 +0100 Subject: [PATCH] fixed more short array notations --- lib/Twig/Node/Block.php | 2 +- lib/Twig/Node/CheckSecurity.php | 6 +++--- lib/Twig/Node/Expression/Call.php | 14 ++++++++------ lib/Twig/Node/Expression/GetAttr.php | 2 +- lib/Twig/Node/For.php | 4 ++-- lib/Twig/Node/Include.php | 2 +- lib/Twig/Node/Macro.php | 8 ++++---- lib/Twig/Node/Module.php | 19 ++++++++++++------- lib/Twig/Node/With.php | 2 +- test/Twig/Tests/Node/BlockTest.php | 2 +- test/Twig/Tests/Node/Expression/CallTest.php | 6 +++--- .../Twig/Tests/Node/Expression/FilterTest.php | 2 +- .../Tests/Node/Expression/FunctionTest.php | 2 +- test/Twig/Tests/Node/Expression/TestTest.php | 2 +- test/Twig/Tests/Node/ForTest.php | 12 ++++++------ test/Twig/Tests/Node/MacroTest.php | 8 ++++---- test/Twig/Tests/Node/ModuleTest.php | 14 +++++++------- 17 files changed, 57 insertions(+), 50 deletions(-) diff --git a/lib/Twig/Node/Block.php b/lib/Twig/Node/Block.php index 7676136fb..2d0300ba1 100644 --- a/lib/Twig/Node/Block.php +++ b/lib/Twig/Node/Block.php @@ -26,7 +26,7 @@ class Twig_Node_Block extends Twig_Node { $compiler ->addDebugInfo($this) - ->write(sprintf("public function block_%s(\$context, array \$blocks = array())\n", $this->getAttribute('name')), "{\n") + ->write(sprintf("public function block_%s(\$context, array \$blocks = [])\n", $this->getAttribute('name')), "{\n") ->indent() ; diff --git a/lib/Twig/Node/CheckSecurity.php b/lib/Twig/Node/CheckSecurity.php index b787eb262..aecdb8322 100644 --- a/lib/Twig/Node/CheckSecurity.php +++ b/lib/Twig/Node/CheckSecurity.php @@ -48,9 +48,9 @@ class Twig_Node_CheckSecurity extends Twig_Node ->indent() ->write("\$this->env->getExtension('Twig_Extension_Sandbox')->checkSecurity(\n") ->indent() - ->write(!$tags ? "array(),\n" : "array('".implode("', '", array_keys($tags))."'),\n") - ->write(!$filters ? "array(),\n" : "array('".implode("', '", array_keys($filters))."'),\n") - ->write(!$functions ? "array()\n" : "array('".implode("', '", array_keys($functions))."')\n") + ->write(!$tags ? "[],\n" : "['".implode("', '", array_keys($tags))."'],\n") + ->write(!$filters ? "[],\n" : "['".implode("', '", array_keys($filters))."'],\n") + ->write(!$functions ? "[]\n" : "['".implode("', '", array_keys($functions))."']\n") ->outdent() ->write(");\n") ->outdent() diff --git a/lib/Twig/Node/Expression/Call.php b/lib/Twig/Node/Expression/Call.php index 5bdd96dd0..d1d30259f 100644 --- a/lib/Twig/Node/Expression/Call.php +++ b/lib/Twig/Node/Expression/Call.php @@ -15,6 +15,7 @@ abstract class Twig_Node_Expression_Call extends Twig_Node_Expression protected function compileCallable(Twig_Compiler $compiler) { $closingParenthesis = false; + $isArray = false; if ($this->hasAttribute('callable') && $callable = $this->getAttribute('callable')) { if (is_string($callable) && false === strpos($callable, '::')) { $compiler->raw($callable); @@ -30,24 +31,25 @@ abstract class Twig_Node_Expression_Call extends Twig_Node_Expression $compiler->raw(sprintf('$this->env->getExtension(\'%s\')->%s', get_class($callable[0]), $callable[1])); } else { $type = ucfirst($this->getAttribute('type')); - $compiler->raw(sprintf('call_user_func_array($this->env->get%s(\'%s\')->getCallable(), array', $type, $this->getAttribute('name'))); + $compiler->raw(sprintf('call_user_func_array($this->env->get%s(\'%s\')->getCallable(), ', $type, $this->getAttribute('name'))); $closingParenthesis = true; + $isArray = true; } } } else { $compiler->raw($this->getAttribute('thing')->compile()); } - $this->compileArguments($compiler); + $this->compileArguments($compiler, $isArray); if ($closingParenthesis) { $compiler->raw(')'); } } - protected function compileArguments(Twig_Compiler $compiler) + protected function compileArguments(Twig_Compiler $compiler, $isArray = false) { - $compiler->raw('('); + $compiler->raw($isArray ? '[' : '('); $first = true; @@ -96,7 +98,7 @@ abstract class Twig_Node_Expression_Call extends Twig_Node_Expression } } - $compiler->raw(')'); + $compiler->raw($isArray ? ']' : ')'); } protected function getArguments($callable, $arguments) @@ -248,7 +250,7 @@ abstract class Twig_Node_Expression_Call extends Twig_Node_Expression $callableName = $r->getDeclaringClass()->name.'::'.$callableName; } - throw new LogicException(sprintf('The last parameter of "%s" for %s "%s" must be an array with default value, eg. "array $arg = array()".', $callableName, $this->getAttribute('type'), $this->getAttribute('name'))); + throw new LogicException(sprintf('The last parameter of "%s" for %s "%s" must be an array with default value, eg. "array $arg = []".', $callableName, $this->getAttribute('type'), $this->getAttribute('name'))); } } diff --git a/lib/Twig/Node/Expression/GetAttr.php b/lib/Twig/Node/Expression/GetAttr.php index a5fcb9d10..897691508 100644 --- a/lib/Twig/Node/Expression/GetAttr.php +++ b/lib/Twig/Node/Expression/GetAttr.php @@ -51,7 +51,7 @@ class Twig_Node_Expression_GetAttr extends Twig_Node_Expression if ($this->hasNode('arguments')) { $compiler->raw(', ')->subcompile($this->getNode('arguments')); } else { - $compiler->raw(', array()'); + $compiler->raw(', []'); } } diff --git a/lib/Twig/Node/For.php b/lib/Twig/Node/For.php index c8918ed3b..765194579 100644 --- a/lib/Twig/Node/For.php +++ b/lib/Twig/Node/For.php @@ -51,12 +51,12 @@ class Twig_Node_For extends Twig_Node if ($this->getAttribute('with_loop')) { $compiler - ->write("\$context['loop'] = array(\n") + ->write("\$context['loop'] = [\n") ->write(" 'parent' => \$context['_parent'],\n") ->write(" 'index0' => 0,\n") ->write(" 'index' => 1,\n") ->write(" 'first' => true,\n") - ->write(");\n") + ->write("];\n") ; if (!$this->getAttribute('ifexpr')) { diff --git a/lib/Twig/Node/Include.php b/lib/Twig/Node/Include.php index 7de7f9708..4b263819f 100644 --- a/lib/Twig/Node/Include.php +++ b/lib/Twig/Node/Include.php @@ -74,7 +74,7 @@ class Twig_Node_Include extends Twig_Node implements Twig_NodeOutputInterface protected function addTemplateArguments(Twig_Compiler $compiler) { if (!$this->hasNode('variables')) { - $compiler->raw(false === $this->getAttribute('only') ? '$context' : 'array()'); + $compiler->raw(false === $this->getAttribute('only') ? '$context' : '[]'); } elseif (false === $this->getAttribute('only')) { $compiler ->raw('array_merge($context, ') diff --git a/lib/Twig/Node/Macro.php b/lib/Twig/Node/Macro.php index 210459cd1..0c4f9286a 100644 --- a/lib/Twig/Node/Macro.php +++ b/lib/Twig/Node/Macro.php @@ -64,7 +64,7 @@ class Twig_Node_Macro extends Twig_Node ; $compiler - ->write("\$context = \$this->env->mergeGlobals(array(\n") + ->write("\$context = \$this->env->mergeGlobals([\n") ->indent() ; @@ -91,14 +91,14 @@ class Twig_Node_Macro extends Twig_Node ->repr($count) ->raw(' ? array_slice(func_get_args(), ') ->repr($count) - ->raw(") : array(),\n") + ->raw(") : [],\n") ; } $compiler ->outdent() - ->write("));\n\n") - ->write("\$blocks = array();\n\n") + ->write("]);\n\n") + ->write("\$blocks = [];\n\n") ->write("ob_start();\n") ->write("try {\n") ->indent() diff --git a/lib/Twig/Node/Module.php b/lib/Twig/Node/Module.php index 55cfffdaa..c33bbe157 100644 --- a/lib/Twig/Node/Module.php +++ b/lib/Twig/Node/Module.php @@ -257,11 +257,11 @@ class Twig_Node_Module extends Twig_Node ->write("\$this->blocks = array_merge(\n") ->indent() ->write("\$this->traits,\n") - ->write("array(\n") + ->write("[\n") ; } else { $compiler - ->write("\$this->blocks = array(\n") + ->write("\$this->blocks = [\n") ; } @@ -272,20 +272,25 @@ class Twig_Node_Module extends Twig_Node foreach ($this->getNode('blocks') as $name => $node) { $compiler - ->write(sprintf("'%s' => array(\$this, 'block_%s'),\n", $name, $name)) + ->write(sprintf("'%s' => [\$this, 'block_%s'],\n", $name, $name)) ; } if ($countTraits) { $compiler ->outdent() - ->write(")\n") + ->write("]\n") + ->outdent() + ->write(");\n") + ; + } else { + $compiler + ->outdent() + ->write("];\n") ; } $compiler - ->outdent() - ->write(");\n") ->outdent() ->subcompile($this->getNode('constructor_end')) ->write("}\n\n") @@ -295,7 +300,7 @@ class Twig_Node_Module extends Twig_Node protected function compileDisplay(Twig_Compiler $compiler) { $compiler - ->write("protected function doDisplay(array \$context, array \$blocks = array())\n", "{\n") + ->write("protected function doDisplay(array \$context, array \$blocks = [])\n", "{\n") ->indent() ->subcompile($this->getNode('display_start')) ->subcompile($this->getNode('body')) diff --git a/lib/Twig/Node/With.php b/lib/Twig/Node/With.php index 504c8f0ba..422cc8122 100644 --- a/lib/Twig/Node/With.php +++ b/lib/Twig/Node/With.php @@ -44,7 +44,7 @@ class Twig_Node_With extends Twig_Node ; if ($this->getAttribute('only')) { - $compiler->write("\$context = array('_parent' => \$context);\n"); + $compiler->write("\$context = ['_parent' => \$context];\n"); } else { $compiler->write("\$context['_parent'] = \$context;\n"); } diff --git a/test/Twig/Tests/Node/BlockTest.php b/test/Twig/Tests/Node/BlockTest.php index ba32989ef..0b347df02 100644 --- a/test/Twig/Tests/Node/BlockTest.php +++ b/test/Twig/Tests/Node/BlockTest.php @@ -28,7 +28,7 @@ class Twig_Tests_Node_BlockTest extends Twig_Test_NodeTestCase return [ [$node, <<= 50300) { $node = $this->createFilter(new Twig_Node_Expression_Constant('foo', 1), 'anonymous'); - $tests[] = [$node, 'call_user_func_array($this->env->getFilter(\'anonymous\')->getCallable(), array("foo"))']; + $tests[] = [$node, 'call_user_func_array($this->env->getFilter(\'anonymous\')->getCallable(), ["foo"])']; } // needs environment diff --git a/test/Twig/Tests/Node/Expression/FunctionTest.php b/test/Twig/Tests/Node/Expression/FunctionTest.php index 73d7994e3..ef1d29cd3 100644 --- a/test/Twig/Tests/Node/Expression/FunctionTest.php +++ b/test/Twig/Tests/Node/Expression/FunctionTest.php @@ -84,7 +84,7 @@ class Twig_Tests_Node_Expression_FunctionTest extends Twig_Test_NodeTestCase // function as an anonymous function if (PHP_VERSION_ID >= 50300) { $node = $this->createFunction('anonymous', [new Twig_Node_Expression_Constant('foo', 1)]); - $tests[] = [$node, 'call_user_func_array($this->env->getFunction(\'anonymous\')->getCallable(), array("foo"))']; + $tests[] = [$node, 'call_user_func_array($this->env->getFunction(\'anonymous\')->getCallable(), ["foo"])']; } return $tests; diff --git a/test/Twig/Tests/Node/Expression/TestTest.php b/test/Twig/Tests/Node/Expression/TestTest.php index 98e8a3dd2..28de6cbfd 100644 --- a/test/Twig/Tests/Node/Expression/TestTest.php +++ b/test/Twig/Tests/Node/Expression/TestTest.php @@ -37,7 +37,7 @@ class Twig_Tests_Node_Expression_TestTest extends Twig_Test_NodeTestCase // test as an anonymous function if (PHP_VERSION_ID >= 50300) { $node = $this->createTest(new Twig_Node_Expression_Constant('foo', 1), 'anonymous', [new Twig_Node_Expression_Constant('foo', 1)]); - $tests[] = [$node, 'call_user_func_array($this->env->getTest(\'anonymous\')->getCallable(), array("foo", "foo"))']; + $tests[] = [$node, 'call_user_func_array($this->env->getTest(\'anonymous\')->getCallable(), ["foo", "foo"])']; } // arbitrary named arguments diff --git a/test/Twig/Tests/Node/ForTest.php b/test/Twig/Tests/Node/ForTest.php index 0bab93c2d..278aeda3f 100644 --- a/test/Twig/Tests/Node/ForTest.php +++ b/test/Twig/Tests/Node/ForTest.php @@ -75,12 +75,12 @@ EOF // line 1 \$context['_parent'] = \$context; \$context['_seq'] = twig_ensure_traversable({$this->getVariableGetter('values')}); -\$context['loop'] = array( +\$context['loop'] = [ 'parent' => \$context['_parent'], 'index0' => 0, 'index' => 1, 'first' => true, -); +]; if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) { \$length = count(\$context['_seq']); \$context['loop']['revindex0'] = \$length - 1; @@ -118,12 +118,12 @@ EOF // line 1 \$context['_parent'] = \$context; \$context['_seq'] = twig_ensure_traversable({$this->getVariableGetter('values')}); -\$context['loop'] = array( +\$context['loop'] = [ 'parent' => \$context['_parent'], 'index0' => 0, 'index' => 1, 'first' => true, -); +]; foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) { if (true) { echo {$this->getVariableGetter('foo')}; @@ -152,12 +152,12 @@ EOF \$context['_parent'] = \$context; \$context['_seq'] = twig_ensure_traversable({$this->getVariableGetter('values')}); \$context['_iterated'] = false; -\$context['loop'] = array( +\$context['loop'] = [ 'parent' => \$context['_parent'], 'index0' => 0, 'index' => 1, 'first' => true, -); +]; if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) { \$length = count(\$context['_seq']); \$context['loop']['revindex0'] = \$length - 1; diff --git a/test/Twig/Tests/Node/MacroTest.php b/test/Twig/Tests/Node/MacroTest.php index 1c1b113fc..fca11af04 100644 --- a/test/Twig/Tests/Node/MacroTest.php +++ b/test/Twig/Tests/Node/MacroTest.php @@ -36,7 +36,7 @@ class Twig_Tests_Node_MacroTest extends Twig_Test_NodeTestCase $varargs = '$__varargs__'; } else { $declaration = ''; - $varargs = 'func_num_args() > 2 ? array_slice(func_get_args(), 2) : array()'; + $varargs = 'func_num_args() > 2 ? array_slice(func_get_args(), 2) : []'; } return [ @@ -44,13 +44,13 @@ class Twig_Tests_Node_MacroTest extends Twig_Test_NodeTestCase // line 1 public function getfoo(\$__foo__ = null, \$__bar__ = "Foo"$declaration) { - \$context = \$this->env->mergeGlobals(array( + \$context = \$this->env->mergeGlobals([ "foo" => \$__foo__, "bar" => \$__bar__, "varargs" => $varargs, - )); + ]); - \$blocks = array(); + \$blocks = []; ob_start(); try { diff --git a/test/Twig/Tests/Node/ModuleTest.php b/test/Twig/Tests/Node/ModuleTest.php index 930138f5d..07be19547 100644 --- a/test/Twig/Tests/Node/ModuleTest.php +++ b/test/Twig/Tests/Node/ModuleTest.php @@ -54,11 +54,11 @@ class __TwigTemplate_%x extends Twig_Template \$this->parent = false; - \$this->blocks = array( - ); + \$this->blocks = [ + ]; } - protected function doDisplay(array \$context, array \$blocks = array()) + protected function doDisplay(array \$context, array \$blocks = []) { // line 1 echo "foo"; @@ -108,8 +108,8 @@ class __TwigTemplate_%x extends Twig_Template // line 1 \$this->parent = \$this->loadTemplate("layout.twig", "foo.twig", 1); - \$this->blocks = array( - ); + \$this->blocks = [ + ]; } protected function doGetParent(array \$context) @@ -117,7 +117,7 @@ class __TwigTemplate_%x extends Twig_Template return "layout.twig"; } - protected function doDisplay(array \$context, array \$blocks = array()) + protected function doDisplay(array \$context, array \$blocks = []) { // line 2 \$context["macro"] = \$this->loadTemplate("foo.twig", "foo.twig", 2); @@ -179,7 +179,7 @@ class __TwigTemplate_%x extends Twig_Template return \$this->loadTemplate(((true) ? ("foo") : ("foo")), "foo.twig", 2); } - protected function doDisplay(array \$context, array \$blocks = array()) + protected function doDisplay(array \$context, array \$blocks = []) { // line 4 \$context["foo"] = "foo";