Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
make use of FileNode in imports
  • Loading branch information
TomasVotruba committed Dec 8, 2025
commit d71688c507302d0ed7e6181e18e9e72c393b2cb4
18 changes: 18 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# Upgrading from Rector 2.2.11 to 2.2.12


@todo

* FileWithoutNamespace is deprecated, and replaced by `FileNode`
* `beforeTraverse()` is @final, use getNodeTypes() with `FileNode::class` instead

**Before**

@todo


**After**

@todo


# Upgrading from Rector 1.x to 2.0

## PHP version requirements
Expand Down
12 changes: 4 additions & 8 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ parameters:

# see https://phpstan.org/writing-php-code/phpdoc-types#global-type-aliases
typeAliases:
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
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

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

# allowed internally only
-
Expand Down Expand Up @@ -391,12 +390,6 @@ parameters:
paths:
- rules/Php70/Rector/If_/IfToSpaceshipRector.php

# handles full file
-
paths:
- rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php
identifier: rector.noOnlyNullReturnInRefactor

-
identifier: rector.noIntegerRefactorReturn
paths:
Expand All @@ -408,6 +401,7 @@ parameters:

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

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

=======
>>>>>>> ddfef52951 (cleanup phpstan errors)
16 changes: 9 additions & 7 deletions rules/CodingStyle/Application/UseImportsAdder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Rector\CodingStyle\ClassNameImport\UsedImportsResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\PhpParser\Node\FileNode;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;

Expand All @@ -33,7 +33,7 @@ public function __construct(
* @param array<FullyQualifiedObjectType|AliasedObjectType> $functionUseImportTypes
*/
public function addImportsToStmts(
FileWithoutNamespace $fileWithoutNamespace,
FileNode $fileNode,
array $stmts,
array $useImportTypes,
array $constantUseImportTypes,
Expand All @@ -45,10 +45,12 @@ public function addImportsToStmts(
$existingFunctionUseImports = $usedImports->getFunctionImports();

$useImportTypes = $this->diffFullyQualifiedObjectTypes($useImportTypes, $existingUseImportTypes);

$constantUseImportTypes = $this->diffFullyQualifiedObjectTypes(
$constantUseImportTypes,
$existingConstantUseImports
);

$functionUseImportTypes = $this->diffFullyQualifiedObjectTypes(
$functionUseImportTypes,
$existingFunctionUseImports
Expand Down Expand Up @@ -90,17 +92,17 @@ public function addImportsToStmts(

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

$fileWithoutNamespace->stmts = $stmts;
$fileWithoutNamespace->stmts = array_values($fileWithoutNamespace->stmts);
$fileNode->stmts = $stmts;
$fileNode->stmts = array_values($fileNode->stmts);

return true;
}

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

// make use stmts first
$fileWithoutNamespace->stmts = array_merge($newUses, $this->resolveInsertNop($fileWithoutNamespace), $stmts);
$fileWithoutNamespace->stmts = array_values($fileWithoutNamespace->stmts);
$fileNode->stmts = array_merge($newUses, $this->resolveInsertNop($fileNode), $stmts);
$fileNode->stmts = array_values($fileNode->stmts);

return true;
}
Expand Down Expand Up @@ -154,7 +156,7 @@ public function addImportsToNamespace(
/**
* @return Nop[]
*/
private function resolveInsertNop(FileWithoutNamespace|Namespace_ $namespace): array
private function resolveInsertNop(FileNode|Namespace_ $namespace): array
{
$currentStmt = $namespace->stmts[0] ?? null;
if (! $currentStmt instanceof Stmt || $currentStmt instanceof Use_ || $currentStmt instanceof GroupUse) {
Expand Down
4 changes: 2 additions & 2 deletions rules/CodingStyle/Application/UseImportsRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\PhpParser\Node\FileNode;
use Rector\Renaming\Collector\RenamedNameCollector;

final readonly class UseImportsRemover
Expand All @@ -19,7 +19,7 @@ public function __construct(
/**
* @param string[] $removedUses
*/
public function removeImportsFromStmts(FileWithoutNamespace|Namespace_ $node, array $removedUses): bool
public function removeImportsFromStmts(FileNode|Namespace_ $node, array $removedUses): bool
{
$hasRemoved = false;
foreach ($node->stmts as $key => $stmt) {
Expand Down
8 changes: 4 additions & 4 deletions rules/CodingStyle/ClassNameImport/AliasUsesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\UseItem;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\PhpParser\Node\FileNode;

final readonly class AliasUsesResolver
{
Expand All @@ -25,11 +25,11 @@ public function __construct(
*/
public function resolveFromNode(Node $node, array $stmts): array
{
if (! $node instanceof Namespace_ && ! $node instanceof FileWithoutNamespace) {
/** @var Namespace_[]|FileWithoutNamespace[] $namespaces */
if (! $node instanceof Namespace_ && ! $node instanceof FileNode) {
/** @var Namespace_[]|FileNode[] $namespaces */
$namespaces = array_filter(
$stmts,
static fn (Stmt $stmt): bool => $stmt instanceof Namespace_ || $stmt instanceof FileWithoutNamespace
static fn (Stmt $stmt): bool => $stmt instanceof Namespace_ || $stmt instanceof FileNode
);
if (count($namespaces) !== 1) {
return [];
Expand Down
19 changes: 5 additions & 14 deletions rules/CodingStyle/ClassNameImport/ShortNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Namespace_;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
Expand All @@ -20,7 +19,6 @@
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\ValueObject\Application\File;

/**
Expand Down Expand Up @@ -65,22 +63,15 @@ public function resolveFromFile(File $file): array
*/
public function resolveShortClassLikeNames(File $file): array
{
$newStmts = $file->getNewStmts();

/** @var Namespace_[]|FileWithoutNamespace[] $namespaces */
$namespaces = array_filter(
$newStmts,
static fn (Stmt $stmt): bool => $stmt instanceof Namespace_ || $stmt instanceof FileWithoutNamespace
);
if (count($namespaces) !== 1) {
// only handle single namespace nodes
$rootNode = $file->getUseImportsRootNode();

// nothing to resolve
if (! $rootNode instanceof Node) {
return [];
}

$namespace = current($namespaces);

/** @var ClassLike[] $classLikes */
$classLikes = $this->betterNodeFinder->findInstanceOf($namespace->stmts, ClassLike::class);
$classLikes = $this->betterNodeFinder->findInstanceOf($rootNode->stmts, ClassLike::class);

$shortClassLikeNames = [];
foreach ($classLikes as $classLike) {
Expand Down
4 changes: 2 additions & 2 deletions rules/CodingStyle/ClassNameImport/UseImportsTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\UseItem;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\PhpParser\Node\FileNode;

final readonly class UseImportsTraverser
{
Expand All @@ -26,7 +26,7 @@ public function __construct(
public function traverserStmts(array $stmts, callable $callable): void
{
foreach ($stmts as $stmt) {
if ($stmt instanceof Namespace_ || $stmt instanceof FileWithoutNamespace) {
if ($stmt instanceof Namespace_ || $stmt instanceof FileNode) {
$this->traverserStmts($stmt->stmts, $callable);
continue;
}
Expand Down
1 change: 1 addition & 0 deletions rules/CodingStyle/Node/NameImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function __construct(
*/
public function importName(FullyQualified $fullyQualified, File $file, array $currentUses): ?Name
{

if ($this->classNameImportSkipper->shouldSkipName($fullyQualified, $currentUses)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use PHPStan\Type\ObjectType;
use Rector\Naming\Naming\PropertyNaming;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\PhpParser\Node\FileNode;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -73,24 +73,23 @@ public function getRuleDefinition(): RuleDefinition
*/
public function getNodeTypes(): array
{
return [
ClassMethod::class,
Function_::class,
Closure::class,
FileWithoutNamespace::class,
Namespace_::class,
];
return [ClassMethod::class, Function_::class, Closure::class, FileNode::class, Namespace_::class];
}

/**
* @param ClassMethod|Function_|Closure|FileWithoutNamespace|Namespace_ $node
* @param ClassMethod|Function_|Closure|FileNode|Namespace_ $node
*/
public function refactor(Node $node): ?Node
{
if ($node->stmts === null) {
return null;
}

if ($node instanceof FileNode && $node->isNamespaced()) {
// handled in Namespace_ node
return null;
}

$hasChanged = false;

foreach ($node->stmts as $key => $stmt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\PhpParser\Node\FileNode;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -42,15 +42,21 @@ public function getRuleDefinition(): RuleDefinition
*/
public function getNodeTypes(): array
{
return [FileWithoutNamespace::class, Namespace_::class];
return [FileNode::class, Namespace_::class];
}

/**
* @param FileWithoutNamespace|Namespace_ $node
* @param Namespace_|FileNode $node
*/
public function refactor(Node $node): null|FileWithoutNamespace|Namespace_
public function refactor(Node $node): null|FileNode|Namespace_
{
if ($node instanceof FileNode && $node->isNamespaced()) {
// handle in Namespace_ node
return null;
}

$hasChanged = false;

foreach ($node->stmts as $stmt) {
if (! $stmt instanceof Use_) {
continue;
Expand Down
13 changes: 9 additions & 4 deletions rules/CodingStyle/Rector/Use_/SeparateMultiUseImportsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use PhpParser\Node\Stmt\TraitUse;
use PhpParser\Node\Stmt\TraitUseAdaptation\Alias;
use PhpParser\Node\Stmt\Use_;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\PhpParser\Node\FileNode;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -56,14 +56,19 @@ class SomeClass
*/
public function getNodeTypes(): array
{
return [FileWithoutNamespace::class, Namespace_::class, Class_::class];
return [FileNode::class, Namespace_::class, Class_::class];
}

/**
* @param FileWithoutNamespace|Namespace_|Class_ $node
* @param FileNode|Namespace_|Class_ $node
*/
public function refactor(Node $node): FileWithoutNamespace|Namespace_|Class_|null
public function refactor(Node $node): FileNode|Namespace_|Class_|null
{
if ($node instanceof FileNode && $node->isNamespaced()) {
// handled in Namespace_
return null;
}

$hasChanged = false;
foreach ($node->stmts as $key => $stmt) {
if ($stmt instanceof Use_) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Stmt;
use Rector\NodeAnalyzer\TerminatedNodeAnalyzer;
use Rector\PhpParser\Enum\NodeGroup;
use Rector\PhpParser\Node\FileNode;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -64,6 +65,11 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($node instanceof FileNode && $node->isNamespaced()) {
// handled in Namespace_ node
return null;
}

if ($node->stmts === null) {
return null;
}
Expand Down
Loading