From: Fabien Potencier Date: Thu, 1 Sep 2011 08:40:40 +0000 (+0200) Subject: removed some loop variable when using an if statement in a for loop X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=9c2065faccad20c0e8c096e24aab80b244f91b42;p=web%2Fkonrad%2Ftwig.git removed some loop variable when using an if statement in a for loop --- diff --git a/lib/Twig/Node/For.php b/lib/Twig/Node/For.php index 6c3cebc..eb204e2 100644 --- a/lib/Twig/Node/For.php +++ b/lib/Twig/Node/For.php @@ -51,16 +51,21 @@ class Twig_Node_For extends Twig_Node ->write(" 'index' => 1,\n") ->write(" 'first' => true,\n") ->write(");\n") - ->write("if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) {\n") - ->indent() - ->write("\$length = count(\$context['_seq']);\n") - ->write("\$context['loop']['revindex0'] = \$length - 1;\n") - ->write("\$context['loop']['revindex'] = \$length;\n") - ->write("\$context['loop']['length'] = \$length;\n") - ->write("\$context['loop']['last'] = 1 === \$length;\n") - ->outdent() - ->write("}\n") ; + + if (null === $this->getNode('ifexpr')) { + $compiler + ->write("if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) {\n") + ->indent() + ->write("\$length = count(\$context['_seq']);\n") + ->write("\$context['loop']['revindex0'] = \$length - 1;\n") + ->write("\$context['loop']['revindex'] = \$length;\n") + ->write("\$context['loop']['length'] = \$length;\n") + ->write("\$context['loop']['last'] = 1 === \$length;\n") + ->outdent() + ->write("}\n") + ; + } } $compiler @@ -74,10 +79,13 @@ class Twig_Node_For extends Twig_Node if (null !== $this->getNode('ifexpr')) { $compiler - ->write("if (") + ->write("if (!(") ->subcompile($this->getNode('ifexpr')) - ->raw(") {\n") + ->raw(")) {\n") ->indent() + ->write("continue;\n") + ->outdent() + ->write("}\n\n") ; } @@ -92,21 +100,19 @@ class Twig_Node_For extends Twig_Node ->write("++\$context['loop']['index0'];\n") ->write("++\$context['loop']['index'];\n") ->write("\$context['loop']['first'] = false;\n") - ->write("if (isset(\$context['loop']['length'])) {\n") - ->indent() - ->write("--\$context['loop']['revindex0'];\n") - ->write("--\$context['loop']['revindex'];\n") - ->write("\$context['loop']['last'] = 0 === \$context['loop']['revindex0'];\n") - ->outdent() - ->write("}\n") ; - } - if (null !== $this->getNode('ifexpr')) { - $compiler - ->outdent() - ->write("}\n") - ; + if (null === $this->getNode('ifexpr')) { + $compiler + ->write("if (isset(\$context['loop']['length'])) {\n") + ->indent() + ->write("--\$context['loop']['revindex0'];\n") + ->write("--\$context['loop']['revindex'];\n") + ->write("\$context['loop']['last'] = 0 === \$context['loop']['revindex0'];\n") + ->outdent() + ->write("}\n") + ; + } } $compiler diff --git a/test/Twig/Tests/Node/ForTest.php b/test/Twig/Tests/Node/ForTest.php index 098a11f..8841ba7 100644 --- a/test/Twig/Tests/Node/ForTest.php +++ b/test/Twig/Tests/Node/ForTest.php @@ -134,25 +134,15 @@ EOF '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; - \$context['loop']['revindex'] = \$length; - \$context['loop']['length'] = \$length; - \$context['loop']['last'] = 1 === \$length; -} foreach (\$context['_seq'] as \$context['k'] => \$context['v']) { - if (true) { - echo \$this->getContext(\$context, 'foo'); - ++\$context['loop']['index0']; - ++\$context['loop']['index']; - \$context['loop']['first'] = false; - if (isset(\$context['loop']['length'])) { - --\$context['loop']['revindex0']; - --\$context['loop']['revindex']; - \$context['loop']['last'] = 0 === \$context['loop']['revindex0']; - } + if (!(true)) { + continue; } + + echo \$this->getContext(\$context, 'foo'); + ++\$context['loop']['index0']; + ++\$context['loop']['index']; + \$context['loop']['first'] = false; } \$_parent = \$context['_parent']; unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']);