-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
ToNativeImplementationRector.php
143 lines (124 loc) · 4.43 KB
/
ToNativeImplementationRector.php
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php declare(strict_types=1);
namespace BenSampo\Enum\Rector;
use BenSampo\Enum\Enum;
use BenSampo\Enum\Tests\Enums\UserType;
use PhpParser\Comment\Doc;
use PhpParser\Node;
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\ExtendsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueNode;
use PHPStan\Type\ObjectType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\Printer\PhpDocInfoPrinter;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\Value\ValueResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/** @see \BenSampo\Enum\Tests\Rector\ToNativeRectorImplementationTest */
class ToNativeImplementationRector extends ToNativeRector
{
public function __construct(
protected PhpDocInfoPrinter $phpDocInfoPrinter,
protected PhpDocInfoFactory $phpDocInfoFactory,
protected ValueResolver $valueResolver,
) {
parent::__construct($valueResolver);
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Convert usages of BenSampo\Enum\Enum to native PHP enums', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
/**
* @method static static ADMIN()
* @method static static MEMBER()
*
* @extends Enum<int>
*/
class UserType extends Enum
{
const ADMIN = 1;
const MEMBER = 2;
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
enum UserType : int
{
case ADMIN = 1;
case MEMBER = 2;
}
CODE_SAMPLE,
[
UserType::class,
],
),
]);
}
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @see \Rector\Php81\NodeFactory\EnumFactory
*
* @param Class_ $class
*/
public function refactor(Node $class): ?Node
{
$this->classes ??= [new ObjectType(Enum::class)];
if (! $this->inConfiguredClasses($class)) {
return null;
}
$enum = new Enum_(
$this->nodeNameResolver->getShortName($class),
[],
['startLine' => $class->getStartLine(), 'endLine' => $class->getEndLine()]
);
$enum->namespacedName = $class->namespacedName;
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($class);
if ($phpDocInfo) {
$phpDocInfo->removeByType(MethodTagValueNode::class);
$phpDocInfo->removeByType(ExtendsTagValueNode::class);
$phpdoc = $this->phpDocInfoPrinter->printFormatPreserving($phpDocInfo);
// By removing unnecessary tags, we are usually left with a couple of redundant newlines.
// There might be valuable ones to keep in long descriptions which will unfortunately
// also be removed, but this should be less common.
$withoutEmptyNewlines = preg_replace('/ \*\n/', '', $phpdoc);
if ($withoutEmptyNewlines) {
$enum->setDocComment(new Doc($withoutEmptyNewlines));
}
}
$enum->stmts = $class->getTraitUses();
$constants = $class->getConstants();
$constantValues = array_map(
fn (ClassConst $classConst): mixed => $this->valueResolver->getValue(
$classConst->consts[0]->value
),
$constants
);
$enumScalarType = $this->enumScalarType($constantValues);
if ($enumScalarType) {
$enum->scalarType = new Identifier($enumScalarType);
}
foreach ($constants as $constant) {
$constConst = $constant->consts[0];
$enumCase = new EnumCase(
$constConst->name,
$constConst->value,
[],
['startLine' => $constConst->getStartLine(), 'endLine' => $constConst->getEndLine()],
);
// mirror comments
$enumCase->setAttribute(AttributeKey::PHP_DOC_INFO, $constant->getAttribute(AttributeKey::PHP_DOC_INFO));
$enumCase->setAttribute(AttributeKey::COMMENTS, $constant->getAttribute(AttributeKey::COMMENTS));
$enum->stmts[] = $enumCase;
}
$enum->stmts = [...$enum->stmts, ...$class->getMethods()];
return $enum;
}
}