File tree Expand file tree Collapse file tree 4 files changed +33
-24
lines changed
PhpParser/Node/CustomNode Expand file tree Collapse file tree 4 files changed +33
-24
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 >
Original file line number Diff line number Diff line change 1717 // ensure use version ^3.2.0
1818 ->ignoreErrorsOnPackage ('composer/pcre ' , [ErrorType::UNUSED_DEPENDENCY ])
1919
20- ->ignoreErrorsOnPath (__DIR__ . '/src/Reporting/DeprecatedRulesReporter.php ' , [ErrorType::UNKNOWN_CLASS ])
21-
2220 ->ignoreErrorsOnPaths ([
2321 __DIR__ . '/stubs ' ,
2422 __DIR__ . '/tests ' ,
Original file line number Diff line number Diff line change 44
55namespace Rector \PhpParser \Node \CustomNode ;
66
7- use PhpParser \Node \Stmt ;
87use Rector \Contract \PhpParser \Node \StmtsAwareInterface ;
98use Rector \PhpParser \Node \FileNode ;
109
1615 */
1716final class FileWithoutNamespace extends FileNode implements StmtsAwareInterface
1817{
19- /**
20- * @param Stmt[] $stmts
21- */
22- public function __construct (array $ stmts )
23- {
24- parent ::__construct ($ stmts );
25- }
26-
2718 public function getType (): string
2819 {
2920 return 'FileWithoutNamespace ' ;
Original file line number Diff line number Diff line change 1010use Rector \Contract \PhpParser \Node \StmtsAwareInterface ;
1111use Rector \Contract \Rector \RectorInterface ;
1212use Rector \PhpParser \Enum \NodeGroup ;
13- <<<<<<< HEAD
14- <<<<<<< HEAD
15- use Rector \PhpParserNode \FileNode;
16- =======
17- <<<<<<< HEAD
18- >>>>>>> 6 bcee47e76 (warn about deprecated type)
19- use ReflectionMethod;
20- =======
21- =======
22- >>>>>>> 00 c8276150 (print stmts with false )
2313use Rector \PhpParser \Node \CustomNode \FileWithoutNamespace ;
2414use Rector \PhpParser \Node \FileNode ;
2515use ReflectionMethod ;
You can’t perform that action at this time.
0 commit comments