Skip to content

Commit

Permalink
removed some loop variable when using an if statement in a for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Sep 1, 2011
1 parent 39774a1 commit 9c2065f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 41 deletions.
54 changes: 30 additions & 24 deletions lib/Twig/Node/For.php
Expand Up @@ -51,16 +51,21 @@ public function compile(Twig_Compiler $compiler)
->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
Expand All @@ -74,10 +79,13 @@ public function compile(Twig_Compiler $compiler)

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")
;
}

Expand All @@ -92,21 +100,19 @@ public function compile(Twig_Compiler $compiler)
->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
Expand Down
24 changes: 7 additions & 17 deletions test/Twig/Tests/Node/ForTest.php
Expand Up @@ -134,25 +134,15 @@ public function getTests()
'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']);
Expand Down

0 comments on commit 9c2065f

Please sign in to comment.