Skip to content

Commit f6068d7

Browse files
committed
Fixed 🎉
1 parent 3d11447 commit f6068d7

7 files changed

Lines changed: 41 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: string
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: string
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: string
24+
enum StatusEnum : string
2525
{
2626
case draft = 'draft';
2727
case published = 'published';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;
4+
5+
final class SkipHasDefaultValueParam
6+
{
7+
public function __construct(
8+
private array $data = []
9+
) {
10+
}
11+
12+
public function getData()
13+
{
14+
return $this->data;
15+
}
16+
}

rules/Php81/NodeFactory/EnumFactory.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueNode;
1313
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
1414
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
15+
use Rector\Core\PhpParser\Node\Value\ValueResolver;
1516
use Rector\NodeNameResolver\NodeNameResolver;
1617
use Rector\NodeTypeResolver\Node\AttributeKey;
1718

@@ -20,7 +21,8 @@ final class EnumFactory
2021
public function __construct(
2122
private NodeNameResolver $nodeNameResolver,
2223
private PhpDocInfoFactory $phpDocInfoFactory,
23-
private BuilderFactory $builderFactory
24+
private BuilderFactory $builderFactory,
25+
private ValueResolver $valueResolver
2426
) {
2527
}
2628

@@ -29,9 +31,18 @@ public function createFromClass(Class_ $class): Enum_
2931
$shortClassName = $this->nodeNameResolver->getShortName($class);
3032
$enum = new Enum_($shortClassName);
3133

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

3748
return $enum;
@@ -47,6 +58,11 @@ public function createFromSpatieClass(Class_ $class): Enum_
4758

4859
$docBlockMethods = $phpDocInfo?->getTagsByName('@method');
4960
if ($docBlockMethods !== null) {
61+
$value = $docBlockMethods[0]->value->methodName;
62+
$enum->scalarType = is_string($value)
63+
? 'string'
64+
: 'int';
65+
5066
foreach ($docBlockMethods as $docBlockMethod) {
5167
$enum->stmts[] = $this->createEnumCaseFromDocComment($docBlockMethod);
5268
}

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: string
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: string
54+
enum StatusEnum : string
5555
{
5656
case draft = 'draft';
5757
case published = 'published';

0 commit comments

Comments
 (0)