-
-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: replace MyCLabs Enum constructor call
- Loading branch information
1 parent
15fcd52
commit ca5a6d2
Showing
11 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
.../Rector/New_/MyCLabsConstructorCallToEnumFromRector/Fixture/constructor_self_call.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\New_\MyCLabsConstructorCallToEnumFromRector\Fixture; | ||
|
||
use MyCLabs\Enum\Enum; | ||
|
||
class EnumWithSelfCreation extends Enum | ||
{ | ||
private const VALUE = 'value'; | ||
|
||
public static function newSelf(): self | ||
{ | ||
return new self('value'); | ||
} | ||
|
||
public static function newStatic(): static | ||
{ | ||
return new static('value'); | ||
} | ||
} | ||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\New_\MyCLabsConstructorCallToEnumFromRector\Fixture; | ||
|
||
use MyCLabs\Enum\Enum; | ||
|
||
class EnumWithSelfCreation extends Enum | ||
{ | ||
private const VALUE = 'value'; | ||
|
||
public static function newSelf(): self | ||
{ | ||
return Rector\Tests\Php81\Rector\New_\MyCLabsConstructorCallToEnumFromRector\Fixture\EnumWithSelfCreation::from('value'); | ||
} | ||
|
||
public static function newStatic(): static | ||
{ | ||
return Rector\Tests\Php81\Rector\New_\MyCLabsConstructorCallToEnumFromRector\Fixture\EnumWithSelfCreation::from('value'); | ||
} | ||
} | ||
?> |
29 changes: 29 additions & 0 deletions
29
...ctor/New_/MyCLabsConstructorCallToEnumFromRector/Fixture/default_constructor_call.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\New_\MyCLabsConstructorCallToEnumFromRector\Fixture; | ||
|
||
use Rector\Tests\Php81\Rector\New_\MyCLabsConstructorCallToEnumFromRector\Source\SomeEnum; | ||
|
||
class RefactorDefaultConstructorCall | ||
{ | ||
public function someMethod(): SomeEnum | ||
{ | ||
return new SomeEnum('value'); | ||
} | ||
} | ||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\New_\MyCLabsConstructorCallToEnumFromRector\Fixture; | ||
|
||
use Rector\Tests\Php81\Rector\New_\MyCLabsConstructorCallToEnumFromRector\Source\SomeEnum; | ||
|
||
class RefactorDefaultConstructorCall | ||
{ | ||
public function someMethod(): SomeEnum | ||
{ | ||
return Rector\Tests\Php81\Rector\New_\MyCLabsConstructorCallToEnumFromRector\Source\SomeEnum::from('value'); | ||
} | ||
} | ||
?> |
14 changes: 14 additions & 0 deletions
14
...structorCallToEnumFromRector/Fixture/skip_constructor_call_for_custom_constructor.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\New_\MyCLabsConstructorCallToEnumFromRector\Fixture; | ||
|
||
use Rector\Tests\Php81\Rector\New_\MyCLabsConstructorCallToEnumFromRector\Source\SomeEnumWithConstructor; | ||
|
||
class SkipConstructorCallForCustomConstructor | ||
{ | ||
public function someMethod(): SomeEnumWithConstructor | ||
{ | ||
return new SomeEnumWithConstructor('value'); | ||
} | ||
} | ||
?> |
14 changes: 14 additions & 0 deletions
14
...MyCLabsConstructorCallToEnumFromRector/Fixture/skip_constructor_call_for_non_enum.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\New_\MyCLabsConstructorCallToEnumFromRector\Fixture; | ||
|
||
use Rector\Tests\Php81\Rector\New_\MyCLabsConstructorCallToEnumFromRector\Source\SomeClass; | ||
|
||
class SkipConstructorCallForNonEnum | ||
{ | ||
public function someMethod(): SomeClass | ||
{ | ||
return new SomeClass(); | ||
} | ||
} | ||
?> |
28 changes: 28 additions & 0 deletions
28
...ew_/MyCLabsConstructorCallToEnumFromRector/MyCLabsConstructorCallToEnumFromRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Php81\Rector\New_\MyCLabsConstructorCallToEnumFromRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class MyCLabsConstructorCallToEnumFromRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
rules-tests/Php81/Rector/New_/MyCLabsConstructorCallToEnumFromRector/Source/SomeClass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Php81\Rector\New_\MyCLabsConstructorCallToEnumFromRector\Source; | ||
|
||
final class SomeClass | ||
{ | ||
} |
15 changes: 15 additions & 0 deletions
15
rules-tests/Php81/Rector/New_/MyCLabsConstructorCallToEnumFromRector/Source/SomeEnum.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Php81\Rector\New_\MyCLabsConstructorCallToEnumFromRector\Source; | ||
|
||
use MyCLabs\Enum\Enum; | ||
|
||
/** | ||
* @method SomeEnum VALUE() | ||
*/ | ||
final class SomeEnum extends Enum | ||
{ | ||
const VALUE = 'value'; | ||
} |
21 changes: 21 additions & 0 deletions
21
...p81/Rector/New_/MyCLabsConstructorCallToEnumFromRector/Source/SomeEnumWithConstructor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Php81\Rector\New_\MyCLabsConstructorCallToEnumFromRector\Source; | ||
|
||
use MyCLabs\Enum\Enum; | ||
|
||
/** | ||
* @method SomeEnumWithConstructor VALUE() | ||
*/ | ||
final class SomeEnumWithConstructor extends Enum | ||
{ | ||
const VALUE = 'value'; | ||
|
||
public function __construct($value) | ||
{ | ||
//some logic | ||
parent::__construct($value); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...tests/Php81/Rector/New_/MyCLabsConstructorCallToEnumFromRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Php81\Rector\New_\MyCLabsConstructorCallToEnumFromRector; | ||
|
||
return RectorConfig::configure() | ||
->withRules([MyCLabsConstructorCallToEnumFromRector::class]); |
106 changes: 106 additions & 0 deletions
106
rules/Php81/Rector/New_/MyCLabsConstructorCallToEnumFromRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
namespace Rector\Php81\Rector\New_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\New_; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Name; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Enum\ObjectReference; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\ValueObject\MethodName; | ||
use Rector\ValueObject\PhpVersionFeature; | ||
use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\Php81\Rector\New_\MyCLabsConstructorCallToEnumFromRector\MyCLabsConstructorCallToEnumFromRectorTest | ||
*/ | ||
final class MyCLabsConstructorCallToEnumFromRector extends AbstractRector implements MinPhpVersionInterface | ||
{ | ||
private const MY_C_LABS_CLASS = 'MyCLabs\Enum\Enum'; | ||
|
||
private const DEFAULT_ENUM_CONSTRUCTOR = 'from'; | ||
|
||
public function __construct( | ||
private readonly ReflectionProvider $reflectionProvider, | ||
) { | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [New_::class]; | ||
} | ||
|
||
/** | ||
* @param New_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
return $this->refactorConstructorCallToStaticFromCall($node); | ||
} | ||
|
||
public function provideMinPhpVersion(): int | ||
{ | ||
return PhpVersionFeature::ENUM; | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Refactor MyCLabs Enum using constructor for instantiation', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
$enum = new Enum($args); | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
$enum = Enum::from($args); | ||
CODE_SAMPLE | ||
)] | ||
); | ||
} | ||
|
||
private function refactorConstructorCallToStaticFromCall(New_ $node): ?StaticCall | ||
{ | ||
if (! $this->isObjectType($node->class, new ObjectType(self::MY_C_LABS_CLASS))) { | ||
return null; | ||
} | ||
|
||
$classname = $this->getName($node->class); | ||
if (in_array($classname, [ObjectReference::SELF, ObjectReference::STATIC], true)) { | ||
$classname = $node->getAttribute('scope')?->getClassReflection()?->getDisplayName(); | ||
} | ||
|
||
if ($classname === null) { | ||
return null; | ||
} | ||
|
||
if (! $this->isMyCLabsConstructor($node, $classname)) { | ||
return null; | ||
} | ||
|
||
return new StaticCall(new Name($classname), self::DEFAULT_ENUM_CONSTRUCTOR, $node->getArgs()); | ||
} | ||
|
||
private function isMyCLabsConstructor(New_ $node, string $classname): bool | ||
{ | ||
$class_reflection = $this->reflectionProvider->getClass($classname); | ||
if (! $class_reflection->hasMethod(MethodName::CONSTRUCT)) { | ||
return true; | ||
} | ||
|
||
$scope = $node->getAttribute('scope'); | ||
return $scope && $class_reflection | ||
->getMethod(MethodName::CONSTRUCT, $scope) | ||
->getDeclaringClass() | ||
->getName() === self::MY_C_LABS_CLASS; | ||
} | ||
} |