added an optimization when the same temporary variable would have been created twice
authorFabien Potencier <fabien.potencier@gmail.com>
Sun, 16 Oct 2011 20:18:02 +0000 (22:18 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Sun, 16 Oct 2011 20:18:02 +0000 (22:18 +0200)
lib/Twig/NodeVisitor/Optimizer.php

index 196c5d7..c9c2f8d 100644 (file)
@@ -107,7 +107,17 @@ class Twig_NodeVisitor_Optimizer implements Twig_NodeVisitorInterface
     protected function optimizeVariables($node, $env)
     {
         if ('Twig_Node_Expression_Name' === get_class($node) && $node->isSimple()) {
-            $this->prependedNodes[0][] = new Twig_Node_SetTemp($node->getAttribute('name'), $node->getLine());
+            // only add it if it's not already there
+            $exists = false;
+            foreach ($this->prependedNodes[0] as $n) {
+                if ($n->getAttribute('name') === $node->getAttribute('name')) {
+                    $exists = true;
+                    break;
+                }
+            }
+            if (!$exists) {
+                $this->prependedNodes[0][] = new Twig_Node_SetTemp($node->getAttribute('name'), $node->getLine());
+            }
 
             return new Twig_Node_Expression_TempName($node->getAttribute('name'), $node->getLine());
         }