Skip to content

Fix Markup truthiness in boolean expressions#4841

Merged
fabpot merged 1 commit into
twigphp:3.xfrom
xtrime-ru:fix-trim
Jun 13, 2026
Merged

Fix Markup truthiness in boolean expressions#4841
fabpot merged 1 commit into
twigphp:3.xfrom
xtrime-ru:fix-trim

Conversation

@xtrime-ru

@xtrime-ru xtrime-ru commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes Markup truthiness handling in boolean expressions.

trim can return a Twig\Markup instance for safe strings. Empty Markup objects must behave like empty strings in Twig truth tests, but PHP treats all objects as truthy. This caused expressions like this to incorrectly evaluate as true:

{% set x %}    {% endset %}
{% if x|trim and x|trim %}fail{% else %}ok{% endif %}

This case was working properly in https://github.com/twigphp/Twig/releases/tag/v3.14.2 and earlier.

Related commits

Changes

  • Added TrueTest::wrap() to centralize wrapping non-primitive expressions with Twig’s Markup-aware true test.
  • Reused TrueTest::wrap() in:
    • IfNode
    • conditional ternary expressions
    • and, or, and xor binary expressions
    • Elvis expressions
    • unary not
  • Added regression coverage for boolean operators (and, or, xor, not) and ternary/Elvis expressions whose operands evaluate to empty Markup.

Tests

./vendor/bin/simple-phpunit tests/IntegrationTest.php --filter markup_test

@fabpot

fabpot commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Thank you @xtrime-ru.

@fabpot fabpot merged commit 2f202b0 into twigphp:3.x Jun 13, 2026
39 of 43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants