Remove deprecated macro calls without parentheses

This commit is contained in:
Fabien Potencier
2026-07-28 16:39:05 +02:00
parent 9b42c9bd3d
commit 096da67e7b
5 changed files with 24 additions and 17 deletions
+1
View File
@@ -9,6 +9,7 @@
* Add a fourth `array $tests` argument to `Twig\Sandbox\SecurityPolicyInterface::checkSecurity()`
* Throw a `SyntaxError` when an `extends`, `use`, or `macro` tag is not at the root of a template
* Throw a `SyntaxError` when a macro is defined more than once in a template
* Throw a `SyntaxError` when calling a macro without parentheses
# 4.0.0 alpha 1 (2026-05-17)
@@ -12,6 +12,7 @@
namespace Twig\Node\Expression;
use Twig\Compiler;
use Twig\Error\SyntaxError;
use Twig\Node\CoercesChildrenToStringInterface;
use Twig\Node\Expression\Variable\MacroVariable;
@@ -73,7 +74,7 @@ class MacroReferenceExpression extends AbstractExpression implements SupportDefi
public function compile(Compiler $compiler): void
{
if (!$this->hasParentheses && !$this->definedTest) {
trigger_deprecation('twig/twig', '3.29', 'Omitting parentheses when calling a macro is deprecated and will throw a SyntaxError in Twig 4.0; add parentheses after the macro name in "%s" at line %d.', $this->getTemplateName(), $this->getTemplateLine());
throw new SyntaxError('Omitting parentheses when calling a macro is not allowed; add parentheses after the macro name.', $this->getTemplateLine(), $this->getSourceContext());
}
if ($this->hasNode('name')) {
@@ -1,16 +0,0 @@
--TEST--
Omitting parentheses when calling macros is deprecated
--DEPRECATION--
Since twig/twig 3.29: Omitting parentheses when calling a macro is deprecated and will throw a SyntaxError in Twig 4.0; add parentheses after the macro name in "index.twig" at line 4.
Since twig/twig 3.29: Omitting parentheses when calling a macro is deprecated and will throw a SyntaxError in Twig 4.0; add parentheses after the macro name in "index.twig" at line 5.
--TEMPLATE--
{% import _self as macros %}
{% set name = 'hello' %}
{{ macros.hello }}
{{ _self.(name) }}
{% macro hello() %}Hello{% endmacro %}
--DATA--
return []
--EXPECT--
Hello
Hello
@@ -0,0 +1,10 @@
--TEST--
Calling a macro without parentheses is forbidden
--TEMPLATE--
{% import _self as macros %}
{{ macros.hello }}
{% macro hello() %}Hello{% endmacro %}
--DATA--
return []
--EXCEPTION--
Twig\Error\SyntaxError: Omitting parentheses when calling a macro is not allowed; add parentheses after the macro name in "index.twig" at line 3.
@@ -0,0 +1,11 @@
--TEST--
Calling a dynamic macro without parentheses is forbidden
--TEMPLATE--
{% import _self as macros %}
{% set name = 'hello' %}
{{ macros.(name) }}
{% macro hello() %}Hello{% endmacro %}
--DATA--
return []
--EXCEPTION--
Twig\Error\SyntaxError: Omitting parentheses when calling a macro is not allowed; add parentheses after the macro name in "index.twig" at line 4.