-
-
Notifications
You must be signed in to change notification settings - Fork 410
feature: replace MyCLabs Enum constructor call #6588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TomasVotruba
merged 4 commits into
rectorphp:main
from
carlosvinicius:feature/replace-myclabs-enum-constructor-call
Jan 11, 2025
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f4cd998
feature: replace MyCLabs Enum constructor call
carlosvinicius 94a4b00
pr-review: update variable naming
carlosvinicius 727313c
pr-review: use node args directly
carlosvinicius 6e76750
pr-review: use name over displayName from reflection
carlosvinicius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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\PHPStan\ScopeFetcher; | ||
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 = ScopeFetcher::fetch($node) ->getClassReflection()?->getDisplayName(); | ||
} | ||
|
||
if ($classname === null) { | ||
return null; | ||
} | ||
|
||
if (! $this->isMyCLabsConstructor($node, $classname)) { | ||
return null; | ||
} | ||
|
||
return new StaticCall(new Name\FullyQualified($classname), self::DEFAULT_ENUM_CONSTRUCTOR, $node->args); | ||
} | ||
|
||
private function isMyCLabsConstructor(New_ $node, string $classname): bool | ||
{ | ||
$classReflection = $this->reflectionProvider->getClass($classname); | ||
if (! $classReflection->hasMethod(MethodName::CONSTRUCT)) { | ||
return true; | ||
} | ||
|
||
return $classReflection | ||
->getMethod(MethodName::CONSTRUCT, ScopeFetcher::fetch($node)) | ||
->getDeclaringClass() | ||
->getName() === self::MY_C_LABS_CLASS; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.