Skip to content

Commit d3a0717

Browse files
committed
minor #4330 Add compile-time checks for the "matches" operator (fabpot)
This PR was merged into the 3.x branch. Discussion ---------- Add compile-time checks for the "matches" operator Commits ------- d1beac4 Add compile-time checks for the "matches" operator
2 parents 653735f + d1beac4 commit d3a0717

5 files changed

Lines changed: 32 additions & 2 deletions

File tree

src/Node/Expression/Binary/MatchesBinary.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,27 @@
1212
namespace Twig\Node\Expression\Binary;
1313

1414
use Twig\Compiler;
15+
use Twig\Error\SyntaxError;
16+
use Twig\Node\Node;
17+
use Twig\Node\Expression\ConstantExpression;
1518

1619
class MatchesBinary extends AbstractBinary
1720
{
21+
public function __construct(Node $left, Node $right, int $lineno)
22+
{
23+
if ($right instanceof ConstantExpression) {
24+
$regexp = $right->getAttribute('value');
25+
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));
26+
try {
27+
preg_match($regexp, '');
28+
} finally {
29+
restore_error_handler();
30+
}
31+
}
32+
33+
parent::__construct($left, $right, $lineno);
34+
}
35+
1836
public function compile(Compiler $compiler): void
1937
{
2038
$compiler

src/Test/IntegrationTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ protected function doIntegrationTest($file, $message, $condition, $templates, $e
245245
$output = trim($template->render(eval($match[1].';')), "\n ");
246246
} catch (\Exception $e) {
247247
if (false !== $exception) {
248-
$this->assertSame(trim($exception), trim(\sprintf('%s: %s', \get_class($e), $e->getMessage())));
248+
$this->assertStringMatchesFormat(trim($exception), trim(\sprintf('%s: %s', \get_class($e), $e->getMessage())));
249249

250250
return;
251251
}

tests/Fixtures/expressions/matches.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
Twig supports the "matches" operator
33
--TEMPLATE--
44
{{ 'foo' matches '/o/' ? 'OK' : 'KO' }}
5+
{{ 'foo' matches '/o/'|lower ? 'OK' : 'KO' }}
56
{{ 'foo' matches '/^fo/' ? 'OK' : 'KO' }}
7+
{{ 'foo' matches '/^' ~ 'fo/' ? 'OK' : 'KO' }}
68
{{ 'foo' matches '/O/i' ? 'OK' : 'KO' }}
79
{{ null matches '/o/' }}
810
--DATA--
@@ -11,4 +13,6 @@ return []
1113
OK
1214
OK
1315
OK
16+
OK
17+
OK
1418
0

tests/Fixtures/expressions/matches_error.test renamed to tests/Fixtures/expressions/matches_error_compilation.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ Twig supports the "matches" operator with a great error message
55
--DATA--
66
return []
77
--EXCEPTION--
8-
Twig\Error\RuntimeError: Regexp "/o" passed to "matches" is not valid: No ending delimiter '/' found in "index.twig" at line 2
8+
Twig\Error\SyntaxError: Regexp "/o" passed to "matches" is not valid: No ending delimiter '/' found in "index.twig" at line 2.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--TEST--
2+
Twig supports the "matches" operator with a great error message
3+
--TEMPLATE--
4+
{{ 'foo' matches 1 + 2 }}
5+
--DATA--
6+
return []
7+
--EXCEPTION--
8+
Twig\Error\RuntimeError: Regexp "3" passed to "matches" is not valid: Delimiter must not be alphanumeric%sbackslash%sin "index.twig" at line 2

0 commit comments

Comments
 (0)