Skip to content

Commit d71688c

Browse files
committed
make use of FileNode in imports
1 parent 7f4a918 commit d71688c

34 files changed

+267
-213
lines changed

UPGRADING.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
# Upgrading from Rector 2.2.11 to 2.2.12
2+
3+
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+
16+
@todo
17+
18+
119
# Upgrading from Rector 1.x to 2.0
220

321
## PHP version requirements

phpstan.neon

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ parameters:
1010

1111
# see https://phpstan.org/writing-php-code/phpdoc-types#global-type-aliases
1212
typeAliases:
13-
StmtsAware: \PhpParser\Node\Stmt\Block | \PhpParser\Node\Expr\Closure | \PhpParser\Node\Stmt\Case_ | \PhpParser\Node\Stmt\Catch_ | \PhpParser\Node\Stmt\ClassMethod | \PhpParser\Node\Stmt\Do_ | \PhpParser\Node\Stmt\Else_ | \PhpParser\Node\Stmt\ElseIf_ | \PhpParser\Node\Stmt\Finally_ | \PhpParser\Node\Stmt\For_ | \PhpParser\Node\Stmt\Foreach_ | \PhpParser\Node\Stmt\Function_ | \PhpParser\Node\Stmt\If_ | \PhpParser\Node\Stmt\Namespace_ | \PhpParser\Node\Stmt\TryCatch | \PhpParser\Node\Stmt\While_ | \Rector\PhpParser\Node\CustomNode\FileWithoutNamespace
13+
StmtsAware: \PhpParser\Node\Stmt\Block | \PhpParser\Node\Expr\Closure | \PhpParser\Node\Stmt\Case_ | \PhpParser\Node\Stmt\Catch_ | \PhpParser\Node\Stmt\ClassMethod | \PhpParser\Node\Stmt\Do_ | \PhpParser\Node\Stmt\Else_ | \PhpParser\Node\Stmt\ElseIf_ | \PhpParser\Node\Stmt\Finally_ | \PhpParser\Node\Stmt\For_ | \PhpParser\Node\Stmt\Foreach_ | \PhpParser\Node\Stmt\Function_ | \PhpParser\Node\Stmt\If_ | \PhpParser\Node\Stmt\Namespace_ | \PhpParser\Node\Stmt\TryCatch | \PhpParser\Node\Stmt\While_ | \Rector\PhpParser\Node\FileNode
1414

1515
# requires exact closure types
1616
checkMissingCallableSignature: true
@@ -284,7 +284,6 @@ parameters:
284284
- src/Configuration/RectorConfigBuilder.php
285285
- src/Reporting/DeprecatedRulesReporter.php
286286
identifier: classConstant.deprecatedInterface
287-
- '#Class Rector\\PhpParser\\Node\\CustomNode\\FileWithoutNamespace implements deprecated interface Rector\\Contract\\PhpParser\\Node\\StmtsAwareInterface#'
288287

289288
# allowed internally only
290289
-
@@ -391,12 +390,6 @@ parameters:
391390
paths:
392391
- rules/Php70/Rector/If_/IfToSpaceshipRector.php
393392

394-
# handles full file
395-
-
396-
paths:
397-
- rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php
398-
identifier: rector.noOnlyNullReturnInRefactor
399-
400393
-
401394
identifier: rector.noIntegerRefactorReturn
402395
paths:
@@ -408,6 +401,7 @@ parameters:
408401

409402
# condition check, just to be sure
410403
- '#Method Rector\\Rector\\AbstractRector\:\:enterNode\(\) never returns 3 so it can be removed from the return type#'
404+
<<<<<<< HEAD
411405

412406
# special case, working on a file-level
413407
-
@@ -433,3 +427,5 @@ parameters:
433427
- rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php
434428
- rules/Php81/Enum/AttributeName.php
435429

430+
=======
431+
>>>>>>> ddfef52951 (cleanup phpstan errors)

rules/CodingStyle/Application/UseImportsAdder.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Rector\CodingStyle\ClassNameImport\UsedImportsResolver;
1515
use Rector\NodeTypeResolver\Node\AttributeKey;
1616
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
17-
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
17+
use Rector\PhpParser\Node\FileNode;
1818
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
1919
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
2020

@@ -33,7 +33,7 @@ public function __construct(
3333
* @param array<FullyQualifiedObjectType|AliasedObjectType> $functionUseImportTypes
3434
*/
3535
public function addImportsToStmts(
36-
FileWithoutNamespace $fileWithoutNamespace,
36+
FileNode $fileNode,
3737
array $stmts,
3838
array $useImportTypes,
3939
array $constantUseImportTypes,
@@ -45,10 +45,12 @@ public function addImportsToStmts(
4545
$existingFunctionUseImports = $usedImports->getFunctionImports();
4646

4747
$useImportTypes = $this->diffFullyQualifiedObjectTypes($useImportTypes, $existingUseImportTypes);
48+
4849
$constantUseImportTypes = $this->diffFullyQualifiedObjectTypes(
4950
$constantUseImportTypes,
5051
$existingConstantUseImports
5152
);
53+
5254
$functionUseImportTypes = $this->diffFullyQualifiedObjectTypes(
5355
$functionUseImportTypes,
5456
$existingFunctionUseImports
@@ -90,17 +92,17 @@ public function addImportsToStmts(
9092

9193
array_splice($stmts, $key + 1, 0, $nodesToAdd);
9294

93-
$fileWithoutNamespace->stmts = $stmts;
94-
$fileWithoutNamespace->stmts = array_values($fileWithoutNamespace->stmts);
95+
$fileNode->stmts = $stmts;
96+
$fileNode->stmts = array_values($fileNode->stmts);
9597

9698
return true;
9799
}
98100

99101
$this->mirrorUseComments($stmts, $newUses);
100102

101103
// make use stmts first
102-
$fileWithoutNamespace->stmts = array_merge($newUses, $this->resolveInsertNop($fileWithoutNamespace), $stmts);
103-
$fileWithoutNamespace->stmts = array_values($fileWithoutNamespace->stmts);
104+
$fileNode->stmts = array_merge($newUses, $this->resolveInsertNop($fileNode), $stmts);
105+
$fileNode->stmts = array_values($fileNode->stmts);
104106

105107
return true;
106108
}
@@ -154,7 +156,7 @@ public function addImportsToNamespace(
154156
/**
155157
* @return Nop[]
156158
*/
157-
private function resolveInsertNop(FileWithoutNamespace|Namespace_ $namespace): array
159+
private function resolveInsertNop(FileNode|Namespace_ $namespace): array
158160
{
159161
$currentStmt = $namespace->stmts[0] ?? null;
160162
if (! $currentStmt instanceof Stmt || $currentStmt instanceof Use_ || $currentStmt instanceof GroupUse) {

rules/CodingStyle/Application/UseImportsRemover.php

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

77
use PhpParser\Node\Stmt\Namespace_;
88
use PhpParser\Node\Stmt\Use_;
9-
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
9+
use Rector\PhpParser\Node\FileNode;
1010
use Rector\Renaming\Collector\RenamedNameCollector;
1111

1212
final readonly class UseImportsRemover
@@ -19,7 +19,7 @@ public function __construct(
1919
/**
2020
* @param string[] $removedUses
2121
*/
22-
public function removeImportsFromStmts(FileWithoutNamespace|Namespace_ $node, array $removedUses): bool
22+
public function removeImportsFromStmts(FileNode|Namespace_ $node, array $removedUses): bool
2323
{
2424
$hasRemoved = false;
2525
foreach ($node->stmts as $key => $stmt) {

rules/CodingStyle/ClassNameImport/AliasUsesResolver.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use PhpParser\Node\Stmt\Namespace_;
1111
use PhpParser\Node\Stmt\Use_;
1212
use PhpParser\Node\UseItem;
13-
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
13+
use Rector\PhpParser\Node\FileNode;
1414

1515
final readonly class AliasUsesResolver
1616
{
@@ -25,11 +25,11 @@ public function __construct(
2525
*/
2626
public function resolveFromNode(Node $node, array $stmts): array
2727
{
28-
if (! $node instanceof Namespace_ && ! $node instanceof FileWithoutNamespace) {
29-
/** @var Namespace_[]|FileWithoutNamespace[] $namespaces */
28+
if (! $node instanceof Namespace_ && ! $node instanceof FileNode) {
29+
/** @var Namespace_[]|FileNode[] $namespaces */
3030
$namespaces = array_filter(
3131
$stmts,
32-
static fn (Stmt $stmt): bool => $stmt instanceof Namespace_ || $stmt instanceof FileWithoutNamespace
32+
static fn (Stmt $stmt): bool => $stmt instanceof Namespace_ || $stmt instanceof FileNode
3333
);
3434
if (count($namespaces) !== 1) {
3535
return [];

rules/CodingStyle/ClassNameImport/ShortNameResolver.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use PhpParser\Node\Name;
1010
use PhpParser\Node\Stmt;
1111
use PhpParser\Node\Stmt\ClassLike;
12-
use PhpParser\Node\Stmt\Namespace_;
1312
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
1413
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
1514
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
@@ -20,7 +19,6 @@
2019
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
2120
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser;
2221
use Rector\PhpParser\Node\BetterNodeFinder;
23-
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
2422
use Rector\ValueObject\Application\File;
2523

2624
/**
@@ -65,22 +63,15 @@ public function resolveFromFile(File $file): array
6563
*/
6664
public function resolveShortClassLikeNames(File $file): array
6765
{
68-
$newStmts = $file->getNewStmts();
69-
70-
/** @var Namespace_[]|FileWithoutNamespace[] $namespaces */
71-
$namespaces = array_filter(
72-
$newStmts,
73-
static fn (Stmt $stmt): bool => $stmt instanceof Namespace_ || $stmt instanceof FileWithoutNamespace
74-
);
75-
if (count($namespaces) !== 1) {
76-
// only handle single namespace nodes
66+
$rootNode = $file->getUseImportsRootNode();
67+
68+
// nothing to resolve
69+
if (! $rootNode instanceof Node) {
7770
return [];
7871
}
7972

80-
$namespace = current($namespaces);
81-
8273
/** @var ClassLike[] $classLikes */
83-
$classLikes = $this->betterNodeFinder->findInstanceOf($namespace->stmts, ClassLike::class);
74+
$classLikes = $this->betterNodeFinder->findInstanceOf($rootNode->stmts, ClassLike::class);
8475

8576
$shortClassLikeNames = [];
8677
foreach ($classLikes as $classLike) {

rules/CodingStyle/ClassNameImport/UseImportsTraverser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use PhpParser\Node\Stmt\Use_;
1111
use PhpParser\Node\UseItem;
1212
use Rector\NodeNameResolver\NodeNameResolver;
13-
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
13+
use Rector\PhpParser\Node\FileNode;
1414

1515
final readonly class UseImportsTraverser
1616
{
@@ -26,7 +26,7 @@ public function __construct(
2626
public function traverserStmts(array $stmts, callable $callable): void
2727
{
2828
foreach ($stmts as $stmt) {
29-
if ($stmt instanceof Namespace_ || $stmt instanceof FileWithoutNamespace) {
29+
if ($stmt instanceof Namespace_ || $stmt instanceof FileNode) {
3030
$this->traverserStmts($stmt->stmts, $callable);
3131
continue;
3232
}

rules/CodingStyle/Node/NameImporter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function __construct(
3232
*/
3333
public function importName(FullyQualified $fullyQualified, File $file, array $currentUses): ?Name
3434
{
35+
3536
if ($this->classNameImportSkipper->shouldSkipName($fullyQualified, $currentUses)) {
3637
return null;
3738
}

rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use PHPStan\Type\ObjectType;
2222
use Rector\Naming\Naming\PropertyNaming;
2323
use Rector\NodeTypeResolver\Node\AttributeKey;
24-
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
24+
use Rector\PhpParser\Node\FileNode;
2525
use Rector\Rector\AbstractRector;
2626
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2727
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -73,24 +73,23 @@ public function getRuleDefinition(): RuleDefinition
7373
*/
7474
public function getNodeTypes(): array
7575
{
76-
return [
77-
ClassMethod::class,
78-
Function_::class,
79-
Closure::class,
80-
FileWithoutNamespace::class,
81-
Namespace_::class,
82-
];
76+
return [ClassMethod::class, Function_::class, Closure::class, FileNode::class, Namespace_::class];
8377
}
8478

8579
/**
86-
* @param ClassMethod|Function_|Closure|FileWithoutNamespace|Namespace_ $node
80+
* @param ClassMethod|Function_|Closure|FileNode|Namespace_ $node
8781
*/
8882
public function refactor(Node $node): ?Node
8983
{
9084
if ($node->stmts === null) {
9185
return null;
9286
}
9387

88+
if ($node instanceof FileNode && $node->isNamespaced()) {
89+
// handled in Namespace_ node
90+
return null;
91+
}
92+
9493
$hasChanged = false;
9594

9695
foreach ($node->stmts as $key => $stmt) {

rules/CodingStyle/Rector/Stmt/RemoveUselessAliasInUseStatementRector.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use PhpParser\Node\Identifier;
1010
use PhpParser\Node\Stmt\Namespace_;
1111
use PhpParser\Node\Stmt\Use_;
12-
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
12+
use Rector\PhpParser\Node\FileNode;
1313
use Rector\Rector\AbstractRector;
1414
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1515
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -42,15 +42,21 @@ public function getRuleDefinition(): RuleDefinition
4242
*/
4343
public function getNodeTypes(): array
4444
{
45-
return [FileWithoutNamespace::class, Namespace_::class];
45+
return [FileNode::class, Namespace_::class];
4646
}
4747

4848
/**
49-
* @param FileWithoutNamespace|Namespace_ $node
49+
* @param Namespace_|FileNode $node
5050
*/
51-
public function refactor(Node $node): null|FileWithoutNamespace|Namespace_
51+
public function refactor(Node $node): null|FileNode|Namespace_
5252
{
53+
if ($node instanceof FileNode && $node->isNamespaced()) {
54+
// handle in Namespace_ node
55+
return null;
56+
}
57+
5358
$hasChanged = false;
59+
5460
foreach ($node->stmts as $stmt) {
5561
if (! $stmt instanceof Use_) {
5662
continue;

0 commit comments

Comments
 (0)