mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-13 10:56:38 +00:00
Help the developer when they specify an invalid function by providing some alternatives
This commit is contained in:
committed by
Fabien Potencier
parent
55c7ff00b1
commit
2c79f494f5
@@ -17,9 +17,26 @@ class Twig_Node_Expression_Function extends Twig_Node_Expression
|
||||
|
||||
public function compile(Twig_Compiler $compiler)
|
||||
{
|
||||
$function = $compiler->getEnvironment()->getFunction($this->getAttribute('name'));
|
||||
$name = $this->getAttribute('name');
|
||||
|
||||
$function = $compiler->getEnvironment()->getFunction($name);
|
||||
|
||||
if (false === $function) {
|
||||
throw new Twig_Error_Syntax(sprintf('The function "%s" does not exist', $this->getAttribute('name')), $this->getLine());
|
||||
$alternativeFunctions = array();
|
||||
|
||||
foreach ($compiler->getEnvironment()->getFunctions() as $functionName => $function) {
|
||||
if (false !== strpos($functionName, $name)) {
|
||||
$alternativeFunctions[] = $functionName;
|
||||
}
|
||||
}
|
||||
|
||||
$exceptionMessage = sprintf('The function "%s" does not exist', $name);
|
||||
|
||||
if (count($alternativeFunctions)) {
|
||||
$exceptionMessage = sprintf('%s. Did you mean "%s"?', $exceptionMessage, implode('", "', $alternativeFunctions));
|
||||
}
|
||||
|
||||
throw new Twig_Error_Syntax($exceptionMessage, $this->getLine());
|
||||
}
|
||||
|
||||
$compiler
|
||||
|
||||
Reference in New Issue
Block a user