From 8a37332def642cbcd5c31ce34b671092ee905c59 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 27 Aug 2026 13:08:19 +0200 Subject: [PATCH] Fix duplicate macro deprecation wording --- src/Parser.php | 2 +- .../macros/duplicate_definition.legacy.test | 2 +- tests/ParserTest.php | 16 +++++++++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Parser.php b/src/Parser.php index fbee6ed7b..849c0fde3 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -309,7 +309,7 @@ class Parser public function setMacro(string $name, MacroNode $node): void { if (isset($this->macros[$name])) { - trigger_deprecation('twig/twig', '3.29', 'Defining the macro "%s" more than once in "%s" is deprecated and will throw a SyntaxError in Twig 4.0 (first definition at line %d, second at line %d). The last definition is used in Twig 3.', $name, $this->stream->getSourceContext()->getName(), $this->macros[$name]->getTemplateLine(), $node->getTemplateLine()); + trigger_deprecation('twig/twig', '3.29', 'Defining the macro "%s" more than once in "%s" is deprecated and will throw a SyntaxError in Twig 4.0 (previous definition at line %d, new definition at line %d). The last definition is used in Twig 3.', $name, $this->stream->getSourceContext()->getName(), $this->macros[$name]->getTemplateLine(), $node->getTemplateLine()); } $this->macros[$name] = $node; diff --git a/tests/Fixtures/macros/duplicate_definition.legacy.test b/tests/Fixtures/macros/duplicate_definition.legacy.test index a0fb17000..f0ce38b60 100644 --- a/tests/Fixtures/macros/duplicate_definition.legacy.test +++ b/tests/Fixtures/macros/duplicate_definition.legacy.test @@ -1,7 +1,7 @@ --TEST-- Defining a macro more than once is deprecated --DEPRECATION-- -Since twig/twig 3.29: Defining the macro "greet" more than once in "index.twig" is deprecated and will throw a SyntaxError in Twig 4.0 (first definition at line 3, second at line 4). The last definition is used in Twig 3. +Since twig/twig 3.29: Defining the macro "greet" more than once in "index.twig" is deprecated and will throw a SyntaxError in Twig 4.0 (previous definition at line 3, new definition at line 4). The last definition is used in Twig 3. --TEMPLATE-- {% import _self as macros %} {% macro greet() %}first{% endmacro %} diff --git a/tests/ParserTest.php b/tests/ParserTest.php index f90fa46c5..e4a13f4d4 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -453,7 +453,7 @@ TWIG, 'index'))); { $twig = new Environment(new ArrayLoader()); - $this->expectDeprecation('Since twig/twig 3.29: Defining the macro "input" more than once in "index" is deprecated and will throw a SyntaxError in Twig 4.0 (first definition at line 1, second at line 1). The last definition is used in Twig 3.'); + $this->expectDeprecation('Since twig/twig 3.29: Defining the macro "input" more than once in "index" is deprecated and will throw a SyntaxError in Twig 4.0 (previous definition at line 1, new definition at line 1). The last definition is used in Twig 3.'); $module = $twig->parse($twig->tokenize(new Source('{## First #}{% macro input() %}{% endmacro %}{## Second #}{% macro input() %}{% endmacro %}', 'index'))); @@ -463,6 +463,20 @@ TWIG, 'index'))); } } + /** + * @group legacy + */ + #[Group('legacy')] + public function testDuplicateMacroDeprecationsReferToPreviousDefinition(): void + { + $twig = new Environment(new ArrayLoader()); + + $this->expectDeprecation('Since twig/twig 3.29: Defining the macro "input" more than once in "index" is deprecated and will throw a SyntaxError in Twig 4.0 (previous definition at line 1, new definition at line 2). The last definition is used in Twig 3.'); + $this->expectDeprecation('Since twig/twig 3.29: Defining the macro "input" more than once in "index" is deprecated and will throw a SyntaxError in Twig 4.0 (previous definition at line 2, new definition at line 3). The last definition is used in Twig 3.'); + + $twig->parse($twig->tokenize(new Source("{% macro input() %}{% endmacro %}\n{% macro input() %}{% endmacro %}\n{% macro input() %}{% endmacro %}", 'index'))); + } + public function testBodyForParentTemplates(): void { $twig = new Environment(new ArrayLoader());