Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for ValidConstantFunctionRule #282

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/Rules/Node/ValidConstantFunctionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,28 @@ public function enterNode(Node $node, Environment $env): Node
}

$arguments = $node->getNode('arguments');
if (1 !== $arguments->count()) {
return $node;
$argument = $arguments->hasNode('0') ? $arguments->getNode('0') : null;
// Try for named parameters
if (null === $argument && $arguments->hasNode('constant')) {
$argument = $arguments->getNode('constant');
}

$argument = $arguments->getNode('0');
if (!$argument instanceof ConstantExpression) {
return $node;
}

$constant = $argument->getAttribute('value');
if (!\is_string($constant)) {
$this->addError(
'The first param of the function "constant()" must be a string.',
$node,
'StringConstant'
);

return $node;
}

// The object to get the constant from cannot be resolved statically.
if (1 !== $arguments->count()) {
return $node;
}

Expand All @@ -46,7 +57,7 @@ public function enterNode(Node $node, Environment $env): Node

if ('::class' === strtolower(substr($constant, -7))) {
$this->addError(
sprintf('You cannot use the Twig function "constant()" to access "%s". You could provide an object and call constant("class", $object) or use the class name directly as a string.', $constant),
Copy link
Owner Author

@VincentLanglet VincentLanglet Jul 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take any suggestion here @ruudk but I'd like to avoid a too long error message.

So far I always had short message like "Variable should be called foo" or "Expected 2 spaces, found 1". This gave report with all the error messages written on single line every time and I'm not happy with the fact that this error would be certainly written with multiples lines.

Maybe letter something like an additional help text could be written, but so far I'd like a short message.
I try 'You cannot use the function "constant()" to resolve class names.' but take any suggestion

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'You cannot use the function "constant()" to resolve class names.',
$node,
'ClassConstant'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ public function testRule(): void
{
$this->checkRule(new ValidConstantFunctionRule(), [
'ValidConstantFunction.ConstantUndefined:7' => 'Constant "ThisDoesNotExist::SomeKey" is undefined.',
'ValidConstantFunction.ClassConstant:9' => 'You cannot use the Twig function "constant()" to access "ThisDoesNotExist::class". You could provide an object and call constant("class", $object) or use the class name directly as a string.',
'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.',
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,17 @@
{{ constant('ThisDoesNotExist::SomeKey') }}
{{ constant(someVar) }}
{{ constant('ThisDoesNotExist::class') }}
{{ constant(1) }}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ruudk you wrote in your initial PR bffffcb

        $constant = $argument->getAttribute('value');
        if (!\is_string($constant)) {
            return $node;
        }

Is there valid case when the value is not a string or should we display an error for this case ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this because the return type was mixed according to psalm or PHPStan. Shouldn't be the case I think.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So maybe beter to report an error in that case.


{{ constant(constant='DateTimeInterface::ATOM') }}
{{ constant(constant='DATE_ATOM') }}
{{ constant(constant='TwigCsFixer\\Tests\\Rules\\Node\\ValidConstantFunction\\ValidConstantFunctionRuleTest::SOME_CONSTANT') }}
{{ constant(constant='class', someObject) }}

{{ constant(constant='ThisDoesNotExist::SomeKey') }}
{{ constant(constant=someVar) }}
{{ constant(constant='ThisDoesNotExist::class') }}
{{ constant(constant=1) }}

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