-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathUseImportsAdder.php
More file actions
274 lines (222 loc) · 9.17 KB
/
UseImportsAdder.php
File metadata and controls
274 lines (222 loc) · 9.17 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
declare(strict_types=1);
namespace Rector\CodingStyle\Application;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Declare_;
use PhpParser\Node\Stmt\GroupUse;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Use_;
use Rector\CodingStyle\ClassNameImport\UsedImportsResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\PhpParser\Node\FileNode;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
final readonly class UseImportsAdder
{
public function __construct(
private UsedImportsResolver $usedImportsResolver,
private TypeFactory $typeFactory
) {
}
/**
* @param Stmt[] $stmts
* @param array<FullyQualifiedObjectType|AliasedObjectType> $useImportTypes
* @param array<FullyQualifiedObjectType|AliasedObjectType> $constantUseImportTypes
* @param array<FullyQualifiedObjectType|AliasedObjectType> $functionUseImportTypes
*/
public function addImportsToStmts(
FileNode $fileNode,
array $stmts,
array $useImportTypes,
array $constantUseImportTypes,
array $functionUseImportTypes
): bool {
$usedImports = $this->usedImportsResolver->resolveForStmts($stmts);
$existingUseImportTypes = $usedImports->getUseImports();
$existingConstantUseImports = $usedImports->getConstantImports();
$existingFunctionUseImports = $usedImports->getFunctionImports();
$useImportTypes = $this->diffFullyQualifiedObjectTypes($useImportTypes, $existingUseImportTypes);
$constantUseImportTypes = $this->diffFullyQualifiedObjectTypes(
$constantUseImportTypes,
$existingConstantUseImports
);
$functionUseImportTypes = $this->diffFullyQualifiedObjectTypes(
$functionUseImportTypes,
$existingFunctionUseImports
);
$newUses = $this->createUses($useImportTypes, $constantUseImportTypes, $functionUseImportTypes, null);
if ($newUses === []) {
return false;
}
$stmts = array_values(array_filter($stmts, static function (Stmt $stmt): bool {
if (! $stmt instanceof Use_) {
return true;
}
return $stmt->uses !== [];
}));
// place after declare strict_types
foreach ($stmts as $key => $stmt) {
// maybe just added a space
if ($stmt instanceof Nop) {
continue;
}
// when we found a non-declare, directly stop
if (! $stmt instanceof Declare_) {
break;
}
$nodesToAdd = array_merge([new Nop()], $newUses);
$this->mirrorUseComments($stmts, $newUses, $key + 1);
// remove space before next use tweak
if (isset($stmts[$key + 1]) && ($stmts[$key + 1] instanceof Use_ || $stmts[$key + 1] instanceof GroupUse)) {
$stmts[$key + 1]->setAttribute(AttributeKey::ORIGINAL_NODE, null);
}
array_splice($stmts, $key + 1, 0, $nodesToAdd);
$fileNode->stmts = $stmts;
$fileNode->stmts = array_values($fileNode->stmts);
return true;
}
$this->mirrorUseComments($stmts, $newUses);
// make use stmts first
$fileNode->stmts = array_merge($newUses, $this->resolveInsertNop($fileNode), $stmts);
$fileNode->stmts = array_values($fileNode->stmts);
return true;
}
/**
* @param FullyQualifiedObjectType[] $useImportTypes
* @param FullyQualifiedObjectType[] $constantUseImportTypes
* @param FullyQualifiedObjectType[] $functionUseImportTypes
*/
public function addImportsToNamespace(
Namespace_ $namespace,
array $useImportTypes,
array $constantUseImportTypes,
array $functionUseImportTypes
): bool {
$namespaceName = $this->getNamespaceName($namespace);
$existingUsedImports = $this->usedImportsResolver->resolveForStmts($namespace->stmts);
$existingUseImportTypes = $existingUsedImports->getUseImports();
$existingConstantUseImportTypes = $existingUsedImports->getConstantImports();
$existingFunctionUseImportTypes = $existingUsedImports->getFunctionImports();
$existingUseImportTypes = $this->typeFactory->uniquateTypes($existingUseImportTypes);
$useImportTypes = $this->diffFullyQualifiedObjectTypes($useImportTypes, $existingUseImportTypes);
$constantUseImportTypes = $this->diffFullyQualifiedObjectTypes(
$constantUseImportTypes,
$existingConstantUseImportTypes
);
$functionUseImportTypes = $this->diffFullyQualifiedObjectTypes(
$functionUseImportTypes,
$existingFunctionUseImportTypes
);
$newUses = $this->createUses($useImportTypes, $constantUseImportTypes, $functionUseImportTypes, $namespaceName);
if ($newUses === []) {
return false;
}
$this->mirrorUseComments($namespace->stmts, $newUses);
$namespace->stmts = array_merge($newUses, $this->resolveInsertNop($namespace), $namespace->stmts);
$namespace->stmts = array_values($namespace->stmts);
return true;
}
/**
* @return Nop[]
*/
private function resolveInsertNop(FileNode|Namespace_ $namespace): array
{
$currentStmt = $namespace->stmts[0] ?? null;
if (! $currentStmt instanceof Stmt || $currentStmt instanceof Use_ || $currentStmt instanceof GroupUse) {
return [];
}
return [new Nop()];
}
/**
* @param Stmt[] $stmts
* @param Use_[] $newUses
*/
private function mirrorUseComments(array $stmts, array $newUses, int $indexStmt = 0): void
{
if ($stmts === []) {
return;
}
if (isset($stmts[$indexStmt]) && $stmts[$indexStmt] instanceof Use_) {
$comments = (array) $stmts[$indexStmt]->getAttribute(AttributeKey::COMMENTS);
if ($comments !== []) {
$newUses[0]->setAttribute(
AttributeKey::COMMENTS,
$stmts[$indexStmt]->getAttribute(AttributeKey::COMMENTS)
);
$stmts[$indexStmt]->setAttribute(AttributeKey::COMMENTS, []);
}
}
}
/**
* @param array<FullyQualifiedObjectType|AliasedObjectType> $mainTypes
* @param array<FullyQualifiedObjectType|AliasedObjectType> $typesToRemove
* @return array<FullyQualifiedObjectType|AliasedObjectType>
*/
private function diffFullyQualifiedObjectTypes(array $mainTypes, array $typesToRemove): array
{
foreach ($mainTypes as $key => $mainType) {
foreach ($typesToRemove as $typeToRemove) {
if ($mainType->equals($typeToRemove)) {
unset($mainTypes[$key]);
}
}
}
return array_values($mainTypes);
}
/**
* @param array<AliasedObjectType|FullyQualifiedObjectType> $useImportTypes
* @param array<FullyQualifiedObjectType|AliasedObjectType> $constantUseImportTypes
* @param array<FullyQualifiedObjectType|AliasedObjectType> $functionUseImportTypes
* @return Use_[]
*/
private function createUses(
array $useImportTypes,
array $constantUseImportTypes,
array $functionUseImportTypes,
?string $namespaceName
): array {
$newUses = [];
/** @var array<Use_::TYPE_*, array<AliasedObjectType|FullyQualifiedObjectType>> $importsMapping */
$importsMapping = [
Use_::TYPE_NORMAL => $useImportTypes,
Use_::TYPE_CONSTANT => $constantUseImportTypes,
Use_::TYPE_FUNCTION => $functionUseImportTypes,
];
foreach ($importsMapping as $type => $importTypes) {
/** @var AliasedObjectType|FullyQualifiedObjectType $importType */
foreach ($importTypes as $importType) {
if ($namespaceName !== null && $this->isCurrentNamespace($namespaceName, $importType)) {
continue;
}
if ($namespaceName === null
&& $importType instanceof FullyQualifiedObjectType
&& substr_count(ltrim($importType->getClassName(), '\\'), '\\') === 0) {
continue;
}
// already imported in previous cycle
$newUses[] = $importType->getUseNode($type);
}
}
return $newUses;
}
private function getNamespaceName(Namespace_ $namespace): ?string
{
if (! $namespace->name instanceof Name) {
return null;
}
return $namespace->name->toString();
}
private function isCurrentNamespace(
string $namespaceName,
AliasedObjectType|FullyQualifiedObjectType $objectType
): bool {
$className = $objectType->getClassName();
if (! str_starts_with($className, $namespaceName . '\\')) {
return false;
}
return $namespaceName . '\\' . $objectType->getShortName() === $className;
}
}