From f880bab988129314378a32a3c5aaba1ae9c41ce4 Mon Sep 17 00:00:00 2001 From: Jonathan Ingram Date: Mon, 5 Dec 2011 11:01:25 +1100 Subject: [PATCH] Help the developer when they specify an invalid filter by providing some alternatives --- lib/Twig/Node/Expression/Filter.php | 17 ++++++++++++++++- 1 files changed, 16 insertions(+), 1 deletions(-) diff --git a/lib/Twig/Node/Expression/Filter.php b/lib/Twig/Node/Expression/Filter.php index bff1a67..7f3cc5b 100644 --- a/lib/Twig/Node/Expression/Filter.php +++ b/lib/Twig/Node/Expression/Filter.php @@ -19,8 +19,23 @@ class Twig_Node_Expression_Filter extends Twig_Node_Expression public function compile(Twig_Compiler $compiler) { $name = $this->getNode('filter')->getAttribute('value'); + if (false === $filter = $compiler->getEnvironment()->getFilter($name)) { - throw new Twig_Error_Syntax(sprintf('The filter "%s" does not exist', $name), $this->getLine()); + $alternativeFilters = array(); + + foreach ($compiler->getEnvironment()->getFilters() as $filterName => $filter) { + if (false !== strpos($filterName, $name)) { + $alternativeFilters[] = $filterName; + } + } + + $exceptionMessage = sprintf('The filter "%s" does not exist', $name); + + if (count($alternativeFilters)) { + $exceptionMessage = sprintf('%s. Did you mean "%s"?', $exceptionMessage, implode('", "', $alternativeFilters)); + } + + throw new Twig_Error_Syntax($exceptionMessage, $this->getLine()); } $this->compileFilter($compiler, $filter); -- 1.7.2.5