Skip to content

Commit 8949a7f

Browse files
committed
add FileNode upgrade to UPGRADING
1 parent 45fc85f commit 8949a7f

File tree

1 file changed

+59
-6
lines changed

1 file changed

+59
-6
lines changed

UPGRADING.md

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,73 @@
11
# Upgrading from Rector 2.2.11 to 2.2.12
22

3+
* `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
35

4-
@todo
6+
**Before**
57

6-
* FileWithoutNamespace is deprecated, and replaced by `FileNode`
7-
* `beforeTraverse()` is @final, use getNodeTypes() with `FileNode::class` instead
8+
```php
9+
use Rector\PhpParser\Node\FileWithoutNamespace;
10+
use Rector\Rector\AbstractRector;
811

9-
**Before**
12+
final class SomeRector extends AbstractRector
13+
{
14+
public function getNodeTypes(): array
15+
{
16+
return [FileWithoutNamespace::class];
17+
}
1018

11-
@todo
19+
public function beforeTraverse(array $nodes): array
20+
{
21+
// some node hacking
22+
}
1223

24+
/**
25+
* @param FileWithoutNamespace $node
26+
*/
27+
public function refactor(Node $node): ?Node
28+
{
29+
// ...
30+
}
31+
32+
}
33+
```
1334

1435
**After**
1536

16-
@todo
37+
```php
38+
use Rector\PhpParser\Node\FileNode;
39+
use Rector\Rector\AbstractRector;
40+
41+
final class SomeRector extends AbstractRector
42+
{
43+
public function getNodeTypes(): array
44+
{
45+
return [FileNode::class];
46+
}
47+
48+
/**
49+
* @param FileNode $node
50+
*/
51+
public function refactor(Node $node): ?Node
52+
{
53+
foreach ($node->stmts as $stmt) {
54+
// check if has declare_strict already?
55+
// ...
56+
57+
// create it
58+
$declareStrictTypes = $this->createDeclareStrictTypesNode();
1759

60+
// add it
61+
$node->stmts = array_merge([$declareStrictTypes], $node->stmts);
62+
}
63+
64+
return $node;
65+
}
66+
67+
}
68+
```
69+
70+
<br>
1871

1972
# Upgrading from Rector 1.x to 2.0
2073

0 commit comments

Comments
 (0)