Skip to content

Commit

Permalink
[PHP 8.1] Do not process static methods from Enum class (#2911)
Browse files Browse the repository at this point in the history
* Do not process static methods from Enum class

* Properly handle case where result of the method is further accessed (chain call).

* keep FQN class names by convention

* [ci-review] Rector Rectify

Co-authored-by: xrogers <xrogers@gmail.com>
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
3 people committed Sep 5, 2022
1 parent edf1d52 commit f230e54
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

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

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

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

?>
-----
<?php

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

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

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

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

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

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

final class StaticEnumStaticMethodCall
{
public function run($value)
{
SomeEnum::from($value)->getKey();
SomeEnum::from($value)->getValue();
$compare = SomeEnum::from($value);
$compare = SomeEnum::values();
$compare = SomeEnum::keys();
$compare = SomeEnum::isValid($value);
$compare = SomeEnum::search($value);
$compare = SomeEnum::toArray();
SomeEnum::assertValidValue($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 UsageOfGetValue
{
public function run($value)
{
$enum = SomeEnum::USED_TO_BE_CONST()->getValue();
}
}

?>
-----
<?php

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

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

final class UsageOfGetValue
{
public function run($value)
{
$enum = \Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Source\SomeEnum::USED_TO_BE_CONST->value;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ public function test(string $filePath): void
$this->doTestFile($filePath);
}

/**
* @return Iterator<array<string>>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
*/
final class MyCLabsMethodCallToEnumConstRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @var string[]
*/
private const ENUM_METHODS = ['from', 'values', 'keys', 'isValid', 'search', 'toArray', 'assertValidValue'];

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Refactor MyCLabs enum fetch to Enum const', [
Expand Down Expand Up @@ -63,6 +68,10 @@ public function refactor(Node $node): ?Node
return null;
}

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

if ($node instanceof MethodCall) {
return $this->refactorMethodCall($node, $enumCaseName);
}
Expand Down Expand Up @@ -101,6 +110,10 @@ private function refactorGetKeyMethodCall(MethodCall $methodCall): ?ClassConstFe
return null;
}

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

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

Expand All @@ -121,6 +134,10 @@ private function refactorGetValueMethodCall(MethodCall $methodCall): ?PropertyFe
return null;
}

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

$enumConstFetch = $this->nodeFactory->createClassConstFetch($className, $enumCaseName);

return new PropertyFetch($enumConstFetch, 'value');
Expand All @@ -142,4 +159,9 @@ private function refactorMethodCall(MethodCall $methodCall, string $methodName):

return null;
}

private function shouldOmitEnumCase(string $enumCaseName): bool
{
return in_array($enumCaseName, self::ENUM_METHODS, true);
}
}

0 comments on commit f230e54

Please sign in to comment.