bug #2702 Fix the error handling for the optimized extension-based function calls (stof)

This PR was merged into the 2.x branch.

Discussion
----------

Fix the error handling for the optimized extension-based function calls

Triggering a Twig_Error_Runtime at compile-time breaks the contract of the Twig environment, as such exception is for errors during the rendering.
This moves back the exception to a runtime one (same behavior than before the optimization).

Another option would be to replace this with a `Twig_Error_Syntax` instead (reporting the error earlier), but that would change the exception being thrown for such case.

Commits
-------

9928ae14 Fix the error handling for the optimized extension-based function calls
This commit is contained in:
Fabien Potencier
2018-06-07 07:44:00 +02:00
+5 -2
View File
@@ -31,10 +31,13 @@ abstract class Twig_Node_Expression_Call extends Twig_Node_Expression
// For BC/FC with namespaced aliases
$class = (new ReflectionClass(get_class($callable[0])))->name;
if (!$compiler->getEnvironment()->hasExtension($class)) {
throw new Twig_Error_Runtime(sprintf('The "%s" extension is not enabled.', $class));
// Compile a non-optimized call to trigger a Twig_Error_Runtime, which cannot be a compile-time error
$compiler->raw(sprintf('$this->env->getExtension(\'%s\')', $class));
} else {
$compiler->raw(sprintf('$this->extensions[\'%s\']', ltrim($class, '\\')));
}
$compiler->raw(sprintf('$this->extensions[\'%s\']->%s', ltrim($class, '\\'), $callable[1]));
$compiler->raw(sprintf('->%s', $callable[1]));
} else {
$closingParenthesis = true;
$compiler->raw(sprintf('call_user_func_array($this->env->get%s(\'%s\')->getCallable(), array', ucfirst($this->getAttribute('type')), $this->getAttribute('name')));