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
  • Loading branch information
TomasVotruba committed Jul 20, 2022
1 parent aee31ce commit 8c83d4c
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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::VALUE();
}
}

?>
-----
<?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::VALUE;
}
}

?>
Original file line number Diff line number Diff line change
@@ -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->value;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace Rector\Php81\Rector\MethodCall;

use PhpParser\Node;
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 +45,39 @@ 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'))) {
return null;
if ($node instanceof MethodCall) {
return $this->refactorMethodCall($node);
}

if (! $this->isName($node->name, 'getKey')) {
if (! $this->isObjectType($node->class, new ObjectType('MyCLabs\Enum\Enum'))) {
return null;
}

if (! $node->var instanceof StaticCall) {
$className = $this->getName($node->class);

return $this->nodeFactory->createClassConstFetch($className, $node->name->toString());
}

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 +91,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): null|ClassConstFetch|PropertyFetch
{
if (! $this->isObjectType($methodCall->var, new ObjectType('MyCLabs\Enum\Enum'))) {
return null;
}

if ($this->isName($methodCall->name, 'getKey')) {
return $this->refactorGetKeyMethodCall($methodCall);
}

if ($this->isName($methodCall->name, 'getValue')) {
return $this->refactorGetValueMethodCall($methodCall);
}

return null;
}
}

0 comments on commit 8c83d4c

Please sign in to comment.