Skip to content

Commit

Permalink
use different name than value to avoid conflict with enum value property
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jul 20, 2022
1 parent 8c83d4c commit 6807758
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Fixture;

use Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Source\SomeEnum;

final class SkipMagicCalls
{
public function run($value, $magicMethod)
{
$compare = SomeEnum::$magicMethod();
}
}
Expand Up @@ -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();
}
}

Expand Down
Expand Up @@ -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();
}
}

Expand Down
Expand Up @@ -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';
}
Expand Up @@ -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;
Expand Down Expand Up @@ -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'))) {
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 6807758

Please sign in to comment.