Skip to content

Commit 1cbf32d

Browse files
authored
[Php81] Add scalar type on Enum MyCLabsClassToEnumRector and SpatieEnumClassToEnumRector (#1366)
* [Php81] Add native type on Enum MyCLabsClassToEnumRector and SpatieEnumClassToEnumRector * Fixed 🎉 * clean up * phpstan
1 parent 25da77f commit 1cbf32d

6 files changed

Lines changed: 23 additions & 9 deletions

File tree

rules-tests/Php81/Rector/Class_/MyCLabsClassToEnumRector/Fixture/private_constant_with_static_method.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace Rector\Tests\Php81\Rector\Class_\MyCLabsClassToEnumRector\Fixture;
2323

2424
use MyCLabs\Enum\Enum;
2525

26-
enum PrivateConstWithStaticMethod
26+
enum PrivateConstWithStaticMethod : string
2727
{
2828
/**
2929
* Some comment

rules-tests/Php81/Rector/Class_/MyCLabsClassToEnumRector/Fixture/some_class.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace Rector\Tests\Php81\Rector\Class_\MyCLabsClassToEnumRector\Fixture;
2222

2323
use MyCLabs\Enum\Enum;
2424

25-
enum SomeClass
25+
enum SomeClass : string
2626
{
2727
/**
2828
* Some comment

rules-tests/Php81/Rector/Class_/SpatieEnumClassToEnumRector/Fixture/some_class.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace Rector\Tests\Php81\Rector\Class_\SpatieEnumClassToEnumRector\Fixture;
2121

2222
use Spatie\Enum\Enum;
2323

24-
enum StatusEnum
24+
enum StatusEnum : string
2525
{
2626
case draft = 'draft';
2727
case published = 'published';

rules/Php81/NodeFactory/EnumFactory.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
namespace Rector\Php81\NodeFactory;
66

77
use PhpParser\BuilderFactory;
8+
use PhpParser\Node\Identifier;
89
use PhpParser\Node\Stmt\Class_;
910
use PhpParser\Node\Stmt\ClassConst;
1011
use PhpParser\Node\Stmt\Enum_;
1112
use PhpParser\Node\Stmt\EnumCase;
1213
use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueNode;
1314
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
1415
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
16+
use Rector\Core\PhpParser\Node\Value\ValueResolver;
1517
use Rector\NodeNameResolver\NodeNameResolver;
1618
use Rector\NodeTypeResolver\Node\AttributeKey;
1719

@@ -20,7 +22,8 @@ final class EnumFactory
2022
public function __construct(
2123
private NodeNameResolver $nodeNameResolver,
2224
private PhpDocInfoFactory $phpDocInfoFactory,
23-
private BuilderFactory $builderFactory
25+
private BuilderFactory $builderFactory,
26+
private ValueResolver $valueResolver
2427
) {
2528
}
2629

@@ -29,9 +32,18 @@ public function createFromClass(Class_ $class): Enum_
2932
$shortClassName = $this->nodeNameResolver->getShortName($class);
3033
$enum = new Enum_($shortClassName);
3134

32-
// constant to cases
33-
foreach ($class->getConstants() as $classConst) {
34-
$enum->stmts[] = $this->createEnumCaseFromConst($classConst);
35+
$constants = $class->getConstants();
36+
37+
if ($constants !== []) {
38+
$value = $this->valueResolver->getValue($constants[0]->consts[0]->value);
39+
$enum->scalarType = is_string($value)
40+
? new Identifier('string')
41+
: new Identifier('int');
42+
43+
// constant to cases
44+
foreach ($constants as $constant) {
45+
$enum->stmts[] = $this->createEnumCaseFromConst($constant);
46+
}
3547
}
3648

3749
return $enum;
@@ -47,6 +59,8 @@ public function createFromSpatieClass(Class_ $class): Enum_
4759

4860
$docBlockMethods = $phpDocInfo?->getTagsByName('@method');
4961
if ($docBlockMethods !== null) {
62+
$enum->scalarType = new Identifier('string');
63+
5064
foreach ($docBlockMethods as $docBlockMethod) {
5165
$enum->stmts[] = $this->createEnumCaseFromDocComment($docBlockMethod);
5266
}

rules/Php81/Rector/Class_/MyCLabsClassToEnumRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ final class Action extends Enum
4343

4444
,
4545
<<<'CODE_SAMPLE'
46-
enum Action
46+
enum Action : string
4747
{
4848
case VIEW = 'view';
4949
case EDIT = 'edit';

rules/Php81/Rector/Class_/SpatieEnumClassToEnumRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class StatusEnum extends Enum
5151

5252
,
5353
<<<'CODE_SAMPLE'
54-
enum StatusEnum
54+
enum StatusEnum : string
5555
{
5656
case draft = 'draft';
5757
case published = 'published';

0 commit comments

Comments
 (0)