Skip to content

Commit 9025616

Browse files
committed
[CodeQuality] Skip @Final doc with public class constant on ConvertStaticToSelfRector
1 parent e95966d commit 9025616

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Class_\ConvertStaticToSelfRector\Fixture;
4+
5+
class SkipFinalDocClassConstant
6+
{
7+
/**
8+
* @final
9+
*/
10+
public const BAR = 1;
11+
12+
public function run()
13+
{
14+
static::BAR;
15+
}
16+
}

rules/CodeQuality/Rector/Class_/ConvertStaticToSelfRector.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
use PHPStan\Reflection\ClassReflection;
1717
use Rector\Configuration\Parameter\FeatureFlags;
1818
use Rector\Enum\ObjectReference;
19+
use Rector\Php\PhpVersionProvider;
1920
use Rector\PHPStan\ScopeFetcher;
2021
use Rector\Rector\AbstractRector;
22+
use Rector\ValueObject\PhpVersionFeature;
2123
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2224
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2325

@@ -31,6 +33,10 @@
3133
*/
3234
final class ConvertStaticToSelfRector extends AbstractRector
3335
{
36+
public function __construct(private readonly PhpVersionProvider $phpVersionProvider)
37+
{
38+
}
39+
3440
public function getRuleDefinition(): RuleDefinition
3541
{
3642
return new RuleDefinition('Change `static::*` to `self::*` on final class or private static members', [
@@ -163,9 +169,25 @@ private function shouldSkip(
163169
}
164170

165171
if (! $isFinal) {
166-
$memberIsFinal = $reflection instanceof ClassConstantReflection
167-
? $reflection->isFinal()
168-
: $reflection->isFinalByKeyword()->yes();
172+
if ($reflection instanceof ClassConstantReflection) {
173+
// Get the native ReflectionClassConstant
174+
$declaringClass = $reflection->getDeclaringClass();
175+
$nativeReflectionClass = $declaringClass->getNativeReflection();
176+
$constantName = $reflection->getName();
177+
178+
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::FINAL_CLASS_CONSTANTS)) {
179+
// PHP 8.1+
180+
$nativeReflection = $nativeReflectionClass->getReflectionConstant($constantName);
181+
$memberIsFinal = $nativeReflection instanceof \ReflectionClassConstant
182+
? $nativeReflection->isFinal()
183+
: false;
184+
} else {
185+
// On PHP < 8.1, class constants can't be final
186+
$memberIsFinal = false;
187+
}
188+
} else {
189+
$memberIsFinal = $reflection->isFinalByKeyword()->yes();
190+
}
169191

170192
// Final native members can be safely converted
171193
if ($memberIsFinal) {

0 commit comments

Comments
 (0)