Skip to content

feat: support Twig 3 alongside Twig 2#49

Merged
LukeTowers merged 1 commit into
assetic-php:masterfrom
austinderrick:deps/twig-3-support
Jun 20, 2026
Merged

feat: support Twig 3 alongside Twig 2#49
LukeTowers merged 1 commit into
assetic-php:masterfrom
austinderrick:deps/twig-3-support

Conversation

@austinderrick

@austinderrick austinderrick commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Widens the twig/twig dev requirement from ^2.11 to ^2.11 || ^3.0 so Assetic's Twig integration is exercised against Twig 3, and resolves the incompatibilities and deprecations Twig 3 surfaces. All changes are backward compatible with Twig 2.x, so existing consumers on Twig 2 are unaffected.

This also removes the first-party reason CI saw deprecation noise on PHP 8.4/8.5: the Implicitly marking parameter … nullable notices came from the old Twig 2.x line, which is clean on Twig 3.

Changes

  • composer.jsontwig/twig: "^2.11 || ^3.0". Composer resolves the highest compatible version, so CI runs against Twig 3 on PHP 8.x while Twig 2 stays supported.
  • src/Assetic/Extension/Twig/AsseticTokenParser.php — Twig 3 lexes the opening [ of a vars=[...] attribute list as an operator token rather than punctuation. The parser now accepts either, so {% javascripts … vars=["foo","bar"] %} parses on both majors. (, and ] are punctuation in both and are unchanged.)
  • src/Assetic/Extension/Twig/AsseticNode.php — two Twig 3 deprecations in first-party code:
    • Twig 3.9 deprecated nodes that compile to echo instead of yield. AsseticNode only assigns to the context and subcompiles its body (it never emits echo), so it is yield-safe — marked with #[\Twig\Attribute\YieldReady]. The attribute is inert on Twig 2.x (never resolved) and on PHP 7.x (parses as a comment).
    • Twig 3.12 deprecated passing the node tag to the Node constructor (the Parser sets it automatically now). The tag is no longer forwarded on Twig ≥ 3.12, while Twig 2.x / < 3.12 still receive it.
  • tests/Assetic/Test/Extension/Twig/AsseticExtensionTest.php — the internal Environment::loadTemplate() signature changed in Twig 3; the test helper now uses the public load() method, which returns a renderable TemplateWrapper on both Twig 2.11+ and 3.x.

Verification

Ran the full suite with the symfony/phpunit-bridge deprecation helper enabled (the default) — no failures, no errors, and no remaining first-party deprecations — against both Twig versions:

# PHP 8.3 + twig/twig v3.27.1  →  exit 0
# PHP 8.3 + twig/twig v2.16.1  →  exit 0
OK, but incomplete, skipped, or risky tests!
Tests: 408, Assertions: 547, Skipped: 61.

(The earlier push failed CI because the bridge correctly flagged the two first-party Twig 3 deprecations above; both are now fixed at the source rather than suppressed.) phpcs (PSR-12) is clean on the changed files.

Notes

Independent of #48 (which adds 8.4/8.5 to the CI matrix and quiets the unfixable vendor deprecations); the two PRs touch disjoint files and can merge in any order. Dropping Twig 2 entirely (^3.0 only) was deliberately avoided to preserve backward compatibility, in keeping with Assetic's wide version support.

@austinderrick austinderrick requested a review from a team as a code owner June 13, 2026 03:04
Widens the twig/twig dev requirement to "^2.11 || ^3.0" so Assetic's Twig
integration is exercised against Twig 3, and resolves the incompatibilities
and deprecations Twig 3 surfaces. All changes remain backward compatible
with Twig 2.x.

- AsseticTokenParser: Twig 3 lexes the opening "[" of a vars=[...] attribute
  list as an operator token rather than punctuation. Accept either
  classification so the tag parses on both major versions.
- AsseticNode: mark the node #[YieldReady] (Twig 3.9 deprecated nodes that
  still compile to echo instead of yield; this node only assigns to the
  context and subcompiles its body, so it is yield-safe), and stop passing
  the node tag to the parent Node constructor on Twig >= 3.12, which
  deprecated that argument (the Parser now sets it automatically). The
  attribute is inert on Twig 2.x and on PHP 7.x, where it parses as a comment.
- AsseticExtensionTest: the internal Environment::loadTemplate() signature
  changed in Twig 3; switch the test helper to the public load() method,
  which returns a renderable TemplateWrapper on both Twig 2.11+ and 3.x.

Verified with the symfony/phpunit-bridge deprecation helper enabled: the
full suite passes with no failures and no remaining first-party deprecations
on PHP 8.3 against both twig/twig v3.27.1 and v2.16.1.
@LukeTowers LukeTowers merged commit 2e26287 into assetic-php:master Jun 20, 2026
7 checks passed
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.

2 participants