removed some loop variable when using an if statement in a for loop
authorFabien Potencier <fabien.potencier@gmail.com>
Thu, 1 Sep 2011 08:40:40 +0000 (10:40 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Thu, 1 Sep 2011 08:41:03 +0000 (10:41 +0200)
lib/Twig/Node/For.php
test/Twig/Tests/Node/ForTest.php

index 6c3cebc..eb204e2 100644 (file)
@@ -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
index 098a11f..8841ba7 100644 (file)
@@ -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']);