Skip to content

Commit f78209f

Browse files
authored
[CodeQuality] Skip @Final doc with public class constant on ConvertStaticToSelfRector (#7170)
* [CodeQuality] Skip @Final doc with public class constant on ConvertStaticToSelfRector * reduce else * update config
1 parent 1bcddc7 commit f78209f

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
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-tests/CodeQuality/Rector/Class_/ConvertStaticToSelfRector/config/configured_rule.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Rector\CodeQuality\Rector\Class_\ConvertStaticToSelfRector;
66
use Rector\Config\RectorConfig;
7+
use Rector\ValueObject\PhpVersionFeature;
78

89
return RectorConfig::configure()
9-
->withRules([ConvertStaticToSelfRector::class]);
10+
->withRules([ConvertStaticToSelfRector::class])
11+
->withPhpVersion(PhpVersionFeature::FINAL_CLASS_CONSTANTS);

rules/CodeQuality/Rector/Class_/ConvertStaticToSelfRector.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
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;
23+
use ReflectionClassConstant;
2124
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2225
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2326

@@ -31,6 +34,11 @@
3134
*/
3235
final class ConvertStaticToSelfRector extends AbstractRector
3336
{
37+
public function __construct(
38+
private readonly PhpVersionProvider $phpVersionProvider
39+
) {
40+
}
41+
3442
public function getRuleDefinition(): RuleDefinition
3543
{
3644
return new RuleDefinition('Change `static::*` to `self::*` on final class or private static members', [
@@ -163,10 +171,23 @@ private function shouldSkip(
163171
}
164172

165173
if (! $isFinal) {
166-
$memberIsFinal = $reflection instanceof ClassConstantReflection
167-
? $reflection->isFinal()
168-
: $reflection->isFinalByKeyword()
174+
// init
175+
$memberIsFinal = false;
176+
if ($reflection instanceof ClassConstantReflection) {
177+
// Get the native ReflectionClassConstant
178+
$declaringClass = $reflection->getDeclaringClass();
179+
$nativeReflectionClass = $declaringClass->getNativeReflection();
180+
$constantName = $reflection->getName();
181+
182+
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::FINAL_CLASS_CONSTANTS)) {
183+
// PHP 8.1+
184+
$nativeReflection = $nativeReflectionClass->getReflectionConstant($constantName);
185+
$memberIsFinal = $nativeReflection instanceof ReflectionClassConstant && $nativeReflection->isFinal();
186+
}
187+
} else {
188+
$memberIsFinal = $reflection->isFinalByKeyword()
169189
->yes();
190+
}
170191

171192
// Final native members can be safely converted
172193
if ($memberIsFinal) {

0 commit comments

Comments
 (0)