Skip to content

Delegate getNodes() in GetAttrExpressionDecorator (Twig 3.31) - #49

Open
kcirkovic wants to merge 1 commit into
netgen:masterfrom
kcirkovic:fix-twig-331-getnodes
Open

kcirkovic wants to merge 1 commit into
netgen:masterfrom
kcirkovic:fix-twig-331-getnodes

Conversation

@kcirkovic

Copy link
Copy Markdown

Problem

Twig 3.31.0 (2026-11-23) changed NodeTraverser to walk child nodes through the new Node::getNodes() instead of iterating the node:

foreach ($node->getNodes() as $k => $n) {

GetAttrExpressionDecorator does not call the parent constructor and delegates every Node method to the decorated GetAttrExpression, including getIterator(). getNodes() did not exist when the decorator was written, so it is not delegated. Twig's own implementation returns the decorator's $nodes, which is never initialized, and every template compilation fails:

Twig\Node\Node::getNodes(): Return value must be of type array, null returned
  at vendor/twig/twig/src/Node/Node.php:302
  Twig\NodeTraverser->traverseForVisitor() at vendor/twig/twig/src/NodeTraverser.php:64

We hit it on a Media Site based project (Ibexa 5.0.11, Site API 7.0.3) as soon as Composer pulled twig/twig 3.31.1: cache:clear fails while warming up the Twig cache, and so does every page.

Fix

Delegate getNodes() to the decorated expression, the same way getIterator() already is. On Twig below 3.31 nothing calls the method, so it changes nothing there.

Tests

GetAttrExpressionDecoratorTest is new. It covers:

  • getNodes() returns the nodes of the decorated expression (skipped on Twig below 3.31, where Node::getNodes() does not exist)
  • NodeTraverser visits the decorator and the nodes of the decorated expression without an error

I could not run the test suite locally. I checked the change end to end on the project instead: with Twig 3.31.1, cache:clear failed with the error above before the change and passed after it.

🤖 Generated with Claude Code

Twig 3.31 traverses nodes through Node::getNodes() instead of iterating
the node. The decorator delegated getIterator() but not the new method,
so getNodes() returned the decorator's own, never initialized $nodes and
compiling any template failed with: Twig\Node\Node::getNodes(): Return
value must be of type array, null returned.
@kcirkovic
kcirkovic requested a review from pspanja November 26, 2026 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant