Twig: Wrong argument for twig_matches

After upgrading to TWIG v3.5.0 (coming from v3.4.3) I get a strange error and as my knowledge of the TWIG codebase is limited, I’m not able to get whats the fault.

Error: twig_matches(): Argument #2 ($str) must be of type string, null given

Calling template part:

{% if not result is empty %}
    {% include 'my-template.html.twig' with {"result":result} %}
{% endif %}

result is an array passed to a sub template. This works in v3.4.3 but not in v3.5.0

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 15 (8 by maintainers)

Commits related to this issue

Most upvoted comments

BC break fixed in #3809

I also ran into this BC-breaking change.

Just to be super clear, here’s the trivial reproduce case:

{{ null matches '' }}

As pointed out by uwej711, it’s easy to fix, but unexpected. Given that Twig is usually very lenient, I think this should be changed.

This was introduced in #3687 to guard against invalid regex patterns.

@stof I’d be happy to create a PR with

- function twig_matches(string $regexp, string $str)
+ function twig_matches(string $regexp, ?string $str)

What do you think?