1616use PHPStan \Type \ObjectType ;
1717use Rector \PhpParser \Enum \NodeGroup ;
1818use Rector \PhpParser \Node \BetterNodeFinder ;
19- use Rector \PhpParser \Node \CustomNode \ FileWithoutNamespace ;
19+ use Rector \PhpParser \Node \FileNode ;
2020use Rector \PhpParser \Node \Value \ValueResolver ;
2121use Rector \Rector \AbstractRector ;
2222use 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