-
-
Notifications
You must be signed in to change notification settings - Fork 441
Expand file tree
/
Copy pathEnumFactory.php
More file actions
90 lines (72 loc) · 2.92 KB
/
Copy pathEnumFactory.php
File metadata and controls
90 lines (72 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
declare(strict_types=1);
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;
final class EnumFactory
{
public function __construct(
private NodeNameResolver $nodeNameResolver,
private PhpDocInfoFactory $phpDocInfoFactory,
private BuilderFactory $builderFactory,
private ValueResolver $valueResolver
) {
}
public function createFromClass(Class_ $class): Enum_
{
$shortClassName = $this->nodeNameResolver->getShortName($class);
$enum = new Enum_($shortClassName);
$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;
}
public function createFromSpatieClass(Class_ $class): Enum_
{
$shortClassName = $this->nodeNameResolver->getShortName($class);
$enum = new Enum_($shortClassName);
// constant to cases
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($class);
$docBlockMethods = $phpDocInfo?->getTagsByName('@method');
if ($docBlockMethods !== null) {
$enum->scalarType = new Identifier('string');
foreach ($docBlockMethods as $docBlockMethod) {
$enum->stmts[] = $this->createEnumCaseFromDocComment($docBlockMethod);
}
}
return $enum;
}
private function createEnumCaseFromConst(ClassConst $classConst): EnumCase
{
$constConst = $classConst->consts[0];
$enumCase = new EnumCase($constConst->name, $constConst->value);
// mirror comments
$enumCase->setAttribute(AttributeKey::PHP_DOC_INFO, $classConst->getAttribute(AttributeKey::PHP_DOC_INFO));
$enumCase->setAttribute(AttributeKey::COMMENTS, $classConst->getAttribute(AttributeKey::COMMENTS));
return $enumCase;
}
private function createEnumCaseFromDocComment(PhpDocTagNode $phpDocTagNode): EnumCase
{
/** @var MethodTagValueNode $nodeValue */
$nodeValue = $phpDocTagNode->value;
return new EnumCase($nodeValue->methodName, $this->builderFactory->val($nodeValue->methodName));
}
}