-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathEnumCaseToPascalCaseRector.php
More file actions
218 lines (180 loc) · 6.48 KB
/
EnumCaseToPascalCaseRector.php
File metadata and controls
218 lines (180 loc) · 6.48 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
declare(strict_types=1);
namespace Rector\CodingStyle\Rector\Enum_;
use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Enum_;
use PhpParser\Node\Stmt\EnumCase;
use PHPStan\BetterReflection\Reflector\DefaultReflector;
use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound;
use PHPStan\Reflection\ClassReflection;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
use Rector\Rector\AbstractRector;
use Rector\Skipper\FileSystem\PathNormalizer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodingStyle\Rector\Enum_\EnumCaseToPascalCaseRector\EnumCaseToPascalCaseRectorTest
* @see \Rector\Tests\CodingStyle\Rector\Enum_\EnumCaseToPascalCaseRector\WithAutoloadPathsTest
*/
final class EnumCaseToPascalCaseRector extends AbstractRector
{
public function __construct(
private readonly DynamicSourceLocatorProvider $dynamicSourceLocatorProvider,
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Convert enum cases to PascalCase and update their usages',
[
new CodeSample(
<<<'CODE_SAMPLE'
enum Status
{
case PENDING;
case published;
case IN_REVIEW;
case waiting_for_approval;
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
enum Status
{
case Pending;
case Published;
case InReview;
case WaitingForApproval;
}
CODE_SAMPLE
),
]
);
}
public function getNodeTypes(): array
{
return [Enum_::class, ClassConstFetch::class];
}
/**
* @param Enum_|ClassConstFetch $node
*/
public function refactor(Node $node): ?Node
{
if ($node instanceof Enum_) {
return $this->refactorEnum($node);
}
if ($node instanceof ClassConstFetch) {
return $this->refactorClassConstFetch($node);
}
return null;
}
public function refactorEnum(Enum_ $enum): Enum_|null
{
$enumName = $this->getName($enum);
if ($enumName === null) {
return null;
}
$hasChanged = false;
foreach ($enum->stmts as $stmt) {
if (! $stmt instanceof EnumCase) {
continue;
}
$currentName = $stmt->name->toString();
$pascalCaseName = $this->convertToPascalCase($currentName);
if ($currentName === $pascalCaseName) {
continue;
}
$stmt->name = new Identifier($pascalCaseName);
$hasChanged = true;
}
return $hasChanged ? $enum : null;
}
private function refactorClassConstFetch(ClassConstFetch $classConstFetch): ?Node
{
if (! $classConstFetch->class instanceof Name) {
return null;
}
if (! $classConstFetch->name instanceof Identifier) {
return null;
}
$constName = $classConstFetch->name->toString();
$pascalCaseName = $this->convertToPascalCase($constName);
// short circuit if already in pascal case
if ($constName === $pascalCaseName) {
return null;
}
$classReflection = $this->nodeTypeResolver->getType($classConstFetch->class)
->getObjectClassReflections()[0] ?? null;
if ($classReflection === null || ! $classReflection->isEnum()) {
return null;
}
if (! $this->isEnumCase($classReflection, $constName, $pascalCaseName)) {
return null;
}
if ($this->isUsedOutsideOfProject($classConstFetch->class)) {
return null;
}
$classConstFetch->name = new Identifier($pascalCaseName);
return $classConstFetch;
}
private function isUsedOutsideOfProject(Name $name): bool
{
if (in_array($name->toString(), ['self', 'static'], true)) {
return false;
}
$sourceLocator = $this->dynamicSourceLocatorProvider->provide();
$defaultReflector = new DefaultReflector($sourceLocator);
try {
$classIdentifier = $defaultReflector->reflectClass($name->toString());
} catch (IdentifierNotFound) {
// source is outside the paths defined in withPaths(), eg: vendor
return true;
}
$fileTarget = $classIdentifier->getFileName();
// possibly native
if ($fileTarget === null) {
return true;
}
$autoloadPaths = SimpleParameterProvider::provideArrayParameter(Option::AUTOLOAD_PATHS);
$normalizedFileTarget = PathNormalizer::normalize((string) realpath($fileTarget));
foreach ($autoloadPaths as $autoloadPath) {
$normalizedAutoloadPath = PathNormalizer::normalize($autoloadPath);
if ($autoloadPath === $fileTarget) {
return true;
}
if (str_starts_with($normalizedFileTarget, $normalizedAutoloadPath . '/')) {
return true;
}
}
return false;
}
private function isEnumCase(ClassReflection $classReflection, string $name, string $pascalName): bool
{
// the enum case might have already been renamed, need to check both
if ($classReflection->hasEnumCase($name)) {
return true;
}
return $classReflection->hasEnumCase($pascalName);
}
private function convertToPascalCase(string $name): string
{
$parts = explode('_', $name);
return implode(
'',
array_map(
fn ($part): string =>
// If part is all uppercase, convert to ucfirst(strtolower())
// If part is already mixed or PascalCase, keep as is except ucfirst
ctype_upper($part)
? ucfirst(strtolower($part))
: ucfirst($part),
$parts
)
);
}
}