2021-08-12 14:48:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Php81\Rector\Class_;
|
2021-08-12 14:48:46 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\Enum_;
|
|
|
|
use PHPStan\Type\ObjectType;
|
2024-01-06 13:17:15 +00:00
|
|
|
use Rector\Contract\Rector\ConfigurableRectorInterface;
|
2022-06-06 17:12:56 +00:00
|
|
|
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;
|
2024-01-16 10:21:57 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2021-08-12 14:48:46 +00:00
|
|
|
/**
|
|
|
|
* @changelog https://wiki.php.net/rfc/enumerations
|
|
|
|
* @changelog https://github.com/spatie/enum
|
|
|
|
*
|
|
|
|
* @see \Rector\Tests\Php81\Rector\Class_\SpatieEnumClassToEnumRector\SpatieEnumClassToEnumRectorTest
|
|
|
|
*/
|
2024-01-06 13:17:15 +00:00
|
|
|
final class SpatieEnumClassToEnumRector extends AbstractRector implements MinPhpVersionInterface, ConfigurableRectorInterface
|
2021-08-12 14:48:46 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-08-12 14:48:46 +00:00
|
|
|
* @var \Rector\Php81\NodeFactory\EnumFactory
|
|
|
|
*/
|
|
|
|
private $enumFactory;
|
2024-01-16 10:21:57 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public const TO_UPPER_SNAKE_CASE = 'toUpperSnakeCase';
|
2024-01-17 14:38:19 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $toUpperSnakeCase = \false;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(EnumFactory $enumFactory)
|
2021-08-12 14:48:46 +00:00
|
|
|
{
|
|
|
|
$this->enumFactory = $enumFactory;
|
|
|
|
}
|
2021-09-24 14:11:08 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::ENUM;
|
2021-09-24 14:11:08 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2021-08-12 14:48:46 +00:00
|
|
|
{
|
2024-01-16 10:21:57 +00:00
|
|
|
return new RuleDefinition('Refactor Spatie enum class to native Enum', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
2021-08-12 14:48:46 +00:00
|
|
|
use \Spatie\Enum\Enum;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method static self draft()
|
|
|
|
* @method static self published()
|
|
|
|
* @method static self archived()
|
|
|
|
*/
|
|
|
|
class StatusEnum extends Enum
|
|
|
|
{
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
, <<<'CODE_SAMPLE'
|
2021-12-02 07:34:16 +00:00
|
|
|
enum StatusEnum : string
|
2021-08-12 14:48:46 +00:00
|
|
|
{
|
2022-06-13 09:09:58 +00:00
|
|
|
case DRAFT = 'draft';
|
|
|
|
case PUBLISHED = 'published';
|
|
|
|
case ARCHIVED = 'archived';
|
2021-08-12 14:48:46 +00:00
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2024-01-16 10:21:57 +00:00
|
|
|
, [\Rector\Php81\Rector\Class_\SpatieEnumClassToEnumRector::TO_UPPER_SNAKE_CASE => \false])]);
|
2021-08-12 14:48:46 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return array<class-string<Node>>
|
|
|
|
*/
|
|
|
|
public function getNodeTypes() : array
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Class_::class];
|
2021-08-12 14:48:46 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Class_ $node
|
2021-08-12 14:48:46 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Enum_
|
2021-08-12 14:48:46 +00:00
|
|
|
{
|
2022-06-07 09:46:15 +00:00
|
|
|
if (!$this->isObjectType($node, new ObjectType('Spatie\\Enum\\Enum'))) {
|
2021-08-12 14:48:46 +00:00
|
|
|
return null;
|
|
|
|
}
|
2024-01-06 13:17:15 +00:00
|
|
|
return $this->enumFactory->createFromSpatieClass($node, $this->toUpperSnakeCase);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param mixed[] $configuration
|
|
|
|
*/
|
|
|
|
public function configure(array $configuration) : void
|
|
|
|
{
|
2024-01-16 10:21:57 +00:00
|
|
|
$this->toUpperSnakeCase = $configuration[self::TO_UPPER_SNAKE_CASE] ?? \false;
|
2021-08-12 14:48:46 +00:00
|
|
|
}
|
|
|
|
}
|