-
-
Notifications
You must be signed in to change notification settings - Fork 430
[CodeQuality] Replace static with self for private constants #2332
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
[CodeQuality] Replace static with self for private constants #2332
Conversation
| $classReflection = $scope->getClassReflection(); | ||
| if (! $classReflection instanceof ClassReflection) { | ||
| return false; | ||
| } |
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 only apply when it used inside final class, ref https://3v4l.org/jYOjL vs https://3v4l.org/LWrtJ which result is different.
You can verify inside class with final with:
| } | |
| } | |
| if (! $classReflection->isClass()) { | |
| return false; | |
| } | |
| if (! $classReflection->isFinal()) { | |
| return false; | |
| } |
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.
Also, you can get ClassReflection via ReflectionResolver->resolveClassReflection() ref
| public function resolveClassReflection(?Node $node): ?ClassReflection |
You can inject ReflectionResolver into constructor:
public function __construct(
private readonly \Rector\Core\Reflection\ReflectionResolver $reflectionResolver,
) {
}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.
see my comment #2332 (comment) for cover non-final class if it going to be included to avoid constant redefinition
| [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| class Foo { |
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.
| class Foo { | |
| final class Foo { |
| CODE_SAMPLE | ||
| , | ||
| <<<'CODE_SAMPLE' | ||
| class Foo { |
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.
| class Foo { | |
| final class Foo { |
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition( | ||
| 'Replaces static::* access to private constants with self::*', |
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.
| 'Replaces static::* access to private constants with self::*', | |
| 'Replaces static::* access to private constants with self::* on final class', |
|
For non-final class that extendable, it seems you may looking for child of classes, eg: loop and verify: foreach ($childrenClassReflections as $childrenClassReflection) {
if ($childrenClassReflection->hasConstant($constantName)) {
return false;
}
} |
|
please add test for skip extended class that has the same constant base on #2332 (comment) |
|
After some thinking, it can be dangerous that may cause a BC break if the class is not final as extendable can be anywere. This should be configurable if need to be applied, with:
|
|
I am closing it as it can cause BC break without various check like my comments before. Feel free to reopen PR when you have a chance to polish it. Thank you. |
I wasn't able to find a rule like this and we use this in our project, have a look at the problem here: