Skip to content

Commit e0fbc7e

Browse files
ivantseppfabpot
authored andcommitted
[ExpressionLanguage] Fix matches to handle booleans being used as regexp
1 parent 4e70834 commit e0fbc7e

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Symfony/Component/ExpressionLanguage/Node/BinaryNode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public function compile(Compiler $compiler): void
5252
if ('matches' == $operator) {
5353
if ($this->nodes['right'] instanceof ConstantNode) {
5454
$this->evaluateMatches($this->nodes['right']->evaluate([], []), '');
55+
} elseif ($this->nodes['right'] instanceof self && '~' !== $this->nodes['right']->attributes['operator']) {
56+
throw new SyntaxError('The regex passed to "matches" must be a string.');
5557
}
5658

5759
$compiler

src/Symfony/Component/ExpressionLanguage/Tests/Node/BinaryNodeTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,27 @@ public function testCompileMatchesWithInvalidRegexpAsExpression()
221221
eval('$regexp = "this is not a regexp"; '.$compiler->getSource().';');
222222
}
223223

224+
public function testCompileMatchesWithBooleanBinaryNode()
225+
{
226+
$binaryNode = new BinaryNode('||', new ConstantNode(true), new ConstantNode(false));
227+
$node = new BinaryNode('matches', new ConstantNode('abc'), $binaryNode);
228+
229+
$this->expectException(SyntaxError::class);
230+
$this->expectExceptionMessage('The regex passed to "matches" must be a string');
231+
$compiler = new Compiler([]);
232+
$node->compile($compiler);
233+
}
234+
235+
public function testCompileMatchesWithStringBinaryNode()
236+
{
237+
$binaryNode = new BinaryNode('~', new ConstantNode('a'), new ConstantNode('b'));
238+
$node = new BinaryNode('matches', new ConstantNode('abc'), $binaryNode);
239+
240+
$compiler = new Compiler([]);
241+
$node->compile($compiler);
242+
$this->expectNotToPerformAssertions();
243+
}
244+
224245
public function testDivisionByZero()
225246
{
226247
$node = new BinaryNode('/', new ConstantNode(1), new ConstantNode(0));

0 commit comments

Comments
 (0)