diff --git a/rules-tests/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector/Fixture/skip_magic_calls.php.inc b/rules-tests/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector/Fixture/skip_magic_calls.php.inc new file mode 100644 index 00000000000..ef788a8c307 --- /dev/null +++ b/rules-tests/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector/Fixture/skip_magic_calls.php.inc @@ -0,0 +1,13 @@ + +----- + diff --git a/rules-tests/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector/Fixture/usage_of_constant.php.inc b/rules-tests/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector/Fixture/usage_of_constant.php.inc index c2ff24449a8..34e6402bd12 100644 --- a/rules-tests/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector/Fixture/usage_of_constant.php.inc +++ b/rules-tests/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector/Fixture/usage_of_constant.php.inc @@ -24,7 +24,7 @@ final class UsageOfConstant { public function run($value) { - $compare = \Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Source\SomeEnum::VALUE; + $compare = \Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Source\SomeEnum::VALUE->getKey(); } } diff --git a/rules-tests/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector/Fixture/usage_of_constant_value.php.inc b/rules-tests/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector/Fixture/usage_of_constant_value.php.inc new file mode 100644 index 00000000000..ced49dd1539 --- /dev/null +++ b/rules-tests/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector/Fixture/usage_of_constant_value.php.inc @@ -0,0 +1,31 @@ +getValue(); + } +} + +?> +----- +getValue(); + } +} + +?> diff --git a/rules-tests/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector/Source/SomeEnum.php b/rules-tests/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector/Source/SomeEnum.php index 2f1c4e16617..e522e75f022 100644 --- a/rules-tests/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector/Source/SomeEnum.php +++ b/rules-tests/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector/Source/SomeEnum.php @@ -7,9 +7,9 @@ use MyCLabs\Enum\Enum; /** - * @method SomeEnum VALUE() + * @method SomeEnum USED_TO_BE_CONST() */ final class SomeEnum extends Enum { - const VALUE = 'value'; + const USED_TO_BE_CONST = 'value'; } diff --git a/rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php b/rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php index 5493a3ccf2c..640bacbaded 100644 --- a/rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php +++ b/rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php @@ -5,7 +5,10 @@ namespace Rector\Php81\Rector\MethodCall; use PhpParser\Node; +use PhpParser\Node\Expr; +use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\MethodCall; +use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\StaticCall; use PHPStan\Type\ObjectType; use Rector\Core\Rector\AbstractRector; @@ -43,27 +46,51 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [MethodCall::class]; + return [MethodCall::class, StaticCall::class]; } /** - * @param MethodCall $node + * @param MethodCall|StaticCall $node */ public function refactor(Node $node): ?Node { - if (! $this->isObjectType($node->var, new ObjectType('MyCLabs\Enum\Enum'))) { + if ($node->name instanceof Expr) { return null; } - if (! $this->isName($node->name, 'getKey')) { + $enumCaseName = $this->getName($node->name); + if ($enumCaseName === null) { + return null; + } + + if ($node instanceof MethodCall) { + return $this->refactorMethodCall($node, $enumCaseName); + } + + if (! $this->isObjectType($node->class, new ObjectType('MyCLabs\Enum\Enum'))) { return null; } - if (! $node->var instanceof StaticCall) { + $className = $this->getName($node->class); + if (! is_string($className)) { + return null; + } + + return $this->nodeFactory->createClassConstFetch($className, $enumCaseName); + } + + public function provideMinPhpVersion(): int + { + return PhpVersionFeature::ENUM; + } + + private function refactorGetKeyMethodCall(MethodCall $methodCall): ?ClassConstFetch + { + if (! $methodCall->var instanceof StaticCall) { return null; } - $staticCall = $node->var; + $staticCall = $methodCall->var; $className = $this->getName($staticCall->class); if ($className === null) { return null; @@ -77,8 +104,42 @@ public function refactor(Node $node): ?Node return $this->nodeFactory->createClassConstFetch($className, $enumCaseName); } - public function provideMinPhpVersion(): int + private function refactorGetValueMethodCall(MethodCall $methodCall): ?PropertyFetch { - return PhpVersionFeature::ENUM; + if (! $methodCall->var instanceof StaticCall) { + return null; + } + + $staticCall = $methodCall->var; + $className = $this->getName($staticCall->class); + if ($className === null) { + return null; + } + + $enumCaseName = $this->getName($staticCall->name); + if ($enumCaseName === null) { + return null; + } + + $enumConstFetch = $this->nodeFactory->createClassConstFetch($className, $enumCaseName); + + return new PropertyFetch($enumConstFetch, 'value'); + } + + private function refactorMethodCall(MethodCall $methodCall, string $methodName): null|ClassConstFetch|PropertyFetch + { + if (! $this->isObjectType($methodCall->var, new ObjectType('MyCLabs\Enum\Enum'))) { + return null; + } + + if ($methodName === 'getKey') { + return $this->refactorGetKeyMethodCall($methodCall); + } + + if ($methodName === 'getValue') { + return $this->refactorGetValueMethodCall($methodCall); + } + + return null; } }