From 3d1144777c044740304cfbc7976f0dcfeafba16c Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 2 Dec 2021 12:31:54 +0700 Subject: [PATCH 1/4] [Php81] Add native type on Enum MyCLabsClassToEnumRector and SpatieEnumClassToEnumRector --- .../Fixture/private_constant_with_static_method.php.inc | 2 +- .../Class_/MyCLabsClassToEnumRector/Fixture/some_class.php.inc | 2 +- .../SpatieEnumClassToEnumRector/Fixture/some_class.php.inc | 2 +- rules/Php81/Rector/Class_/MyCLabsClassToEnumRector.php | 2 +- rules/Php81/Rector/Class_/SpatieEnumClassToEnumRector.php | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) 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..011e0f223c7 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..db9dc2bcce9 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..420a7876e81 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/Rector/Class_/MyCLabsClassToEnumRector.php b/rules/Php81/Rector/Class_/MyCLabsClassToEnumRector.php index ae0fc3ebd76..457701c45ec 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..d37a6397aec 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'; From f6068d73508ee25441b912e80a20ac6cfe9aebc1 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 2 Dec 2021 12:49:03 +0700 Subject: [PATCH 2/4] Fixed :tada: --- ...rivate_constant_with_static_method.php.inc | 2 +- .../Fixture/some_class.php.inc | 2 +- .../Fixture/some_class.php.inc | 2 +- .../skip_has_default_value_param.php.inc | 16 +++++++++++++ rules/Php81/NodeFactory/EnumFactory.php | 24 +++++++++++++++---- .../Class_/MyCLabsClassToEnumRector.php | 2 +- .../Class_/SpatieEnumClassToEnumRector.php | 2 +- 7 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 rules-tests/Php81/Rector/Property/ReadOnlyPropertyRector/Fixture/skip_has_default_value_param.php.inc 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 011e0f223c7..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: string +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 db9dc2bcce9..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: string +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 420a7876e81..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: string +enum StatusEnum : string { case draft = 'draft'; case published = 'published'; diff --git a/rules-tests/Php81/Rector/Property/ReadOnlyPropertyRector/Fixture/skip_has_default_value_param.php.inc b/rules-tests/Php81/Rector/Property/ReadOnlyPropertyRector/Fixture/skip_has_default_value_param.php.inc new file mode 100644 index 00000000000..a24a22e86ef --- /dev/null +++ b/rules-tests/Php81/Rector/Property/ReadOnlyPropertyRector/Fixture/skip_has_default_value_param.php.inc @@ -0,0 +1,16 @@ +data; + } +} \ No newline at end of file diff --git a/rules/Php81/NodeFactory/EnumFactory.php b/rules/Php81/NodeFactory/EnumFactory.php index 280e6dd5470..f7936238590 100644 --- a/rules/Php81/NodeFactory/EnumFactory.php +++ b/rules/Php81/NodeFactory/EnumFactory.php @@ -12,6 +12,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 +21,8 @@ final class EnumFactory public function __construct( private NodeNameResolver $nodeNameResolver, private PhpDocInfoFactory $phpDocInfoFactory, - private BuilderFactory $builderFactory + private BuilderFactory $builderFactory, + private ValueResolver $valueResolver ) { } @@ -29,9 +31,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) + ? 'string' + : 'int'; + + // constant to cases + foreach ($constants as $classConst) { + $enum->stmts[] = $this->createEnumCaseFromConst($classConst); + } } return $enum; @@ -47,6 +58,11 @@ public function createFromSpatieClass(Class_ $class): Enum_ $docBlockMethods = $phpDocInfo?->getTagsByName('@method'); if ($docBlockMethods !== null) { + $value = $docBlockMethods[0]->value->methodName; + $enum->scalarType = is_string($value) + ? 'string' + : 'int'; + 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 457701c45ec..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: string +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 d37a6397aec..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: string +enum StatusEnum : string { case draft = 'draft'; case published = 'published'; From e626e5053fcd131481eb4d4b01a7353ee9c442af Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 2 Dec 2021 12:50:12 +0700 Subject: [PATCH 3/4] clean up --- .../Fixture/skip_has_default_value_param.php.inc | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 rules-tests/Php81/Rector/Property/ReadOnlyPropertyRector/Fixture/skip_has_default_value_param.php.inc diff --git a/rules-tests/Php81/Rector/Property/ReadOnlyPropertyRector/Fixture/skip_has_default_value_param.php.inc b/rules-tests/Php81/Rector/Property/ReadOnlyPropertyRector/Fixture/skip_has_default_value_param.php.inc deleted file mode 100644 index a24a22e86ef..00000000000 --- a/rules-tests/Php81/Rector/Property/ReadOnlyPropertyRector/Fixture/skip_has_default_value_param.php.inc +++ /dev/null @@ -1,16 +0,0 @@ -data; - } -} \ No newline at end of file From 85f58ddd7351c94da2ee624f907042dc97e2e8fb Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 2 Dec 2021 12:55:58 +0700 Subject: [PATCH 4/4] phpstan --- rules/Php81/NodeFactory/EnumFactory.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/rules/Php81/NodeFactory/EnumFactory.php b/rules/Php81/NodeFactory/EnumFactory.php index f7936238590..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_; @@ -36,12 +37,12 @@ public function createFromClass(Class_ $class): Enum_ if ($constants !== []) { $value = $this->valueResolver->getValue($constants[0]->consts[0]->value); $enum->scalarType = is_string($value) - ? 'string' - : 'int'; + ? new Identifier('string') + : new Identifier('int'); // constant to cases - foreach ($constants as $classConst) { - $enum->stmts[] = $this->createEnumCaseFromConst($classConst); + foreach ($constants as $constant) { + $enum->stmts[] = $this->createEnumCaseFromConst($constant); } } @@ -58,10 +59,7 @@ public function createFromSpatieClass(Class_ $class): Enum_ $docBlockMethods = $phpDocInfo?->getTagsByName('@method'); if ($docBlockMethods !== null) { - $value = $docBlockMethods[0]->value->methodName; - $enum->scalarType = is_string($value) - ? 'string' - : 'int'; + $enum->scalarType = new Identifier('string'); foreach ($docBlockMethods as $docBlockMethod) { $enum->stmts[] = $this->createEnumCaseFromDocComment($docBlockMethod);