Skip to content

Commit 045b2ca

Browse files
committed
docs
1 parent 578c8a4 commit 045b2ca

File tree

7 files changed

+30
-26
lines changed

7 files changed

+30
-26
lines changed

UPGRADING.md

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

33

4+
@todo
5+
6+
* FileWithoutNamespace is deprecated, and replaced by `FileNode`
7+
* `beforeTraverse()` is @final, use getNodeTypes() with `FileNode::class` instead
8+
9+
**Before**
10+
11+
@todo
12+
13+
14+
**After**
15+
416
@todo
517

618

rules/CodingStyle/ClassNameImport/ShortNameResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function resolveShortClassLikeNames(File $file): array
6666
$rootNode = $file->getUseImportsRootNode();
6767

6868
// nothing to resolve
69-
if (! $rootNode instanceof \PhpParser\Node) {
69+
if (! $rootNode instanceof Node) {
7070
return [];
7171
}
7272

src/Application/FileProcessor.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -140,27 +140,14 @@ private function parseFileAndDecorateNodes(File $file): ?SystemError
140140
private function printFile(File $file, Configuration $configuration, string $filePath): void
141141
{
142142
// only save to string first, no need to print to file when not needed
143-
$newStmts = $file->getNewStmts();
144-
145-
$oldStmts = $file->getOldStmts();
146-
147-
// unwrap FileNode stmts to allow printing
148-
if ($newStmts[0] instanceof FileNode) {
149-
$newStmts = $newStmts[0]->stmts;
150-
}
151-
152-
if ($oldStmts[0] instanceof FileNode) {
153-
$oldStmts = $oldStmts[0]->stmts;
154-
}
155-
156-
$newContent = $this->betterStandardPrinter->printFormatPreserving(
157-
$newStmts,
158-
$oldStmts,
143+
$newFileContent = $this->betterStandardPrinter->printFormatPreserving(
144+
$file->getNewStmts(),
145+
$file->getOldStmts(),
159146
$file->getOldTokens()
160147
);
161148

162149
// change file content early to make $file->hasChanged() based on new content
163-
$file->changeFileContent($newContent);
150+
$file->changeFileContent($newFileContent);
164151
if ($configuration->isDryRun()) {
165152
return;
166153
}
@@ -169,7 +156,7 @@ private function printFile(File $file, Configuration $configuration, string $fil
169156
return;
170157
}
171158

172-
FileSystem::write($filePath, $newContent, null);
159+
FileSystem::write($filePath, $newFileContent, null);
173160
}
174161

175162
private function parseFileNodes(File $file, bool $forNewestSupportedVersion = true): void
@@ -184,6 +171,7 @@ private function parseFileNodes(File $file, bool $forNewestSupportedVersion = tr
184171

185172
// wrap in FileNode to allow file-level rules
186173
$oldStmts = [new FileNode($oldStmts)];
174+
187175
$oldTokens = $stmtsAndTokens->getTokens();
188176

189177
$newStmts = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($file->getFilePath(), $oldStmts);

src/PhpParser/Node/FileNode.php

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

55
namespace Rector\PhpParser\Node;
66

7+
use PhpParser\Node;
78
use PhpParser\Node\Stmt;
89
use PhpParser\Node\Stmt\GroupUse;
910
use PhpParser\Node\Stmt\Namespace_;
@@ -21,7 +22,7 @@ public function __construct(
2122
public array $stmts
2223
) {
2324
$firstStmt = $stmts[0] ?? null;
24-
parent::__construct($firstStmt instanceof \PhpParser\Node ? $firstStmt->getAttributes() : []);
25+
parent::__construct($firstStmt instanceof Node ? $firstStmt->getAttributes() : []);
2526

2627
parent::__construct();
2728

@@ -32,6 +33,9 @@ public function getType(): string
3233
return 'CustomNode_File';
3334
}
3435

36+
/**
37+
* @return array<int, string>
38+
*/
3539
public function getSubNodeNames(): array
3640
{
3741
return ['stmts'];
@@ -40,15 +44,15 @@ public function getSubNodeNames(): array
4044
public function isNamespaced(): bool
4145
{
4246
foreach ($this->stmts as $stmt) {
43-
if ($stmt instanceof Stmt\Namespace_) {
47+
if ($stmt instanceof Namespace_) {
4448
return true;
4549
}
4650
}
4751

4852
return false;
4953
}
5054

51-
public function getNamespace(): ?Stmt\Namespace_
55+
public function getNamespace(): ?Namespace_
5256
{
5357
/** @var Namespace_[] $namespaces */
5458
$namespaces = array_filter($this->stmts, static fn (Stmt $stmt): bool => $stmt instanceof Namespace_);

src/PhpParser/Printer/BetterStandardPrinter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function __construct(
6666
public function printFormatPreserving(array $stmts, array $origStmts, array $origTokens): string
6767
{
6868
$newStmts = $this->unwrapFileNode($stmts);
69+
$origStmts = $this->unwrapFileNode($origStmts);
6970

7071
$content = parent::printFormatPreserving($newStmts, $origStmts, $origTokens);
7172

@@ -519,7 +520,6 @@ private function getIndentCharacter(): string
519520
private function unwrapFileNode(array $stmts): array
520521
{
521522
// $stmts = array_values($stmts);
522-
523523
if (count($stmts) === 1 && $stmts[0] instanceof FileNode) {
524524
return array_values($stmts[0]->stmts);
525525
}

src/PostRector/Rector/ClassRenamingPostRector.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Stmt\Namespace_;
9-
use PhpParser\NodeTraverser;
109
use PhpParser\NodeVisitor;
1110
use Rector\CodingStyle\Application\UseImportsRemover;
1211
use Rector\Configuration\RenamedClassesDataCollector;

src/ValueObject/Application/File.php

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

55
namespace Rector\ValueObject\Application;
66

7+
use PhpParser\Node\Stmt\Namespace_;
78
use PhpParser\Node;
89
use PhpParser\Node\Stmt;
910
use PhpParser\Node\Stmt\InlineHTML;
@@ -156,7 +157,7 @@ public function addRectorClassWithLine(RectorWithLineChange $rectorWithLineChang
156157
* This node returns top most node,
157158
* that includes use imports
158159
*/
159-
public function getUseImportsRootNode(): Stmt\Namespace_|FileNode|null
160+
public function getUseImportsRootNode(): Namespace_|FileNode|null
160161
{
161162
if ($this->newStmts === []) {
162163
return null;
@@ -171,7 +172,7 @@ public function getUseImportsRootNode(): Stmt\Namespace_|FileNode|null
171172
// return sole Namespace, or none
172173
$namespaces = [];
173174
foreach ($firstStmt->stmts as $stmt) {
174-
if ($stmt instanceof Stmt\Namespace_) {
175+
if ($stmt instanceof Namespace_) {
175176
$namespaces[] = $stmt;
176177
}
177178
}

0 commit comments

Comments
 (0)