Skip to content

Commit 10a7bc1

Browse files
committed
bug #3809 Restores the leniency of the matches twig comparison (la5942)
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.
2 parents b3bdf6a + f136668 commit 10a7bc1

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

src/Extension/CoreExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,19 +1021,19 @@ function twig_compare($a, $b)
10211021

10221022
/**
10231023
* @param string $pattern
1024-
* @param string $subject
1024+
* @param string|null $subject
10251025
*
10261026
* @return int
10271027
*
10281028
* @throws RuntimeError When an invalid pattern is used
10291029
*/
1030-
function twig_matches(string $regexp, string $str)
1030+
function twig_matches(string $regexp, ?string $str)
10311031
{
10321032
set_error_handler(function ($t, $m) use ($regexp) {
10331033
throw new RuntimeError(sprintf('Regexp "%s" passed to "matches" is not valid', $regexp).substr($m, 12));
10341034
});
10351035
try {
1036-
return preg_match($regexp, $str);
1036+
return preg_match($regexp, $str ?? '');
10371037
} finally {
10381038
restore_error_handler();
10391039
}

tests/Fixtures/expressions/matches.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ Twig supports the "matches" operator
44
{{ 'foo' matches '/o/' ? 'OK' : 'KO' }}
55
{{ 'foo' matches '/^fo/' ? 'OK' : 'KO' }}
66
{{ 'foo' matches '/O/i' ? 'OK' : 'KO' }}
7+
{{ null matches '/o/' }}
78
--DATA--
89
return []
910
--EXPECT--
1011
OK
1112
OK
1213
OK
14+
0

0 commit comments

Comments
 (0)