|
16 | 16 | use PHPStan\Reflection\ClassReflection; |
17 | 17 | use Rector\Configuration\Parameter\FeatureFlags; |
18 | 18 | use Rector\Enum\ObjectReference; |
| 19 | +use Rector\Php\PhpVersionProvider; |
19 | 20 | use Rector\PHPStan\ScopeFetcher; |
20 | 21 | use Rector\Rector\AbstractRector; |
| 22 | +use Rector\ValueObject\PhpVersionFeature; |
21 | 23 | use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
22 | 24 | use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
23 | 25 |
|
|
31 | 33 | */ |
32 | 34 | final class ConvertStaticToSelfRector extends AbstractRector |
33 | 35 | { |
| 36 | + public function __construct(private readonly PhpVersionProvider $phpVersionProvider) |
| 37 | + { |
| 38 | + } |
| 39 | + |
34 | 40 | public function getRuleDefinition(): RuleDefinition |
35 | 41 | { |
36 | 42 | return new RuleDefinition('Change `static::*` to `self::*` on final class or private static members', [ |
@@ -163,9 +169,25 @@ private function shouldSkip( |
163 | 169 | } |
164 | 170 |
|
165 | 171 | 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 | + } |
169 | 191 |
|
170 | 192 | // Final native members can be safely converted |
171 | 193 | if ($memberIsFinal) { |
|
0 commit comments