diff --git a/src/Node/Expression/Binary/MatchesBinary.php b/src/Node/Expression/Binary/MatchesBinary.php index 4669044e0..ba25313c0 100644 --- a/src/Node/Expression/Binary/MatchesBinary.php +++ b/src/Node/Expression/Binary/MatchesBinary.php @@ -12,9 +12,27 @@ namespace Twig\Node\Expression\Binary; use Twig\Compiler; +use Twig\Error\SyntaxError; +use Twig\Node\Node; +use Twig\Node\Expression\ConstantExpression; class MatchesBinary extends AbstractBinary { + public function __construct(Node $left, Node $right, int $lineno) + { + if ($right instanceof ConstantExpression) { + $regexp = $right->getAttribute('value'); + set_error_handler(static fn ($t, $m) => throw new SyntaxError(\sprintf('Regexp "%s" passed to "matches" is not valid: %s.', $regexp, substr($m, 14)), $lineno)); + try { + preg_match($regexp, ''); + } finally { + restore_error_handler(); + } + } + + parent::__construct($left, $right, $lineno); + } + public function compile(Compiler $compiler): void { $compiler diff --git a/src/Test/IntegrationTestCase.php b/src/Test/IntegrationTestCase.php index 67c168090..b66c17fd2 100644 --- a/src/Test/IntegrationTestCase.php +++ b/src/Test/IntegrationTestCase.php @@ -245,7 +245,7 @@ abstract class IntegrationTestCase extends TestCase $output = trim($template->render(eval($match[1].';')), "\n "); } catch (\Exception $e) { if (false !== $exception) { - $this->assertSame(trim($exception), trim(\sprintf('%s: %s', \get_class($e), $e->getMessage()))); + $this->assertStringMatchesFormat(trim($exception), trim(\sprintf('%s: %s', \get_class($e), $e->getMessage()))); return; } diff --git a/tests/Fixtures/expressions/matches.test b/tests/Fixtures/expressions/matches.test index 8f5e3669e..00a5702b4 100644 --- a/tests/Fixtures/expressions/matches.test +++ b/tests/Fixtures/expressions/matches.test @@ -2,7 +2,9 @@ Twig supports the "matches" operator --TEMPLATE-- {{ 'foo' matches '/o/' ? 'OK' : 'KO' }} +{{ 'foo' matches '/o/'|lower ? 'OK' : 'KO' }} {{ 'foo' matches '/^fo/' ? 'OK' : 'KO' }} +{{ 'foo' matches '/^' ~ 'fo/' ? 'OK' : 'KO' }} {{ 'foo' matches '/O/i' ? 'OK' : 'KO' }} {{ null matches '/o/' }} --DATA-- @@ -11,4 +13,6 @@ return [] OK OK OK +OK +OK 0 diff --git a/tests/Fixtures/expressions/matches_error.test b/tests/Fixtures/expressions/matches_error_compilation.test similarity index 53% rename from tests/Fixtures/expressions/matches_error.test rename to tests/Fixtures/expressions/matches_error_compilation.test index 1220eb422..c251be134 100644 --- a/tests/Fixtures/expressions/matches_error.test +++ b/tests/Fixtures/expressions/matches_error_compilation.test @@ -5,4 +5,4 @@ Twig supports the "matches" operator with a great error message --DATA-- return [] --EXCEPTION-- -Twig\Error\RuntimeError: Regexp "/o" passed to "matches" is not valid: No ending delimiter '/' found in "index.twig" at line 2 +Twig\Error\SyntaxError: Regexp "/o" passed to "matches" is not valid: No ending delimiter '/' found in "index.twig" at line 2. diff --git a/tests/Fixtures/expressions/matches_error_runtime.test b/tests/Fixtures/expressions/matches_error_runtime.test new file mode 100644 index 000000000..4a2bb5943 --- /dev/null +++ b/tests/Fixtures/expressions/matches_error_runtime.test @@ -0,0 +1,8 @@ +--TEST-- +Twig supports the "matches" operator with a great error message +--TEMPLATE-- +{{ 'foo' matches 1 + 2 }} +--DATA-- +return [] +--EXCEPTION-- +Twig\Error\RuntimeError: Regexp "3" passed to "matches" is not valid: Delimiter must not be alphanumeric%sbackslash%sin "index.twig" at line 2