Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Rector\Tests\Php81\Rector\Class_\MyCLabsClassToEnumRector\Fixture;

use MyCLabs\Enum\Enum;

enum PrivateConstWithStaticMethod
enum PrivateConstWithStaticMethod : string
{
/**
* Some comment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Rector\Tests\Php81\Rector\Class_\MyCLabsClassToEnumRector\Fixture;

use MyCLabs\Enum\Enum;

enum SomeClass
enum SomeClass : string
{
/**
* Some comment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
22 changes: 18 additions & 4 deletions rules/Php81/NodeFactory/EnumFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
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_;
use PhpParser\Node\Stmt\EnumCase;
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;

Expand All @@ -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
) {
}

Expand All @@ -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;
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion rules/Php81/Rector/Class_/MyCLabsClassToEnumRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class Action extends Enum

,
<<<'CODE_SAMPLE'
enum Action
enum Action : string
{
case VIEW = 'view';
case EDIT = 'edit';
Expand Down
2 changes: 1 addition & 1 deletion rules/Php81/Rector/Class_/SpatieEnumClassToEnumRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class StatusEnum extends Enum

,
<<<'CODE_SAMPLE'
enum StatusEnum
enum StatusEnum : string
{
case draft = 'draft';
case published = 'published';
Expand Down