2021-05-14 09:53:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Php81\Rector\Class_;
|
2021-05-14 09:53:43 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PHPStan\Type\ObjectType;
|
|
|
|
use Rector\Php81\NodeFactory\EnumFactory;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2021-05-14 09:53:43 +00:00
|
|
|
/**
|
|
|
|
* @changelog https://wiki.php.net/rfc/enumerations
|
|
|
|
* @changelog https://github.com/myclabs/php-enum
|
|
|
|
*
|
|
|
|
* @see \Rector\Tests\Php81\Rector\Class_\MyCLabsClassToEnumRector\MyCLabsClassToEnumRectorTest
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class MyCLabsClassToEnumRector extends AbstractRector implements MinPhpVersionInterface
|
2021-05-14 09:53:43 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-14 09:53:43 +00:00
|
|
|
* @var \Rector\Php81\NodeFactory\EnumFactory
|
|
|
|
*/
|
|
|
|
private $enumFactory;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(EnumFactory $enumFactory)
|
2021-05-14 09:53:43 +00:00
|
|
|
{
|
|
|
|
$this->enumFactory = $enumFactory;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2021-05-14 09:53:43 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Refactor MyCLabs enum class to native Enum', [new CodeSample(<<<'CODE_SAMPLE'
|
2021-05-14 09:53:43 +00:00
|
|
|
use MyCLabs\Enum\Enum;
|
|
|
|
|
|
|
|
final class Action extends Enum
|
|
|
|
{
|
|
|
|
private const VIEW = 'view';
|
|
|
|
private const EDIT = 'edit';
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
, <<<'CODE_SAMPLE'
|
2021-12-02 07:34:16 +00:00
|
|
|
enum Action : string
|
2021-05-14 09:53:43 +00:00
|
|
|
{
|
|
|
|
case VIEW = 'view';
|
|
|
|
case EDIT = 'edit';
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
)]);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return array<class-string<Node>>
|
|
|
|
*/
|
|
|
|
public function getNodeTypes() : array
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Class_::class];
|
2021-05-14 09:53:43 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Class_ $node
|
2021-05-14 09:53:43 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2021-05-14 09:53:43 +00:00
|
|
|
{
|
2022-06-07 09:46:15 +00:00
|
|
|
if (!$this->isObjectType($node, new ObjectType('MyCLabs\\Enum\\Enum'))) {
|
2021-05-14 09:53:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->enumFactory->createFromClass($node);
|
|
|
|
}
|
2021-09-02 15:24:03 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::ENUM;
|
2021-09-02 15:24:03 +00:00
|
|
|
}
|
2021-05-14 09:53:43 +00:00
|
|
|
}
|