Skip to content

Commit 3d11d5d

Browse files
authored
Merge pull request #373 from samsonasik/bump-rector-230
Bump to rector ~2.3.0 and migrate deprecated FileWithoutNamespace to FileNode
2 parents 11d44d4 + 6dace16 commit 3d11d5d

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"php": "^8.1",
99
"cakephp/console": "^5.0",
1010
"nette/utils": "^4.0",
11-
"rector/rector": "~2.2.9",
11+
"rector/rector": "~2.3.0",
1212
"symfony/string": "^6.0 || ^7.0"
1313
},
1414
"autoload": {

src/Rector/Rector/Namespace_/AppUsesStaticCallToUseStatementRector.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use PHPStan\Type\ObjectType;
1717
use Rector\PhpParser\Enum\NodeGroup;
1818
use Rector\PhpParser\Node\BetterNodeFinder;
19-
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
19+
use Rector\PhpParser\Node\FileNode;
2020
use Rector\PhpParser\Node\Value\ValueResolver;
2121
use Rector\Rector\AbstractRector;
2222
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -86,9 +86,11 @@ public function refactor(Node $node): ?Node
8686

8787
if ($node instanceof Namespace_) {
8888
$node->stmts = array_merge($uses, $node->stmts);
89+
90+
return $node;
8991
}
9092

91-
if ($node instanceof FileWithoutNamespace) {
93+
if ($node instanceof FileNode) {
9294
$this->refactorFile($node, $uses);
9395
}
9496

@@ -174,18 +176,22 @@ private function resolveNamesFromStaticCalls(array $staticCalls): array
174176
}
175177

176178
/**
177-
* @param array<\PhpParser\Node\Stmt\Use_> $fileWithoutNamespace
179+
* @param array<\PhpParser\Node\Stmt\Use_> $uses
178180
*/
179-
private function refactorFile(FileWithoutNamespace $fileWithoutNamespace, array $uses): ?FileWithoutNamespace
181+
private function refactorFile(FileNode $fileNode, array $uses): ?FileNode
180182
{
181-
$hasDeclare = $this->betterNodeFinder->findFirstInstanceOf($fileWithoutNamespace->stmts, Declare_::class);
183+
if ($fileNode->isNamespaced()) {
184+
return null;
185+
}
186+
187+
$hasDeclare = $this->betterNodeFinder->findFirstInstanceOf($fileNode->stmts, Declare_::class);
182188
if ($hasDeclare !== null) {
183-
return $this->refactorFileWithDeclare($fileWithoutNamespace, $uses);
189+
return $this->refactorFileWithDeclare($fileNode, $uses);
184190
}
185191

186-
$fileWithoutNamespace->stmts = array_merge($uses, $fileWithoutNamespace->stmts);
192+
$fileNode->stmts = array_merge($uses, $fileNode->stmts);
187193

188-
return $fileWithoutNamespace;
194+
return $fileNode;
189195
}
190196

191197
private function createFullyQualifiedNameFromAppUsesStaticCall(StaticCall $staticCall): string
@@ -203,20 +209,20 @@ private function createFullyQualifiedNameFromAppUsesStaticCall(StaticCall $stati
203209
}
204210

205211
/**
206-
* @param array<\PhpParser\Node\Stmt\Use_> $fileWithoutNamespace
212+
* @param array<\PhpParser\Node\Stmt\Use_> $uses
207213
*/
208214
private function refactorFileWithDeclare(
209-
FileWithoutNamespace $fileWithoutNamespace,
215+
FileNode $fileNode,
210216
array $uses,
211-
): FileWithoutNamespace {
212-
foreach ($fileWithoutNamespace->stmts as $key => $stmt) {
217+
): FileNode {
218+
foreach ($fileNode->stmts as $key => $stmt) {
213219
if ($stmt instanceof Declare_) {
214220
foreach ($uses as $use) {
215-
array_splice($fileWithoutNamespace->stmts, $key + 1, 0, [$use]);
221+
array_splice($fileNode->stmts, $key + 1, 0, [$use]);
216222
}
217223
}
218224
}
219225

220-
return $fileWithoutNamespace;
226+
return $fileNode;
221227
}
222228
}

0 commit comments

Comments
 (0)