diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index d6ff1ce97..65caab31f 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -310,7 +310,6 @@ namespace { use Twig\Environment; use Twig\Error\LoaderError; use Twig\Error\RuntimeError; - use Twig\Error\SyntaxError; use Twig\Extension\CoreExtension; use Twig\Extension\SandboxExtension; use Twig\Markup; @@ -1020,18 +1019,18 @@ function twig_compare($a, $b) return $a <=> $b; } - /** - * @param string $pattern - * @param string $subject - * - * @return int - * - * @throws SyntaxError When an invalid pattern is used - */ +/** + * @param string $pattern + * @param string $subject + * + * @return int + * + * @throws RuntimeError When an invalid pattern is used + */ function twig_matches(string $regexp, string $str) { set_error_handler(function ($t, $m) use ($regexp) { - throw new SyntaxError(sprintf('Regexp "%s" passed to "matches" is not valid', $regexp).substr($m, 12)); + throw new RuntimeError(sprintf('Regexp "%s" passed to "matches" is not valid', $regexp).substr($m, 12)); }); try { return preg_match($regexp, $str); diff --git a/tests/Fixtures/expressions/matches_error.test b/tests/Fixtures/expressions/matches_error.test new file mode 100644 index 000000000..1220eb422 --- /dev/null +++ b/tests/Fixtures/expressions/matches_error.test @@ -0,0 +1,8 @@ +--TEST-- +Twig supports the "matches" operator with a great error message +--TEMPLATE-- +{{ 'foo' matches '/o' }} +--DATA-- +return [] +--EXCEPTION-- +Twig\Error\RuntimeError: Regexp "/o" passed to "matches" is not valid: No ending delimiter '/' found in "index.twig" at line 2