diff --git a/src/ExpressionParser.php b/src/ExpressionParser.php index 88de4cd35..fa480a07c 100644 --- a/src/ExpressionParser.php +++ b/src/ExpressionParser.php @@ -711,6 +711,8 @@ class ExpressionParser $arguments = null; if ($stream->test(Token::PUNCTUATION_TYPE, '(')) { $arguments = $this->parseArguments(true); + } elseif ($test->hasOneMandatoryArgument()) { + $arguments = new Node([0 => $this->parsePrimaryExpression()]); } return new $class($node, $name, $arguments, $this->parser->getCurrentToken()->getLine()); diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index 9f7d20022..91562b242 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -244,11 +244,11 @@ class CoreExtension extends AbstractExtension new TwigTest('odd', null, ['node_class' => '\Twig\Node\Expression\Test\OddTest']), new TwigTest('defined', null, ['node_class' => '\Twig\Node\Expression\Test\DefinedTest']), new TwigTest('sameas', null, ['node_class' => '\Twig\Node\Expression\Test\SameasTest', 'deprecated' => '1.21', 'alternative' => 'same as']), - new TwigTest('same as', null, ['node_class' => '\Twig\Node\Expression\Test\SameasTest']), + new TwigTest('same as', null, ['node_class' => '\Twig\Node\Expression\Test\SameasTest', 'one_mandatory_argument' => true]), new TwigTest('none', null, ['node_class' => '\Twig\Node\Expression\Test\NullTest']), new TwigTest('null', null, ['node_class' => '\Twig\Node\Expression\Test\NullTest']), new TwigTest('divisibleby', null, ['node_class' => '\Twig\Node\Expression\Test\DivisiblebyTest', 'deprecated' => '1.21', 'alternative' => 'divisible by']), - new TwigTest('divisible by', null, ['node_class' => '\Twig\Node\Expression\Test\DivisiblebyTest']), + new TwigTest('divisible by', null, ['node_class' => '\Twig\Node\Expression\Test\DivisiblebyTest', 'one_mandatory_argument' => true]), new TwigTest('constant', null, ['node_class' => '\Twig\Node\Expression\Test\ConstantTest']), new TwigTest('empty', 'twig_test_empty'), new TwigTest('iterable', 'twig_test_iterable'), diff --git a/src/TwigTest.php b/src/TwigTest.php index 5054965fe..962872d03 100644 --- a/src/TwigTest.php +++ b/src/TwigTest.php @@ -35,6 +35,7 @@ class TwigTest 'node_class' => '\Twig\Node\Expression\TestExpression', 'deprecated' => false, 'alternative' => null, + 'one_mandatory_argument' => false, ], $options); } @@ -82,6 +83,11 @@ class TwigTest { return $this->arguments; } + + public function hasOneMandatoryArgument(): bool + { + return (bool) $this->options['one_mandatory_argument']; + } } class_alias('Twig\TwigTest', 'Twig_SimpleTest'); diff --git a/tests/Extension/SandboxTest.php b/tests/Extension/SandboxTest.php index 79e65cace..8ec537f1b 100644 --- a/tests/Extension/SandboxTest.php +++ b/tests/Extension/SandboxTest.php @@ -179,8 +179,11 @@ class SandboxTest extends \PHPUnit\Framework\TestCase 'is_defined' => ['{{ obj.anotherFooObject is defined }}', '1'], 'is_null' => ['{{ obj is null }}', ''], 'is_sameas' => ['{{ obj is same as(obj) }}', '1'], + 'is_sameas_no_brackets' => ['{{ obj is same as obj }}', '1'], 'is_sameas_from_array' => ['{{ arr.obj is same as(arr.obj) }}', '1'], + 'is_sameas_from_array_no_brackets' => ['{{ arr.obj is same as arr.obj }}', '1'], 'is_sameas_from_another_method' => ['{{ obj.anotherFooObject is same as(obj.anotherFooObject) }}', ''], + 'is_sameas_from_another_method_no_brackets' => ['{{ obj.anotherFooObject is same as obj.anotherFooObject }}', ''], ]; } diff --git a/tests/Fixtures/expressions/divisibleby.test b/tests/Fixtures/expressions/divisibleby.test index f1bf5681e..fc5624a1e 100644 --- a/tests/Fixtures/expressions/divisibleby.test +++ b/tests/Fixtures/expressions/divisibleby.test @@ -2,8 +2,10 @@ Twig supports the "divisible by" operator --TEMPLATE-- {{ 8 is divisible by(2) ? 'OK' }} +{{ 8 is divisible by 2 ? 'OK' }} {{ 8 is not divisible by(3) ? 'OK' }} {{ 8 is divisible by (2) ? 'OK' }} +{{ 8 is divisible by 2 ? 'OK' }} {{ 8 is not divisible by @@ -15,3 +17,5 @@ OK OK OK OK +OK +OK diff --git a/tests/Fixtures/expressions/sameas.test b/tests/Fixtures/expressions/sameas.test index 83f3691f9..b4fdaa212 100644 --- a/tests/Fixtures/expressions/sameas.test +++ b/tests/Fixtures/expressions/sameas.test @@ -2,10 +2,14 @@ Twig supports the "same as" operator --TEMPLATE-- {{ 1 is same as(1) ? 'OK' }} +{{ 1 is same as 1 ? 'OK' }} {{ 1 is not same as(true) ? 'OK' }} +{{ 1 is not same as true ? 'OK' }} {{ 1 is same as(1) ? 'OK' }} {{ 1 is not same as(true) ? 'OK' }} {{ 1 is same as (1) ? 'OK' }} +{{ 1 is same as 1 ? 'OK' }} +{{ 1 is not same as '1' ? 'OK' }} {{ 1 is not same as @@ -19,3 +23,7 @@ OK OK OK OK +OK +OK +OK +OK