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

[CodeQuality] Allow transform static to self on final class on ConvertStaticPrivateConstantToSelfRector #6295

Merged
merged 1 commit into from
Sep 8, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassConstFetch\ConvertStaticPrivateConstantToSelfRector\Fixture;

final class OnFinalClass
{
protected const BAR = 1;

public const BAZ = 1;

public function run(): void
{
echo static::BAR;
echo static::BAZ;
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassConstFetch\ConvertStaticPrivateConstantToSelfRector\Fixture;

final class OnFinalClass
{
protected const BAR = 1;

public const BAZ = 1;

public function run(): void
{
echo self::BAR;
echo self::BAZ;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Rector\Tests\CodeQuality\Rector\ClassConstFetch\ConvertStaticPrivateConstantToSelfRector\Fixture;

final class SkipOtherVisibility
class SkipOtherVisibility
{
protected const BAR = 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ public function refactor(Node $node): ?Class_
return null;
}

if ($this->shouldBeSkipped($class, $node)) {
if (! $this->isUsingStatic($node)) {
return null;
}

if (! $class->isFinal() && ! $this->isPrivateConstant($node, $class)) {
return null;
}

Expand Down Expand Up @@ -120,15 +124,6 @@ private function isPrivateConstant(ClassConstFetch $classConstFetch, Class_ $cla
return false;
}

private function shouldBeSkipped(Class_ $class, ClassConstFetch $classConstFetch): bool
{
if (! $this->isUsingStatic($classConstFetch)) {
return true;
}

return ! $this->isPrivateConstant($classConstFetch, $class);
}

private function getConstantName(ClassConstFetch $classConstFetch): ?string
{
$constantNameIdentifier = $classConstFetch->name;
Expand Down