Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support for ignoring syntax erros in an undefined handler in guard #4575

Merged
merged 1 commit into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/ExpressionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,17 @@ private function getTest(int $line): TwigTest

private function getFunction(string $name, int $line): TwigFunction
{
if (!$function = $this->env->getFunction($name)) {
try {
$function = $this->env->getFunction($name);
} catch (SyntaxError $e) {
if (!$this->parser->shouldIgnoreUnknownTwigCallables()) {
throw $e;
}

$function = null;
}

if (!$function) {
if ($this->parser->shouldIgnoreUnknownTwigCallables()) {
return new TwigFunction($name, fn () => '');
}
Expand All @@ -819,7 +829,16 @@ private function getFunction(string $name, int $line): TwigFunction

private function getFilter(string $name, int $line): TwigFilter
{
if (!$filter = $this->env->getFilter($name)) {
try {
$filter = $this->env->getFilter($name);
} catch (SyntaxError $e) {
if (!$this->parser->shouldIgnoreUnknownTwigCallables()) {
throw $e;
}

$filter = null;
}
if (!$filter) {
if ($this->parser->shouldIgnoreUnknownTwigCallables()) {
return new TwigFilter($name, fn () => '');
}
Expand Down
22 changes: 22 additions & 0 deletions tests/Fixtures/tags/guard/throwing_handler.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
"guard" creates a compilation time condition on Twig callables availability
--TEMPLATE--
{% guard filter throwing_undefined_filter %}
NEVER
{{ 'a'|throwing_undefined_filter }}
{% else -%}
The throwing_undefined_filter filter doesn't exist
{% endguard %}

{% guard function throwing_undefined_function -%}
NEVER
{{ throwing_undefined_function() }}
{% else -%}
The throwing_undefined_function function doesn't exist
{% endguard %}
--DATA--
return []
--EXPECT--
The throwing_undefined_filter filter doesn't exist

The throwing_undefined_function function doesn't exist
27 changes: 27 additions & 0 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/

use Twig\DeprecatedCallableInfo;
use Twig\Error\SyntaxError;
use Twig\Extension\AbstractExtension;
use Twig\Extension\DebugExtension;
use Twig\Extension\SandboxExtension;
Expand Down Expand Up @@ -49,6 +50,32 @@ public function getExtensions()
];
}

protected function getUndefinedFunctionCallbacks(): array
{
return [
static function (string $name) {
if ('throwing_undefined_function' === $name) {
throw new SyntaxError('This function is undefined in the tests.');
}

return false;
},
];
}

protected function getUndefinedTokenParserCallbacks(): array
{
return [
static function (string $name) {
if ('throwing_undefined_filter' === $name) {
throw new SyntaxError('This filter is undefined in the tests.');
}

return false;
},
];
}

protected static function getFixturesDirectory(): string
{
return __DIR__.'/Fixtures/';
Expand Down