From 61102373cad56cfd9efca54ff6f5defd5d435b94 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 25 Nov 2026 14:48:06 +0100 Subject: [PATCH] [Php55] Handle own class method prefix concat in StringClassNameToClassConstantRector --- .../self_method_reference_concat.php.inc | 27 ++++++ .../self_method_reference_in_closure.php.inc | 27 ++++++ .../skip_method_reference_concat.php.inc | 17 ++++ ...ethod_reference_in_anonymous_class.php.inc | 26 ++++++ .../skip_method_reference_in_trait.php.inc | 6 +- .../StringClassNameToClassConstantRector.php | 83 ++++++++++++++++--- 6 files changed, 173 insertions(+), 13 deletions(-) create mode 100644 rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/self_method_reference_concat.php.inc create mode 100644 rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/self_method_reference_in_closure.php.inc create mode 100644 rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/skip_method_reference_concat.php.inc create mode 100644 rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/skip_method_reference_in_anonymous_class.php.inc diff --git a/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/self_method_reference_concat.php.inc b/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/self_method_reference_concat.php.inc new file mode 100644 index 00000000000..d8a7957a556 --- /dev/null +++ b/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/self_method_reference_concat.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/self_method_reference_in_closure.php.inc b/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/self_method_reference_in_closure.php.inc new file mode 100644 index 00000000000..473e14fa8e3 --- /dev/null +++ b/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/self_method_reference_in_closure.php.inc @@ -0,0 +1,27 @@ + 'Rector\Tests\Php55\Rector\String_\StringClassNameToClassConstantRector\Fixture\SelfMethodReferenceInClosure::indexAction'; + } +} + +?> +----- + self::class . '::indexAction'; + } +} + +?> diff --git a/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/skip_method_reference_concat.php.inc b/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/skip_method_reference_concat.php.inc new file mode 100644 index 00000000000..a30e189a19b --- /dev/null +++ b/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/skip_method_reference_concat.php.inc @@ -0,0 +1,17 @@ + diff --git a/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/skip_method_reference_in_anonymous_class.php.inc b/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/skip_method_reference_in_anonymous_class.php.inc new file mode 100644 index 00000000000..8d737d758df --- /dev/null +++ b/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/skip_method_reference_in_anonymous_class.php.inc @@ -0,0 +1,26 @@ +name(), + ]; + } + + private function name(): string + { + return 'indexAction'; + } + }; + } +} + +?> diff --git a/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/skip_method_reference_in_trait.php.inc b/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/skip_method_reference_in_trait.php.inc index cd75baabc0e..3255698afd1 100644 --- a/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/skip_method_reference_in_trait.php.inc +++ b/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/skip_method_reference_in_trait.php.inc @@ -6,7 +6,11 @@ trait SkipMethodReferenceInTrait { public function run() { - return 'Rector\Tests\Php55\Rector\String_\StringClassNameToClassConstantRector\Fixture\SkipMethodReferenceInTrait::run'; + return [ + 'Rector\Tests\Php55\Rector\String_\StringClassNameToClassConstantRector\Fixture\SkipMethodReferenceInTrait::run', + '\Rector\Tests\Php55\Rector\String_\StringClassNameToClassConstantRector\Fixture\SkipMethodReferenceInTrait::run', + 'Rector\Tests\Php55\Rector\String_\StringClassNameToClassConstantRector\Fixture\SkipMethodReferenceInTrait::' . $this->name(), + ]; } } diff --git a/rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php b/rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php index fdfb6a2ac38..45199934487 100644 --- a/rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php +++ b/rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php @@ -17,6 +17,7 @@ use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; +use Rector\RuleDoc\CodeSample\CodeSample; use Rector\RuleDoc\CodeSample\ConfiguredCodeSample; use Rector\RuleDoc\RuleDefinition; use Rector\ValueObject\PhpVersion; @@ -73,6 +74,31 @@ public function run() , ['ClassName', 'AnotherClassName'], ), + new CodeSample( + <<<'CODE_SAMPLE' +namespace App; + +class SomeController +{ + public function run() + { + return 'App\SomeController::indexAction'; + } +} +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +namespace App; + +class SomeController +{ + public function run() + { + return self::class . '::indexAction'; + } +} +CODE_SAMPLE + ), ]); } @@ -81,14 +107,18 @@ public function run() */ public function getNodeTypes(): array { - return [String_::class]; + return [String_::class, Concat::class]; } /** - * @param String_ $node + * @param String_|Concat $node */ public function refactor(Node $node): ClassConstFetch|Concat|null { + if ($node instanceof Concat) { + return $this->refactorSelfMethodPrefix($node); + } + if ($this->shouldSkipIsA($node)) { return null; } @@ -176,28 +206,57 @@ private function refactorSelfMethodReference(String_ $string): ?Concat return null; } - $classReflection = $this->reflectionResolver->resolveClassReflection($string); - if (! $classReflection instanceof ClassReflection) { + if (! $this->isSelfClassName($string, $className)) { return null; } - // self::class inside a trait is the using class, not the trait - if ($classReflection->isAnonymous() || $classReflection->isTrait()) { + return $this->createSelfClassConcat('::' . $methodName); + } + + /** + * 'App\SomeClass::' . $method inside App\SomeClass => self::class . '::' . $method + */ + private function refactorSelfMethodPrefix(Concat $concat): ?Concat + { + if (! $concat->left instanceof String_) { return null; } - if ($classReflection->getName() !== $className) { + $value = ltrim($concat->left->value, '\\'); + if (! str_ends_with($value, '::') || substr_count($value, '::') !== 1) { return null; } - if ($this->shouldSkip($className)) { + if (! $this->isSelfClassName($concat->left, substr($value, 0, -2))) { return null; } - return new Concat( - new ClassConstFetch(new Name(ObjectReference::SELF), 'class'), - new String_('::' . $methodName) - ); + $concat->left = $this->createSelfClassConcat('::'); + return $concat; + } + + private function isSelfClassName(String_ $string, string $className): bool + { + $classReflection = $this->reflectionResolver->resolveClassReflection($string); + if (! $classReflection instanceof ClassReflection) { + return false; + } + + // self::class inside a trait is the using class, not the trait + if ($classReflection->isAnonymous() || $classReflection->isTrait()) { + return false; + } + + if ($classReflection->getName() !== $className) { + return false; + } + + return ! $this->shouldSkip($className); + } + + private function createSelfClassConcat(string $suffix): Concat + { + return new Concat(new ClassConstFetch(new Name(ObjectReference::SELF), 'class'), new String_($suffix)); } private function shouldSkipIsA(String_ $string): bool