|
1 | 1 | # Upgrading from Rector 2.2.11 to 2.2.12 |
2 | 2 |
|
| 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 |
3 | 5 |
|
4 | | -@todo |
| 6 | +**Before** |
5 | 7 |
|
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; |
8 | 11 |
|
9 | | -**Before** |
| 12 | +final class SomeRector extends AbstractRector |
| 13 | +{ |
| 14 | + public function getNodeTypes(): array |
| 15 | + { |
| 16 | + return [FileWithoutNamespace::class]; |
| 17 | + } |
10 | 18 |
|
11 | | -@todo |
| 19 | + public function beforeTraverse(array $nodes): array |
| 20 | + { |
| 21 | + // some node hacking |
| 22 | + } |
12 | 23 |
|
| 24 | + /** |
| 25 | + * @param FileWithoutNamespace $node |
| 26 | + */ |
| 27 | + public function refactor(Node $node): ?Node |
| 28 | + { |
| 29 | + // ... |
| 30 | + } |
| 31 | + |
| 32 | +} |
| 33 | +``` |
13 | 34 |
|
14 | 35 | **After** |
15 | 36 |
|
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(); |
17 | 59 |
|
| 60 | + // add it |
| 61 | + $node->stmts = array_merge([$declareStrictTypes], $node->stmts); |
| 62 | + } |
| 63 | + |
| 64 | + return $node; |
| 65 | + } |
| 66 | + |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +<br> |
18 | 71 |
|
19 | 72 | # Upgrading from Rector 1.x to 2.0 |
20 | 73 |
|
|
0 commit comments