feature #4689 Support two words test guard (VincentLanglet)

This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Support two words test guard

Extracted from https://github.com/twigphp/Twig/pull/4687

Commits
-------

30977bdea9 Support two words test guard
This commit is contained in:
Fabien Potencier
2025-09-15 07:51:44 +02:00
3 changed files with 25 additions and 2 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
# 3.22.0 (2025-XX-XX)
* n/a
* Add support for two words test in guard tag
# 3.21.1 (2025-05-03)
+7 -1
View File
@@ -32,9 +32,15 @@ final class GuardTokenParser extends AbstractTokenParser
$method = 'get'.$typeToken->getValue();
$nameToken = $stream->expect(Token::NAME_TYPE);
$name = $nameToken->getValue();
if ('test' === $typeToken->getValue() && $stream->test(Token::NAME_TYPE)) {
// try 2-words tests
$name .= ' '.$stream->getCurrent()->getValue();
$stream->next();
}
try {
$exists = null !== $this->parser->getEnvironment()->$method($nameToken->getValue());
$exists = null !== $this->parser->getEnvironment()->$method($name);
} catch (SyntaxError) {
$exists = false;
}
+17
View File
@@ -13,9 +13,26 @@
{% else %}
NEVER
{% endguard %}
{% guard test foobar %}
NEVER
{{ 'a'|foobar }}
{% else -%}
The foobar test doesn't exist
{% endguard %}
{% guard test divisible by -%}
The divisible by function does exist
{% else %}
NEVER
{% endguard %}
--DATA--
return []
--EXPECT--
The foobar filter doesn't exist
The constant function does exist
The foobar test doesn't exist
The divisible by function does exist