From f3c66c1ebf5dabe50d4f33f44b61955e6cf24db6 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 24 Aug 2025 08:35:17 +0700 Subject: [PATCH 1/3] [CodeQuality] Skip @final doc with public class constant on ConvertStaticToSelfRector --- .../skip_final_doc_class_constant.php.inc | 16 +++++++++++ .../Class_/ConvertStaticToSelfRector.php | 28 +++++++++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/Class_/ConvertStaticToSelfRector/Fixture/skip_final_doc_class_constant.php.inc diff --git a/rules-tests/CodeQuality/Rector/Class_/ConvertStaticToSelfRector/Fixture/skip_final_doc_class_constant.php.inc b/rules-tests/CodeQuality/Rector/Class_/ConvertStaticToSelfRector/Fixture/skip_final_doc_class_constant.php.inc new file mode 100644 index 00000000000..b99cf940eaa --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/ConvertStaticToSelfRector/Fixture/skip_final_doc_class_constant.php.inc @@ -0,0 +1,16 @@ +isFinal() - : $reflection->isFinalByKeyword() + if ($reflection instanceof ClassConstantReflection) { + // Get the native ReflectionClassConstant + $declaringClass = $reflection->getDeclaringClass(); + $nativeReflectionClass = $declaringClass->getNativeReflection(); + $constantName = $reflection->getName(); + + if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::FINAL_CLASS_CONSTANTS)) { + // PHP 8.1+ + $nativeReflection = $nativeReflectionClass->getReflectionConstant($constantName); + $memberIsFinal = $nativeReflection instanceof ReflectionClassConstant && $nativeReflection->isFinal(); + } else { + // On PHP < 8.1, class constants can't be final + $memberIsFinal = false; + } + } else { + $memberIsFinal = $reflection->isFinalByKeyword() ->yes(); + } // Final native members can be safely converted if ($memberIsFinal) { From 64c9ca9aef49da6b4dc68e3272eb48fd3543beac Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 24 Aug 2025 08:44:38 +0700 Subject: [PATCH 2/3] reduce else --- .../CodeQuality/Rector/Class_/ConvertStaticToSelfRector.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/rules/CodeQuality/Rector/Class_/ConvertStaticToSelfRector.php b/rules/CodeQuality/Rector/Class_/ConvertStaticToSelfRector.php index 6dca403a1ab..a83cf169ba0 100644 --- a/rules/CodeQuality/Rector/Class_/ConvertStaticToSelfRector.php +++ b/rules/CodeQuality/Rector/Class_/ConvertStaticToSelfRector.php @@ -171,6 +171,8 @@ private function shouldSkip( } if (! $isFinal) { + // init + $memberIsFinal = false; if ($reflection instanceof ClassConstantReflection) { // Get the native ReflectionClassConstant $declaringClass = $reflection->getDeclaringClass(); @@ -181,9 +183,6 @@ private function shouldSkip( // PHP 8.1+ $nativeReflection = $nativeReflectionClass->getReflectionConstant($constantName); $memberIsFinal = $nativeReflection instanceof ReflectionClassConstant && $nativeReflection->isFinal(); - } else { - // On PHP < 8.1, class constants can't be final - $memberIsFinal = false; } } else { $memberIsFinal = $reflection->isFinalByKeyword() From dfe77882d6b8efa0c4b9c3bda2e2939e3eb156e3 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 24 Aug 2025 08:46:19 +0700 Subject: [PATCH 3/3] update config --- .../ConvertStaticToSelfRector/config/configured_rule.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rules-tests/CodeQuality/Rector/Class_/ConvertStaticToSelfRector/config/configured_rule.php b/rules-tests/CodeQuality/Rector/Class_/ConvertStaticToSelfRector/config/configured_rule.php index 1e38609cc98..830716c9645 100644 --- a/rules-tests/CodeQuality/Rector/Class_/ConvertStaticToSelfRector/config/configured_rule.php +++ b/rules-tests/CodeQuality/Rector/Class_/ConvertStaticToSelfRector/config/configured_rule.php @@ -4,6 +4,8 @@ use Rector\CodeQuality\Rector\Class_\ConvertStaticToSelfRector; use Rector\Config\RectorConfig; +use Rector\ValueObject\PhpVersionFeature; return RectorConfig::configure() - ->withRules([ConvertStaticToSelfRector::class]); + ->withRules([ConvertStaticToSelfRector::class]) + ->withPhpVersion(PhpVersionFeature::FINAL_CLASS_CONSTANTS);