Skip to content

Commit b8ff82d

Browse files
committed
feature #4769 Deprecate passing a non-AbstractExpression node to Parser::setParent() (fabpot)
This PR was merged into the 3.x branch. Discussion ---------- Deprecate passing a non-AbstractExpression node to Parser::setParent() Commits ------- 54d5c00 Deprecate passing a non-AbstractExpression node to Parser::setParent()
2 parents 0319c82 + 54d5c00 commit b8ff82d

File tree

4 files changed

+11
-1
lines changed

4 files changed

+11
-1
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# 3.24.0 (2026-XX-XX)
22

3+
* Deprecate passing a non-`AbstractExpression` node to `Parser::setParent()`
34
* Add support for renaming variables in object destructuring (`{name: userName} = user`)
45
* Add `html_attr_relaxed` escaping strategy that preserves :, @, [, and ] for front-end framework attribute names
56
* Add support for short-circuiting in null-safe operator chains

doc/deprecated.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ Parser
238238
* Passing ``null`` to ``Twig\Parser::setParent()`` is deprecated as of Twig
239239
3.12.
240240

241+
* Passing a non-``AbstractExpression`` node to ``Twig\Parser::setParent()`` is
242+
deprecated as of Twig 3.24; the method will require an ``AbstractExpression``
243+
instance in Twig 4.0.
244+
241245
* The ``Twig\Parser::getExpressionParser()`` method is deprecated as of Twig
242246
3.21, use ``Twig\Parser::parseExpression()`` instead.
243247

src/Parser.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,10 @@ public function setParent(?Node $parent): void
416416
trigger_deprecation('twig/twig', '3.12', 'Passing "null" to "%s()" is deprecated.', __METHOD__);
417417
}
418418

419+
if (null !== $parent && !$parent instanceof AbstractExpression) {
420+
trigger_deprecation('twig/twig', '3.24', 'Passing a "%s" instance to "%s()" is deprecated, pass an "AbstractExpression" instance instead.', $parent::class, __METHOD__);
421+
}
422+
419423
if (null !== $this->parent) {
420424
throw new SyntaxError('Multiple extends tags are forbidden.', $parent->getTemplateLine(), $parent->getSourceContext());
421425
}

tests/ParserTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Twig\Lexer;
2727
use Twig\Loader\ArrayLoader;
2828
use Twig\Node\EmptyNode;
29+
use Twig\Node\Expression\ConstantExpression;
2930
use Twig\Node\Node;
3031
use Twig\Node\Nodes;
3132
use Twig\Node\SetNode;
@@ -208,7 +209,7 @@ public function testImplicitMacroArgumentDefaultValues()
208209
protected function getParser()
209210
{
210211
$parser = new Parser(new Environment(new ArrayLoader()));
211-
$parser->setParent(new EmptyNode());
212+
$parser->setParent(new ConstantExpression('base.html', 1));
212213

213214
$p = new \ReflectionProperty($parser, 'stream');
214215
$p->setValue($parser, new TokenStream([], new Source('', '')));

0 commit comments

Comments
 (0)