File tree Expand file tree Collapse file tree 1 file changed +33
-3
lines changed
Expand file tree Collapse file tree 1 file changed +33
-3
lines changed Original file line number Diff line number Diff line change 11# Upgrading from Rector 2.2.14 to 2.3
22
33* ` FileWithoutNamespace ` is deprecated, and replaced by ` FileNode ` that represents both namespaced and non-namespaced files and allow changes inside
4- * ` beforeTraverse() ` is now soft marked as ` @final ` , use ` getNodeTypes() ` with ` FileNode::class ` instead
4+ * ` beforeTraverse() ` is now marked as ` @final ` , use ` getNodeTypes() ` with ` FileNode::class ` instead
55
66** Before**
77
@@ -67,10 +67,40 @@ final class SomeRector extends AbstractRector
6767}
6868```
6969
70- The ` FileNode ` handles both namespaced and non-namespaced files. To check if the file is namespaced, use:
70+ <br >
71+
72+ The ` FileNode ` handles both namespaced and non-namespaced files. To handle the first stmts inside the file, you hook into 2 nodes:
7173
7274``` php
73- $fileNode->isNamespaced();
75+ use Rector\PhpParser\Node\FileNode;
76+ use Rector\Rector\AbstractRector;
77+ use PhpParser\Node\Stmt\Namespace_;
78+
79+ final class SomeRector extends AbstractRector
80+ {
81+ public function getNodeTypes(): array
82+ {
83+ return [FileNode::class, Namespace_::class];
84+ }
85+
86+ /**
87+ * @param FileNode|Namespace_ $node
88+ */
89+ public function refactor(Node $node): ?Node
90+ {
91+ if ($node instanceof FileNode && $node->isNamespaced()) {
92+ // handled in the Namespace_ node
93+ return null;
94+ }
95+
96+ foreach ($node->stmts as $stmt) {
97+ // modify stmts in desired way here
98+ }
99+
100+ return $node;
101+ }
102+
103+ }
74104```
75105
76106<br >
You can’t perform that action at this time.
0 commit comments