From 6807758c36284578646dec61dde0c01f63c92c78 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 20 Jul 2022 15:05:48 +0200 Subject: [PATCH] use different name than value to avoid conflict with enum value property --- .../Fixture/skip_magic_calls.php.inc | 13 ++++++++++++ .../Fixture/usage_of_constant.php.inc | 2 +- .../Fixture/usage_of_constant_value.php.inc | 2 +- .../Source/SomeEnum.php | 4 ++-- .../MyCLabsMethodCallToEnumConstRector.php | 20 ++++++++++++++----- 5 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 rules-tests/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector/Fixture/skip_magic_calls.php.inc 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 @@ +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 index 2fdc521ceb7..ced49dd1539 100644 --- 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 @@ -24,7 +24,7 @@ final class UsageOfConstantValue { public function run($value) { - $compare = \Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Source\SomeEnum::VALUE->value; + $compare = \Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Source\SomeEnum::VALUE->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 711ba7a5546..b77ae16a801 100644 --- a/rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php +++ b/rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php @@ -5,6 +5,7 @@ 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; @@ -53,8 +54,17 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { + if ($node->name instanceof Expr) { + return null; + } + + $enumCaseName = $this->getName($node->name); + if ($enumCaseName === null) { + return null; + } + if ($node instanceof MethodCall) { - return $this->refactorMethodCall($node); + return $this->refactorMethodCall($node, $enumCaseName); } if (! $this->isObjectType($node->class, new ObjectType('MyCLabs\Enum\Enum'))) { @@ -63,7 +73,7 @@ public function refactor(Node $node): ?Node $className = $this->getName($node->class); - return $this->nodeFactory->createClassConstFetch($className, $node->name->toString()); + return $this->nodeFactory->createClassConstFetch($className, $enumCaseName); } public function provideMinPhpVersion(): int @@ -113,17 +123,17 @@ private function refactorGetValueMethodCall(MethodCall $methodCall): ?PropertyFe return new PropertyFetch($enumConstFetch, 'value'); } - private function refactorMethodCall(MethodCall $methodCall): null|ClassConstFetch|PropertyFetch + private function refactorMethodCall(MethodCall $methodCall, string $methodName): null|ClassConstFetch|PropertyFetch { if (! $this->isObjectType($methodCall->var, new ObjectType('MyCLabs\Enum\Enum'))) { return null; } - if ($this->isName($methodCall->name, 'getKey')) { + if ($methodName === 'getKey') { return $this->refactorGetKeyMethodCall($methodCall); } - if ($this->isName($methodCall->name, 'getValue')) { + if ($methodName === 'getValue') { return $this->refactorGetValueMethodCall($methodCall); }