Skip to content

Commit

Permalink
bug #3809 Restores the leniency of the matches twig comparison (la5…
Browse files Browse the repository at this point in the history
…942)

This PR was merged into the 3.x branch.

Discussion
----------

Restores the leniency of the `matches` twig comparison

Restores the leniency of the `matches` twig comparison, allowing null subject to result in a non-match.

Resolves BC break introduced in PR #3687 ref in #3801 (comment)

As per pattern in #3617

Commits
-------

f136668 Restores the leniency of the `matches` twig comparison, allowing null subject to result in a non-match.
  • Loading branch information
fabpot committed Feb 8, 2023
2 parents b3bdf6a + f136668 commit 10a7bc1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Extension/CoreExtension.php
Expand Up @@ -1021,19 +1021,19 @@ function twig_compare($a, $b)

/**
* @param string $pattern
* @param string $subject
* @param string|null $subject
*
* @return int
*
* @throws RuntimeError When an invalid pattern is used
*/
function twig_matches(string $regexp, string $str)
function twig_matches(string $regexp, ?string $str)
{
set_error_handler(function ($t, $m) use ($regexp) {
throw new RuntimeError(sprintf('Regexp "%s" passed to "matches" is not valid', $regexp).substr($m, 12));
});
try {
return preg_match($regexp, $str);
return preg_match($regexp, $str ?? '');
} finally {
restore_error_handler();
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Fixtures/expressions/matches.test
Expand Up @@ -4,9 +4,11 @@ Twig supports the "matches" operator
{{ 'foo' matches '/o/' ? 'OK' : 'KO' }}
{{ 'foo' matches '/^fo/' ? 'OK' : 'KO' }}
{{ 'foo' matches '/O/i' ? 'OK' : 'KO' }}
{{ null matches '/o/' }}
--DATA--
return []
--EXPECT--
OK
OK
OK
0

0 comments on commit 10a7bc1

Please sign in to comment.