diff --git a/rules-tests/Php81/Rector/Class_/MyCLabsClassToEnumRector/Fixture/private_constant_with_static_method.php.inc b/rules-tests/Php81/Rector/Class_/MyCLabsClassToEnumRector/Fixture/private_constant_with_static_method.php.inc index d8b76649db9..a83e6ef05af 100644 --- a/rules-tests/Php81/Rector/Class_/MyCLabsClassToEnumRector/Fixture/private_constant_with_static_method.php.inc +++ b/rules-tests/Php81/Rector/Class_/MyCLabsClassToEnumRector/Fixture/private_constant_with_static_method.php.inc @@ -23,7 +23,7 @@ namespace Rector\Tests\Php81\Rector\Class_\MyCLabsClassToEnumRector\Fixture; use MyCLabs\Enum\Enum; -enum PrivateConstWithStaticMethod +enum PrivateConstWithStaticMethod : string { /** * Some comment diff --git a/rules-tests/Php81/Rector/Class_/MyCLabsClassToEnumRector/Fixture/some_class.php.inc b/rules-tests/Php81/Rector/Class_/MyCLabsClassToEnumRector/Fixture/some_class.php.inc index 147d371f4d5..07f705db1a1 100644 --- a/rules-tests/Php81/Rector/Class_/MyCLabsClassToEnumRector/Fixture/some_class.php.inc +++ b/rules-tests/Php81/Rector/Class_/MyCLabsClassToEnumRector/Fixture/some_class.php.inc @@ -22,7 +22,7 @@ namespace Rector\Tests\Php81\Rector\Class_\MyCLabsClassToEnumRector\Fixture; use MyCLabs\Enum\Enum; -enum SomeClass +enum SomeClass : string { /** * Some comment diff --git a/rules-tests/Php81/Rector/Class_/SpatieEnumClassToEnumRector/Fixture/some_class.php.inc b/rules-tests/Php81/Rector/Class_/SpatieEnumClassToEnumRector/Fixture/some_class.php.inc index 7e083403f76..4bc2404f949 100644 --- a/rules-tests/Php81/Rector/Class_/SpatieEnumClassToEnumRector/Fixture/some_class.php.inc +++ b/rules-tests/Php81/Rector/Class_/SpatieEnumClassToEnumRector/Fixture/some_class.php.inc @@ -21,7 +21,7 @@ namespace Rector\Tests\Php81\Rector\Class_\SpatieEnumClassToEnumRector\Fixture; use Spatie\Enum\Enum; -enum StatusEnum +enum StatusEnum : string { case draft = 'draft'; case published = 'published'; diff --git a/rules/Php81/NodeFactory/EnumFactory.php b/rules/Php81/NodeFactory/EnumFactory.php index 280e6dd5470..bed7128e782 100644 --- a/rules/Php81/NodeFactory/EnumFactory.php +++ b/rules/Php81/NodeFactory/EnumFactory.php @@ -5,6 +5,7 @@ namespace Rector\Php81\NodeFactory; use PhpParser\BuilderFactory; +use PhpParser\Node\Identifier; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassConst; use PhpParser\Node\Stmt\Enum_; @@ -12,6 +13,7 @@ use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; +use Rector\Core\PhpParser\Node\Value\ValueResolver; use Rector\NodeNameResolver\NodeNameResolver; use Rector\NodeTypeResolver\Node\AttributeKey; @@ -20,7 +22,8 @@ final class EnumFactory public function __construct( private NodeNameResolver $nodeNameResolver, private PhpDocInfoFactory $phpDocInfoFactory, - private BuilderFactory $builderFactory + private BuilderFactory $builderFactory, + private ValueResolver $valueResolver ) { } @@ -29,9 +32,18 @@ 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->createEnumCaseFromConst($classConst); + $constants = $class->getConstants(); + + if ($constants !== []) { + $value = $this->valueResolver->getValue($constants[0]->consts[0]->value); + $enum->scalarType = is_string($value) + ? new Identifier('string') + : new Identifier('int'); + + // constant to cases + foreach ($constants as $constant) { + $enum->stmts[] = $this->createEnumCaseFromConst($constant); + } } return $enum; @@ -47,6 +59,8 @@ public function createFromSpatieClass(Class_ $class): Enum_ $docBlockMethods = $phpDocInfo?->getTagsByName('@method'); if ($docBlockMethods !== null) { + $enum->scalarType = new Identifier('string'); + foreach ($docBlockMethods as $docBlockMethod) { $enum->stmts[] = $this->createEnumCaseFromDocComment($docBlockMethod); } diff --git a/rules/Php81/Rector/Class_/MyCLabsClassToEnumRector.php b/rules/Php81/Rector/Class_/MyCLabsClassToEnumRector.php index ae0fc3ebd76..b5ae351eb03 100644 --- a/rules/Php81/Rector/Class_/MyCLabsClassToEnumRector.php +++ b/rules/Php81/Rector/Class_/MyCLabsClassToEnumRector.php @@ -43,7 +43,7 @@ final class Action extends Enum , <<<'CODE_SAMPLE' -enum Action +enum Action : string { case VIEW = 'view'; case EDIT = 'edit'; diff --git a/rules/Php81/Rector/Class_/SpatieEnumClassToEnumRector.php b/rules/Php81/Rector/Class_/SpatieEnumClassToEnumRector.php index 456c4b256a2..33d772ad904 100644 --- a/rules/Php81/Rector/Class_/SpatieEnumClassToEnumRector.php +++ b/rules/Php81/Rector/Class_/SpatieEnumClassToEnumRector.php @@ -51,7 +51,7 @@ class StatusEnum extends Enum , <<<'CODE_SAMPLE' -enum StatusEnum +enum StatusEnum : string { case draft = 'draft'; case published = 'published';