1212use PHPStan \PhpDocParser \Ast \PhpDoc \MethodTagValueNode ;
1313use PHPStan \PhpDocParser \Ast \PhpDoc \PhpDocTagNode ;
1414use Rector \BetterPhpDocParser \PhpDocInfo \PhpDocInfoFactory ;
15+ use Rector \Core \PhpParser \Node \Value \ValueResolver ;
1516use Rector \NodeNameResolver \NodeNameResolver ;
1617use 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 }
0 commit comments