Skip to content

Commit

Permalink
[PHP 8.1] Extend MyCLabsMethodCallToEnumConstRector with getValue() a…
Browse files Browse the repository at this point in the history
…nd static call (#2695)
  • Loading branch information
TomasVotruba committed Jul 20, 2022
1 parent aee31ce commit 963c48c
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 11 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();
}
}
@@ -0,0 +1,31 @@
<?php

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

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

final class StaticMethodCall
{
public function run($value)
{
$compare = SomeEnum::USED_TO_BE_CONST();
}
}

?>
-----
<?php

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

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

final class StaticMethodCall
{
public function run($value)
{
$compare = \Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Source\SomeEnum::USED_TO_BE_CONST;
}
}

?>
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
@@ -0,0 +1,31 @@
<?php

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

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

final class UsageOfConstantValue
{
public function run($value)
{
$compare = SomeEnum::VALUE()->getValue();
}
}

?>
-----
<?php

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

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

final class UsageOfConstantValue
{
public function run($value)
{
$compare = \Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Source\SomeEnum::VALUE->getValue();
}
}

?>
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,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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}

0 comments on commit 963c48c

Please sign in to comment.