Skip to content

Commit

Permalink
Refactor MyCLabs\Enum\Enum::equals calls to comparisons (#4532)
Browse files Browse the repository at this point in the history
  • Loading branch information
rojtjo committed Jul 18, 2023
1 parent 56068f8 commit 5a9315a
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

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

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

final class UsageOfEquals
{
private SomeEnum $prop;

public function run($value)
{
$this->prop = $var = SomeEnum::USED_TO_BE_CONST();

$compare = SomeEnum::USED_TO_BE_CONST()->equals(SomeEnum::USED_TO_BE_CONST());
$compare = SomeEnum::USED_TO_BE_CONST()->equals($var);
$compare = SomeEnum::USED_TO_BE_CONST()->equals($this->prop);

$compare = $var->equals(SomeEnum::USED_TO_BE_CONST());
$compare = $var->equals($var);
$compare = $var->equals($this->prop);

$compare = $this->prop->equals(SomeEnum::USED_TO_BE_CONST());
$compare = $this->prop->equals($var);
$compare = $this->prop->equals($this->prop);
}
}

?>
-----
<?php

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

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

final class UsageOfEquals
{
private SomeEnum $prop;

public function run($value)
{
$this->prop = $var = \Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Source\SomeEnum::USED_TO_BE_CONST;

$compare = \Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Source\SomeEnum::USED_TO_BE_CONST === \Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Source\SomeEnum::USED_TO_BE_CONST;
$compare = \Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Source\SomeEnum::USED_TO_BE_CONST === $var;
$compare = \Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Source\SomeEnum::USED_TO_BE_CONST === $this->prop;

$compare = $var === \Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Source\SomeEnum::USED_TO_BE_CONST;
$compare = $var === $var;
$compare = $var === $this->prop;

$compare = $this->prop === \Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Source\SomeEnum::USED_TO_BE_CONST;
$compare = $this->prop === $var;
$compare = $this->prop === $this->prop;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
Expand Down Expand Up @@ -143,8 +145,67 @@ private function refactorGetValueMethodCall(MethodCall $methodCall): ?PropertyFe
return new PropertyFetch($enumConstFetch, 'value');
}

private function refactorMethodCall(MethodCall $methodCall, string $methodName): null|ClassConstFetch|PropertyFetch
private function refactorEqualsMethodCall(MethodCall $methodCall): ?Identical
{
$left = $this->getValidEnumExpr($methodCall->var);
if ($left === null) {
return null;
}

$arg = $methodCall->getArgs()[0] ?? null;
if ($arg === null) {
return null;
}

$right = $this->getValidEnumExpr($arg->value);
if ($right === null) {
return null;
}

return new Identical($left, $right);
}

private function getValidEnumExpr(Node $node): null|ClassConstFetch|Expr
{
return match ($node::class) {
Variable::class, PropertyFetch::class => $this->getPropertyFetchOrVariable($node),
StaticCall::class => $this->getEnumConstFetch($node),
default => null,
};
}

private function getPropertyFetchOrVariable(PropertyFetch|Variable $expr): null|PropertyFetch|Variable
{
if (! $this->isObjectType($expr, new ObjectType('MyCLabs\Enum\Enum'))) {
return null;
}

return $expr;
}

private function getEnumConstFetch(StaticCall $staticCall): null|ClassConstFetch
{
$className = $this->getName($staticCall->class);
if ($className === null) {
return null;
}

$enumCaseName = $this->getName($staticCall->name);
if ($enumCaseName === null) {
return null;
}

if ($this->shouldOmitEnumCase($enumCaseName)) {
return null;
}

return $this->nodeFactory->createClassConstFetch($className, $enumCaseName);
}

private function refactorMethodCall(
MethodCall $methodCall,
string $methodName
): null|ClassConstFetch|PropertyFetch|Identical {
if (! $this->isObjectType($methodCall->var, new ObjectType('MyCLabs\Enum\Enum'))) {
return null;
}
Expand All @@ -157,6 +218,10 @@ private function refactorMethodCall(MethodCall $methodCall, string $methodName):
return $this->refactorGetValueMethodCall($methodCall);
}

if ($methodName === 'equals') {
return $this->refactorEqualsMethodCall($methodCall);
}

return null;
}

Expand Down

0 comments on commit 5a9315a

Please sign in to comment.