Skip to content

Commit

Permalink
Add compile-time checks for the "matches" operator
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Sep 21, 2024
1 parent 3f03d2b commit 04f1839
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/Node/Expression/Binary/MatchesBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Test/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ protected function doIntegrationTest($file, $message, $condition, $templates, $e
$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())));

Check failure on line 248 in src/Test/IntegrationTestCase.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

Failed asserting that string matches format description.

return;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Fixtures/expressions/matches.test
Original file line number Diff line number Diff line change
Expand Up @@ -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--
Expand All @@ -11,4 +13,6 @@ return []
OK
OK
OK
OK
OK
0
Original file line number Diff line number Diff line change
Expand Up @@ -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.
8 changes: 8 additions & 0 deletions tests/Fixtures/expressions/matches_error_runtime.test
Original file line number Diff line number Diff line change
@@ -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, backslash%s in "index.twig" at line 2

0 comments on commit 04f1839

Please sign in to comment.