refactored code
authorFabien Potencier <fabien.potencier@gmail.com>
Tue, 1 Nov 2011 10:54:32 +0000 (11:54 +0100)
committerFabien Potencier <fabien.potencier@gmail.com>
Tue, 1 Nov 2011 10:56:04 +0000 (11:56 +0100)
lib/Twig/ExpressionParser.php
lib/Twig/Extension/Core.php

index af96e98..33cc0dc 100644 (file)
@@ -259,12 +259,7 @@ class Twig_ExpressionParser
                     return new Twig_Node_Expression_GetAttr($alias['node'], new Twig_Node_Expression_Constant($alias['name'], $line), $args, Twig_TemplateInterface::METHOD_CALL, $line);
                 }
 
-                $functionMap = $this->parser->getEnvironment()->getFunctions();
-                if (isset($functionMap[$name]) && $functionMap[$name] instanceof Twig_Filter_Node) {
-                    $class = $functionMap[$name]->getClass();
-                } else {
-                    $class = 'Twig_Node_Expression_Function';
-                }
+                $class = $this->getFunctionNodeClass($name);
 
                 return new $class($name, $args, $line);
         }
@@ -325,12 +320,7 @@ class Twig_ExpressionParser
                 $arguments = $this->parseArguments();
             }
 
-            $filterMap = $this->parser->getEnvironment()->getFilters();
-            if (isset($filterMap[$name->getAttribute('value')]) && $filterMap[$name->getAttribute('value')] instanceof Twig_Filter_Node) {
-                $class = $filterMap[$name->getAttribute('value')]->getClass();
-            } else {
-                $class = 'Twig_Node_Expression_Filter';
-            }
+            $class = $this->getFilterNodeClass($name->getAttribute('value'));
 
             $node = new $class($node, $name, $arguments, $token->getLine(), $tag);
 
@@ -393,4 +383,24 @@ class Twig_ExpressionParser
 
         return new Twig_Node($targets);
     }
+
+    protected function getFunctionNodeClass($name)
+    {
+        $functionMap = $this->parser->getEnvironment()->getFunctions();
+        if (isset($functionMap[$name]) && $functionMap[$name] instanceof Twig_Filter_Node) {
+            return $functionMap[$name]->getClass();
+        }
+
+        return 'Twig_Node_Expression_Function';
+    }
+
+    protected function getFilterNodeClass($name)
+    {
+        $filterMap = $this->parser->getEnvironment()->getFilters();
+        if (isset($filterMap[$name]) && $filterMap[$name] instanceof Twig_Filter_Node) {
+            return $filterMap[$name]->getClass();
+        }
+
+        return 'Twig_Node_Expression_Filter';
+    }
 }
index 5e5fdee..465818d 100644 (file)
@@ -178,14 +178,19 @@ class Twig_Extension_Core extends Twig_Extension
             $arguments = $parser->getExpressionParser()->parseArguments();
         }
 
-        $testMap = $parser->getEnvironment()->getTests();
+        $class = $this->getTestNodeClass($parser->getEnvironment(), $name);
+
+        return new $class($node, $name, $arguments, $parser->getCurrentToken()->getLine());
+    }
+
+    protected function getTestNodeClass(Twig_Environment $env, $name)
+    {
+        $testMap = $env->getTests();
         if (isset($testMap[$name]) && $testMap[$name] instanceof Twig_Test_Node) {
-            $class = $testMap[$name]->getClass();
-        } else {
-            $class = 'Twig_Node_Expression_Test';
+            return $testMap[$name]->getClass();
         }
 
-        return new $class($node, $name, $arguments, $parser->getCurrentToken()->getLine());
+        return 'Twig_Node_Expression_Test';
     }
 
     /**