-
Notifications
You must be signed in to change notification settings - Fork 504
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
Implement MagicClassConstantRule #2741
Conversation
d610eb2
to
0b186a6
Compare
conf/config.level0.neon
Outdated
@@ -56,6 +56,7 @@ rules: | |||
- PHPStan\Rules\Classes\NonClassAttributeClassRule | |||
- PHPStan\Rules\Classes\TraitAttributeClassRule | |||
- PHPStan\Rules\Constants\FinalConstantRule | |||
- PHPStan\Rules\Constants\MagicClassConstantRule |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be enabled only with bleeding edge. The code does not error, it just returns an empty string AFAIK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have used a feature flag with a more general name, so I can use it for similar rules for __FUNCTION__
and __METHOD__
|
||
return [ | ||
RuleErrorBuilder::message( | ||
sprintf('Magic constant %s cannot be used outside a class.', $node->getName()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The message should be different. It just returns an empty string.
Can you also please go through other magic constants and report them when used in a wrong way? Maybe it can all be done inside this single rule. |
5ef5af0
to
92c94d4
Compare
there is one interessting leftover case: // test.php
function doFooParams(
string $file = __FILE__,
int $line = __LINE__,
string $class = __CLASS__,
string $dir = __DIR__,
string $namespace = __NAMESPACE__,
string $method = __METHOD__,
string $function = __FUNCTION__,
string $trait = __TRAIT__
): void
{
} since used in the parameter-list and not the function body it false-positives with
|
we could fix the above case by moving phpstan-src/src/Analyser/NodeScopeResolver.php Lines 451 to 459 in 231ed3e
wdyt? |
909ab5d
to
842d371
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of changing NodeScopeResolver I went with adding a dedicated new NodeVisitor to prevent the false positives
8e518b0
to
85473c8
Compare
if ($scope->getNamespace() === null) { | ||
return [ | ||
RuleErrorBuilder::message( | ||
sprintf('Magic constant %s is always empty when used in global namespace.', $node->getName()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please deleted "when used" from all messages. They'll still make sense.
|
||
return [ | ||
RuleErrorBuilder::message( | ||
sprintf('Magic constant %s is always empty when used outside a trait-using-class.', $node->getName()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"outside a trait"
c1172f6
to
9191a31
Compare
(btw: interessting that issuebot did not realize that the snippet from issue phpstan/phpstan#10099 gets a new error with this PR - locally it works as expected though) |
)->build(), | ||
]; | ||
} elseif ($node instanceof MagicConst\Method || $node instanceof MagicConst\Function_) { | ||
// https://3v4l.org/3CAHm |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem right. What about private $foo = __METHOD__;
?
Also a false positive here - you can have an anonymous function outside a class/function and it's going to report '{closure}'
.
9191a31
to
4ff7c84
Compare
4ff7c84
to
5e4d878
Compare
Thank you. |
closes phpstan/phpstan#10099
background: https://www.php.net/manual/en/language.constants.magic.php