mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-17 21:07:18 +00:00
Add compile-time checks for the "matches" operator
This commit is contained in:
@@ -12,9 +12,27 @@
|
|||||||
namespace Twig\Node\Expression\Binary;
|
namespace Twig\Node\Expression\Binary;
|
||||||
|
|
||||||
use Twig\Compiler;
|
use Twig\Compiler;
|
||||||
|
use Twig\Error\SyntaxError;
|
||||||
|
use Twig\Node\Node;
|
||||||
|
use Twig\Node\Expression\ConstantExpression;
|
||||||
|
|
||||||
class MatchesBinary extends AbstractBinary
|
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
|
public function compile(Compiler $compiler): void
|
||||||
{
|
{
|
||||||
$compiler
|
$compiler
|
||||||
|
|||||||
@@ -245,7 +245,7 @@ abstract class IntegrationTestCase extends TestCase
|
|||||||
$output = trim($template->render(eval($match[1].';')), "\n ");
|
$output = trim($template->render(eval($match[1].';')), "\n ");
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
if (false !== $exception) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,9 @@
|
|||||||
Twig supports the "matches" operator
|
Twig supports the "matches" operator
|
||||||
--TEMPLATE--
|
--TEMPLATE--
|
||||||
{{ 'foo' matches '/o/' ? 'OK' : 'KO' }}
|
{{ 'foo' matches '/o/' ? 'OK' : 'KO' }}
|
||||||
|
{{ 'foo' matches '/o/'|lower ? 'OK' : 'KO' }}
|
||||||
{{ 'foo' matches '/^fo/' ? 'OK' : 'KO' }}
|
{{ 'foo' matches '/^fo/' ? 'OK' : 'KO' }}
|
||||||
|
{{ 'foo' matches '/^' ~ 'fo/' ? 'OK' : 'KO' }}
|
||||||
{{ 'foo' matches '/O/i' ? 'OK' : 'KO' }}
|
{{ 'foo' matches '/O/i' ? 'OK' : 'KO' }}
|
||||||
{{ null matches '/o/' }}
|
{{ null matches '/o/' }}
|
||||||
--DATA--
|
--DATA--
|
||||||
@@ -11,4 +13,6 @@ return []
|
|||||||
OK
|
OK
|
||||||
OK
|
OK
|
||||||
OK
|
OK
|
||||||
|
OK
|
||||||
|
OK
|
||||||
0
|
0
|
||||||
|
|||||||
+1
-1
@@ -5,4 +5,4 @@ Twig supports the "matches" operator with a great error message
|
|||||||
--DATA--
|
--DATA--
|
||||||
return []
|
return []
|
||||||
--EXCEPTION--
|
--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.
|
||||||
@@ -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
|
||||||
Reference in New Issue
Block a user