From f136668933b075a7bbb2d0197355ae65613a386a Mon Sep 17 00:00:00 2001 From: Jacob Richardson Date: Tue, 7 Feb 2023 17:06:12 +0000 Subject: [PATCH] Restores the leniency of the `matches` twig comparison, allowing null subject to result in a non-match. Resolves BC break introduced in PR https://github.com/twigphp/Twig/pull/3687 As per pattern in https://github.com/twigphp/Twig/pull/3617 --- src/Extension/CoreExtension.php | 6 +++--- tests/Fixtures/expressions/matches.test | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index ce846e91f7..f99adda451 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -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(); } diff --git a/tests/Fixtures/expressions/matches.test b/tests/Fixtures/expressions/matches.test index 95459c3b0f..8f5e3669e6 100644 --- a/tests/Fixtures/expressions/matches.test +++ b/tests/Fixtures/expressions/matches.test @@ -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