From affdc25f84b9315ab6505d56ccf1fcd2fd1574ad Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Tue, 23 Jul 2024 09:40:50 +0200 Subject: [PATCH] Add error for no constant --- src/Rules/Node/ValidConstantFunctionRule.php | 6 ++++++ .../ValidConstantFunction/ValidConstantFunctionRuleTest.php | 1 + .../ValidConstantFunctionRuleTest.twig | 1 + 3 files changed, 8 insertions(+) diff --git a/src/Rules/Node/ValidConstantFunctionRule.php b/src/Rules/Node/ValidConstantFunctionRule.php index 51354e54..785b3130 100644 --- a/src/Rules/Node/ValidConstantFunctionRule.php +++ b/src/Rules/Node/ValidConstantFunctionRule.php @@ -32,6 +32,12 @@ public function enterNode(Node $node, Environment $env): Node // Try for named parameters $argument = $arguments->getNode('constant'); } else { + $this->addError( + 'The first param of the function "constant()" is required.', + $node, + 'NoConstant' + ); + return $node; } if (!$argument instanceof ConstantExpression) { diff --git a/tests/Rules/Node/ValidConstantFunction/ValidConstantFunctionRuleTest.php b/tests/Rules/Node/ValidConstantFunction/ValidConstantFunctionRuleTest.php index 224e2b04..ac46f5a5 100644 --- a/tests/Rules/Node/ValidConstantFunction/ValidConstantFunctionRuleTest.php +++ b/tests/Rules/Node/ValidConstantFunction/ValidConstantFunctionRuleTest.php @@ -22,6 +22,7 @@ public function testRule(): void 'ValidConstantFunction.ClassConstant:20' => 'You cannot use the function "constant()" to resolve class names.', 'ValidConstantFunction.ClassConstant:21' => 'You cannot use the function "constant()" to resolve class names.', 'ValidConstantFunction.StringConstant:22' => 'The first param of the function "constant()" must be a string.', + 'ValidConstantFunction.NoConstant:24' => 'The first param of the function "constant()" is required.', ]); } } diff --git a/tests/Rules/Node/ValidConstantFunction/ValidConstantFunctionRuleTest.twig b/tests/Rules/Node/ValidConstantFunction/ValidConstantFunctionRuleTest.twig index f32ac652..14c0abdf 100644 --- a/tests/Rules/Node/ValidConstantFunction/ValidConstantFunctionRuleTest.twig +++ b/tests/Rules/Node/ValidConstantFunction/ValidConstantFunctionRuleTest.twig @@ -21,5 +21,6 @@ {{ constant(constant='ThisDoesNotExist::CLASS') }} {{ constant(constant=1) }} + {{ constant() }} {{ notConstant('ThisDoesNotExist::class') }} {% endblock %}