Skip to content

Commit

Permalink
Add error for missing param in ValidConstantFunctionRule (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored Jul 23, 2024
1 parent ae7fc45 commit 2d821c0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
15 changes: 12 additions & 3 deletions src/Rules/Node/ValidConstantFunctionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,19 @@ public function enterNode(Node $node, Environment $env): Node
}

$arguments = $node->getNode('arguments');
$argument = $arguments->hasNode('0') ? $arguments->getNode('0') : null;
// Try for named parameters
if (null === $argument && $arguments->hasNode('constant')) {
if ($arguments->hasNode('0')) {
$argument = $arguments->getNode('0');
} elseif ($arguments->hasNode('constant')) {
// 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) {
return $node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ public function testRule(): void
$this->checkRule(new ValidConstantFunctionRule(), [
'ValidConstantFunction.ConstantUndefined:7' => 'Constant "ThisDoesNotExist::SomeKey" is undefined.',
'ValidConstantFunction.ClassConstant:9' => 'You cannot use the function "constant()" to resolve class names.',
'ValidConstantFunction.StringConstant:10' => 'The first param of the function "constant()" must be a string.',
'ValidConstantFunction.ConstantUndefined:17' => 'Constant "ThisDoesNotExist::SomeKey" is undefined.',
'ValidConstantFunction.ClassConstant:19' => 'You cannot use the function "constant()" to resolve class names.',
'ValidConstantFunction.StringConstant:20' => 'The first param of the function "constant()" must be a string.',
'ValidConstantFunction.ClassConstant:10' => 'You cannot use the function "constant()" to resolve class names.',
'ValidConstantFunction.StringConstant:11' => 'The first param of the function "constant()" must be a string.',
'ValidConstantFunction.ConstantUndefined:18' => 'Constant "ThisDoesNotExist::SomeKey" is undefined.',
'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.',
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{{ constant('ThisDoesNotExist::SomeKey') }}
{{ constant(someVar) }}
{{ constant('ThisDoesNotExist::class') }}
{{ constant('ThisDoesNotExist::CLASS') }}
{{ constant(1) }}

{{ constant(constant='DateTimeInterface::ATOM') }}
Expand All @@ -17,7 +18,9 @@
{{ constant(constant='ThisDoesNotExist::SomeKey') }}
{{ constant(constant=someVar) }}
{{ constant(constant='ThisDoesNotExist::class') }}
{{ constant(constant='ThisDoesNotExist::CLASS') }}
{{ constant(constant=1) }}

{{ constant() }}
{{ notConstant('ThisDoesNotExist::class') }}
{% endblock %}

0 comments on commit 2d821c0

Please sign in to comment.