-
-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PHP 8.1] Add MyClabs enum to native ENUM (#30)
- Loading branch information
1 parent
903dfab
commit f2c0023
Showing
17 changed files
with
422 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# testing Windows spaces - https://help.github.com/en/github/using-git/configuring-git-to-handle-line-endings | ||
packages-tests/FileFormatter/ValueObject/Fixture/composer_carriage_return_line_feed.json json eol=crlf |
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
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
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
34 changes: 34 additions & 0 deletions
34
...ector/Class_/MyCLabsClassToEnumRector/Fixture/private_constant_with_static_method.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,34 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\Class_\MyCLabsClassToEnumRector\Fixture; | ||
|
||
use MyCLabs\Enum\Enum; | ||
|
||
/** | ||
* @method static PrivateConstWithStaticMethod VIDEO() | ||
*/ | ||
final class PrivateConstWithStaticMethod extends Enum | ||
{ | ||
/** | ||
* Some comment | ||
*/ | ||
private const VIDEO = 'video'; | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\Class_\MyCLabsClassToEnumRector\Fixture; | ||
|
||
use MyCLabs\Enum\Enum; | ||
|
||
enum PrivateConstWithStaticMethod | ||
{ | ||
/** | ||
* Some comment | ||
*/ | ||
case VIDEO = 'video'; | ||
} | ||
|
||
?> |
34 changes: 34 additions & 0 deletions
34
rules-tests/Php81/Rector/Class_/MyCLabsClassToEnumRector/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,34 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\Class_\MyCLabsClassToEnumRector\Fixture; | ||
|
||
use MyCLabs\Enum\Enum; | ||
|
||
final class SomeClass extends Enum | ||
{ | ||
/** | ||
* Some comment | ||
*/ | ||
private const VIEW = 'view'; | ||
|
||
private const EDIT = 'edit'; | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\Class_\MyCLabsClassToEnumRector\Fixture; | ||
|
||
use MyCLabs\Enum\Enum; | ||
|
||
enum SomeClass | ||
{ | ||
/** | ||
* Some comment | ||
*/ | ||
case VIEW = 'view'; | ||
case EDIT = 'edit'; | ||
} | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
rules-tests/Php81/Rector/Class_/MyCLabsClassToEnumRector/MyCLabsClassToEnumRectorTest.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\Php81\Rector\Class_\MyCLabsClassToEnumRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class MyCLabsClassToEnumRectorTest 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'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
rules-tests/Php81/Rector/Class_/MyCLabsClassToEnumRector/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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Php81\Rector\Class_\MyCLabsClassToEnumRector; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
$services->set(MyCLabsClassToEnumRector::class); | ||
}; |
31 changes: 31 additions & 0 deletions
31
...81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector/Fixture/usage_of_constant.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,31 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Fixture; | ||
|
||
use Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Source\SomeEnum; | ||
|
||
final class UsageOfConstant | ||
{ | ||
public function run($value) | ||
{ | ||
$compare = SomeEnum::VALUE()->getKey(); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Fixture; | ||
|
||
use Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Source\SomeEnum; | ||
|
||
final class UsageOfConstant | ||
{ | ||
public function run($value) | ||
{ | ||
$compare = \Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\Source\SomeEnum::VALUE; | ||
} | ||
} | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
.../MethodCall/MyCLabsMethodCallToEnumConstRector/MyCLabsMethodCallToEnumConstRectorTest.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\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class MyCLabsMethodCallToEnumConstRectorTest 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'; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
rules-tests/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector/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\MethodCall\MyCLabsMethodCallToEnumConstRector\Source; | ||
|
||
use MyCLabs\Enum\Enum; | ||
|
||
/** | ||
* @method SomeEnum VALUE() | ||
*/ | ||
final class SomeEnum extends Enum | ||
{ | ||
const VALUE = 'value'; | ||
} |
11 changes: 11 additions & 0 deletions
11
...sts/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector/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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
$services->set(MyCLabsMethodCallToEnumConstRector::class); | ||
}; |
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
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,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php81\NodeFactory; | ||
|
||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassConst; | ||
use PhpParser\Node\Stmt\Enum_; | ||
use PhpParser\Node\Stmt\EnumCase; | ||
use Rector\NodeNameResolver\NodeNameResolver; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
|
||
final class EnumFactory | ||
{ | ||
public function __construct( | ||
private NodeNameResolver $nodeNameResolver | ||
) { | ||
} | ||
|
||
public function createFromClass(Class_ $class): Enum_ | ||
{ | ||
$shortClassName = $this->nodeNameResolver->getShortName($class); | ||
$enum = new Enum_($shortClassName); | ||
|
||
// constant to cases | ||
foreach ($class->getConstants() as $classConst) { | ||
$enum->stmts[] = $this->createEnumCase($classConst); | ||
} | ||
|
||
return $enum; | ||
} | ||
|
||
private function createEnumCase(ClassConst $classConst): EnumCase | ||
{ | ||
$constConst = $classConst->consts[0]; | ||
$enumCase = new EnumCase($constConst->name, $constConst->value); | ||
|
||
// mirrow comments | ||
$enumCase->setAttribute(AttributeKey::PHP_DOC_INFO, $classConst->getAttribute(AttributeKey::PHP_DOC_INFO)); | ||
$enumCase->setAttribute(AttributeKey::COMMENTS, $classConst->getAttribute(AttributeKey::COMMENTS)); | ||
|
||
return $enumCase; | ||
} | ||
} |
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,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php81\Rector\Class_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Php81\NodeFactory\EnumFactory; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @changelog https://wiki.php.net/rfc/enumerations | ||
* @changelog https://github.com/myclabs/php-enum | ||
* | ||
* @see \Rector\Tests\Php81\Rector\Class_\MyCLabsClassToEnumRector\MyCLabsClassToEnumRectorTest | ||
*/ | ||
final class MyCLabsClassToEnumRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private EnumFactory $enumFactory | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Refactor MyCLabs enum class to native Enum', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use MyCLabs\Enum\Enum; | ||
final class Action extends Enum | ||
{ | ||
private const VIEW = 'view'; | ||
private const EDIT = 'edit'; | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
enum Action | ||
{ | ||
case VIEW = 'view'; | ||
case EDIT = 'edit'; | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Class_::class]; | ||
} | ||
|
||
/** | ||
* @param Class_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->isObjectType($node, new ObjectType('MyCLabs\Enum\Enum'))) { | ||
return null; | ||
} | ||
|
||
return $this->enumFactory->createFromClass($node); | ||
} | ||
} |
Oops, something went wrong.