-
-
Notifications
You must be signed in to change notification settings - Fork 338
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 basic support for Spatie Enums to native Enums (#653)
* Add basic support for spatie Enums * move spatie/enum to require-dev * Update rules/Php81/NodeFactory/EnumFactory.php Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com> * Update rules/Php81/Rector/Class_/SpatieEnumClassToEnumRector.php Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com> * Update rules-tests/Php81/Rector/Class_/SpatieEnumClassToEnumRector/Fixture/some_class.php.inc Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com> * Update rules-tests/Php81/Rector/Class_/SpatieEnumClassToEnumRector/Fixture/some_class.php.inc * Update rules-tests/Php81/Rector/Class_/SpatieEnumClassToEnumRector/Fixture/some_class.php.inc * Update rules/Php81/Rector/Class_/SpatieEnumClassToEnumRector.php Co-authored-by: Tomas Votruba <tomas.vot@gmail.com> Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
- Loading branch information
1 parent
8e48e49
commit 48121fb
Showing
6 changed files
with
190 additions
and
4 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
31 changes: 31 additions & 0 deletions
31
rules-tests/Php81/Rector/Class_/SpatieEnumClassToEnumRector/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,31 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\Class_\SpatieEnumClassToEnumRector\Fixture; | ||
|
||
use Spatie\Enum\Enum; | ||
|
||
/** | ||
* @method static self draft() | ||
* @method static self published() | ||
* @method static self archived() | ||
*/ | ||
class StatusEnum extends Enum | ||
{ | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\Class_\SpatieEnumClassToEnumRector\Fixture; | ||
|
||
use Spatie\Enum\Enum; | ||
|
||
enum StatusEnum | ||
{ | ||
case draft = 'draft'; | ||
case published = 'published'; | ||
case archived = 'archived'; | ||
} | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
...tests/Php81/Rector/Class_/SpatieEnumClassToEnumRector/SpatieEnumClassToEnumRectorTest.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_\SpatieEnumClassToEnumRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class SpatieEnumClassToEnumRectorTest 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_/SpatieEnumClassToEnumRector/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_\SpatieEnumClassToEnumRector; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
$services->set(SpatieEnumClassToEnumRector::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,77 @@ | ||
<?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/spatie/enum | ||
* | ||
* @see \Rector\Tests\Php81\Rector\Class_\SpatieEnumClassToEnumRector\SpatieEnumClassToEnumRectorTest | ||
*/ | ||
final class SpatieEnumClassToEnumRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private EnumFactory $enumFactory | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Refactor Spatie enum class to native Enum', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use \Spatie\Enum\Enum; | ||
/** | ||
* @method static self draft() | ||
* @method static self published() | ||
* @method static self archived() | ||
*/ | ||
class StatusEnum extends Enum | ||
{ | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
enum StatusEnum | ||
{ | ||
case draft = 'draft'; | ||
case published = 'published'; | ||
case archived = 'archived'; | ||
} | ||
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('Spatie\Enum\Enum'))) { | ||
return null; | ||
} | ||
|
||
return $this->enumFactory->createFromSpatieClass($node); | ||
} | ||
} |