Skip to content

Introduce FileNode to handle file-level changes - #7728

Merged
TomasVotruba merged 11 commits into
mainfrom
tv-rector-avoid-before-traverser
Dec 22, 2025
Merged

Introduce FileNode to handle file-level changes#7728
TomasVotruba merged 11 commits into
mainfrom
tv-rector-avoid-before-traverser

Conversation

@TomasVotruba

@TomasVotrubaTomasVotruba commented Dec 7, 2025

Copy link
Copy Markdown
Member

At the moment, there is no way to handle file-level operations, like adding declare(...) to a file. We hack around this using beforeTraverse(), which is internal part of php-parser and should be avoided.

This PR adds a FileNode to fix that. It allows to make changes directly in refactor() method as it should be 👍

finalclass DeclareStrictRector extends AbstractRector
{
publicfunctiongetNodeTypes(): array
{
return [FileNode::class];
}
/** * @param FileNode $node */publicfunctionrefactor(Node$node): ?FileNode
{
if ($this->declareStrictTypeFinder->hasDeclareStrictTypes($node)) {
returnnull;
}
$declaresStrictType = $this->nodeFactory->createDeclaresStrictType();
$node->stmts = array_merge([$declaresStrictType, newNop()], $node->stmts);
return$node;
}
}

It also opens-up possibility to have a single place to manage use statements (now handled by couple meta services). Next step will be marking beforeTraverse() and afterTraverse()final in AbstractRector, to keep BC promise only for RectorInterface (as it should be from the start).


This PR keeps BC as much as possible and adds reporting about deprecation to in custom rules.

Screenshot From 2025-12-22 11-12-19

Use of FileWithoutNamespace::class node: https://github.com/search?q=%22FileWithoutNamespace%3A%3Aclass%22+NOT+is%3Afork&type=code

Todo

  • add BC layer to make FileWithoutNamespace still work
  • add warning about it's use in getNodeTypes() and explain upgrade path

Related package PRs

@TomasVotrubaTomasVotruba changed the title tv rector avoid before traverserIntroduce FileNode to handle file-level changesDec 7, 2025
@TomasVotruba
TomasVotrubaforce-pushed the tv-rector-avoid-before-traverser branch 2 times, most recently from 3af10dd to 4263effCompareDecember 7, 2025 11:59
@TomasVotruba
TomasVotrubaforce-pushed the tv-rector-avoid-before-traverser branch 11 times, most recently from 6d9c96d to 652b580CompareDecember 8, 2025 23:33
@TomasVotruba
TomasVotrubaforce-pushed the tv-rector-avoid-before-traverser branch 5 times, most recently from ec1edaa to a4b2b44CompareDecember 22, 2025 10:09
Comment threadUPGRADING.md Outdated
@TomasVotruba
TomasVotrubaforce-pushed the tv-rector-avoid-before-traverser branch from a4b2b44 to 4af1f44CompareDecember 22, 2025 10:24
Comment threadsrc/PhpParser/Printer/BetterStandardPrinter.php Outdated
@TomasVotruba
TomasVotrubaforce-pushed the tv-rector-avoid-before-traverser branch from 41954f4 to 3fc5f1eCompareDecember 22, 2025 10:29
Comment threadsrc/PhpParser/Printer/BetterStandardPrinter.php
Comment threadsrc/PhpParser/Node/CustomNode/FileWithoutNamespace.php Outdated
Comment threadsrc/PhpParser/Node/CustomNode/FileWithoutNamespace.php Outdated
@TomasVotruba
TomasVotrubaforce-pushed the tv-rector-avoid-before-traverser branch from 3fc5f1e to e53b2faCompareDecember 22, 2025 13:04
@TomasVotruba
TomasVotrubaforce-pushed the tv-rector-avoid-before-traverser branch 2 times, most recently from 083efb3 to cbc1c5eCompareDecember 22, 2025 13:32
Comment threadsrc/PhpParser/Node/CustomNode/FileWithoutNamespace.php Outdated
@TomasVotruba
TomasVotrubaforce-pushed the tv-rector-avoid-before-traverser branch from 6208ebe to ff771b0CompareDecember 22, 2025 15:19
@TomasVotruba

Copy link
Copy Markdown
MemberAuthor

Let's ship it 👍

@TomasVotruba

Copy link
Copy Markdown
MemberAuthor

@samsonasik Just released 2.3 with FileNode 👍 ready to merge these drafts

@TomasVotruba

Copy link
Copy Markdown
MemberAuthor

Here is upgrade guide for the FileNode: https://github.com/rectorphp/rector-src/blob/main/UPGRADING.md

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@TomasVotruba@samsonasik