-
-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Enum] Add DowngradeEnumToConstantListClassRector
- Loading branch information
1 parent
a73dafd
commit 372cd32
Showing
6 changed files
with
196 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
33 changes: 33 additions & 0 deletions
33
...um_/DowngradeEnumToConstantListClassRector/DowngradeEnumToConstantListClassRectorTest.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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\Enum_\DowngradeEnumToConstantListClassRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class DowngradeEnumToConstantListClassRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
/** | ||
* @return Iterator<SmartFileInfo> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...gradePhp80/Rector/Enum_/DowngradeEnumToConstantListClassRector/Fixture/some_class.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,24 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\Enum_\DowngradeEnumToConstantListClassRector\Fixture; | ||
|
||
enum Direction | ||
{ | ||
case LEFT; | ||
|
||
case RIGHT; | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp80\Rector\Enum_\DowngradeEnumToConstantListClassRector\Fixture; | ||
|
||
class Direction | ||
{ | ||
public const LEFT = 'left'; | ||
public const RIGHT = 'right'; | ||
} | ||
|
||
?> |
10 changes: 10 additions & 0 deletions
10
...gradePhp80/Rector/Enum_/DowngradeEnumToConstantListClassRector/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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\DowngradePhp80\Rector\Enum_\DowngradeEnumToConstantListClassRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(DowngradeEnumToConstantListClassRector::class); | ||
}; |
66 changes: 66 additions & 0 deletions
66
rules/DowngradePhp80/Rector/Enum_/DowngradeEnumToConstantListClassRector.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,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DowngradePhp80\Rector\Enum_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\Enum_; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Php81\NodeFactory\ClassFromEnumFactory; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\DowngradePhp80\Rector\Enum_\DowngradeEnumToConstantListClassRector\DowngradeEnumToConstantListClassRectorTest | ||
*/ | ||
final class DowngradeEnumToConstantListClassRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private readonly ClassFromEnumFactory $classFromEnumFactory | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Downgrade enum to constant list class', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
enum Direction | ||
{ | ||
case LEFT; | ||
case RIGHT; | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
class Direction | ||
{ | ||
public const LEFT = 'left'; | ||
public const RIGHT = 'right'; | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Enum_::class]; | ||
} | ||
|
||
/** | ||
* @param Enum_ $node | ||
*/ | ||
public function refactor(Node $node): Class_ | ||
{ | ||
return $this->classFromEnumFactory->createFromEnum($node); | ||
} | ||
} |
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,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php81\NodeFactory; | ||
|
||
use PhpParser\Node\Const_; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Scalar\String_; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassConst; | ||
use PhpParser\Node\Stmt\Enum_; | ||
use PhpParser\Node\Stmt\EnumCase; | ||
use Rector\Core\ValueObject\Visibility; | ||
use Rector\NodeNameResolver\NodeNameResolver; | ||
|
||
final class ClassFromEnumFactory | ||
{ | ||
public function __construct( | ||
private readonly NodeNameResolver $nodeNameResolver | ||
) { | ||
} | ||
|
||
public function createFromEnum(Enum_ $enum): Class_ | ||
{ | ||
$shortClassName = $this->nodeNameResolver->getShortName($enum); | ||
|
||
$classConsts = []; | ||
foreach ($enum->stmts as $stmt) { | ||
if (! $stmt instanceof EnumCase) { | ||
continue; | ||
} | ||
|
||
$constValue = $this->createConstValue($stmt); | ||
|
||
$classConsts[] = new ClassConst([new Const_($stmt->name, $constValue)], Visibility::PUBLIC); | ||
} | ||
|
||
$class = new Class_($shortClassName, [ | ||
'stmts' => $classConsts, | ||
]); | ||
|
||
$class->namespacedName = $enum->namespacedName; | ||
|
||
return $class; | ||
} | ||
|
||
private function createConstValue(EnumCase $enumCase): Expr | ||
{ | ||
if ($enumCase->expr instanceof Expr) { | ||
return $enumCase->expr; | ||
} | ||
|
||
/** @var string $enumName */ | ||
$enumName = $this->nodeNameResolver->getName($enumCase); | ||
|
||
// minimal convention | ||
$lowercasedEnumValue = strtolower($enumName); | ||
|
||
return new String_($lowercasedEnumValue); | ||
} | ||
} |