Skip to content

Commit ff771b0

Browse files
committed
[docs] add specific example how to iterate stmts of namespaced and non-namespaced file
1 parent fbd4e4c commit ff771b0

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed

UPGRADING.md

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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>

composer-dependency-analyser.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
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',

src/PhpParser/Node/CustomNode/FileWithoutNamespace.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Rector\PhpParser\Node\CustomNode;
66

7-
use PhpParser\Node\Stmt;
87
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
98
use Rector\PhpParser\Node\FileNode;
109

@@ -16,14 +15,6 @@
1615
*/
1716
final 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';

src/Reporting/DeprecatedRulesReporter.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@
1010
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
1111
use Rector\Contract\Rector\RectorInterface;
1212
use Rector\PhpParser\Enum\NodeGroup;
13-
<<<<<<< HEAD
14-
<<<<<<< HEAD
15-
use Rector\PhpParserNode\FileNode;
16-
=======
17-
<<<<<<< HEAD
18-
>>>>>>> 6bcee47e76 (warn about deprecated type)
19-
use ReflectionMethod;
20-
=======
21-
=======
22-
>>>>>>> 00c8276150 (print stmts with false)
2313
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
2414
use Rector\PhpParser\Node\FileNode;
2515
use ReflectionMethod;

0 commit comments

Comments
 (0)