Skip to content

Conversation

@simonschaufi
Copy link
Contributor


// notify this rule changing code
$rectorWithLineChange = new RectorWithLineChange(self::class, $node->getStartLine());
$this->file->addRectorClassWithLine($rectorWithLineChange);
Copy link
Member

@samsonasik samsonasik Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add logic to verify that removeImportsFromStmts apply the change, if no change, don't add to the list.

Copy link
Contributor Author

@simonschaufi simonschaufi Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The challenge is that none of these post rectors have test coverage...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can create e2e test for it, add to

https://github.com/rectorphp/rector-src/tree/main/e2e

and register to

directory:
- 'e2e/applied-rule-change-docblock'
- 'e2e/applied-rule-return-array-nodes'
- 'e2e/no-parallel-reflection-resolver'
- 'e2e/parallel-custom-config'
- 'e2e/parallel-reflection-resolver'
- 'e2e/applied-rule-removed-node'
- 'e2e/parallel with space'
- 'e2e/different-path-over-skip-config'
- 'e2e/invalid-paths'
- 'e2e/applied-polyfill-php80'
- 'e2e/print-new-node'
- 'e2e/only-option'
- 'e2e/only-option-quote-double-equalnone'
- 'e2e/only-option-quote-single'
- 'e2e/only-option-quote-single-bsdouble'
- 'e2e/only-option-quote-single-equalnone'
- 'e2e/parallel-kaizen-applied-rules'

Copy link
Contributor Author

@simonschaufi simonschaufi Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't even understand how the ClassRenamingPostRector supposed to work and also the UseAddingPostRector

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try create separate alternative PR :),

No need to close this PR for now, you may be faster than me so understand the flow early, and got the solution.

Copy link
Contributor Author

@simonschaufi simonschaufi Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an e2e test but I don't know how to make use of the UseAddingPostRector. Maybe you can contribute some code examples?

$removedUses = $this->renamedClassesDataCollector->getOldClasses();
$node->stmts = $this->useImportsRemover->removeImportsFromStmts($node->stmts, $removedUses);
$stmts = $this->useImportsRemover->removeImportsFromStmts($node->stmts, $removedUses);
if ($stmts !== $node->stmts) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

objects collection can't be compared like this, use something like $hasChanged flag instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how would you do it as I can't access the $hasChanged within the UseImportsRemover

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removeImportsFromStmts can return bool, then return verify it, no need to reassign since objects always by reference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a cleaner solution. Please have a look.

@simonschaufi simonschaufi force-pushed the issue-9373 branch 2 times, most recently from 7d1713c to 94e0a70 Compare September 19, 2025 18:49
if ($node instanceof FileWithoutNamespace || $node instanceof Namespace_) {
$removedUses = $this->renamedClassesDataCollector->getOldClasses();
$node->stmts = $this->useImportsRemover->removeImportsFromStmts($node->stmts, $removedUses);
if ($removedUses !== []) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It already never empty since checked on shouldTraverse method.

The $this->useImportsRemover->removeImportsFromStmts($node->stmts, $removedUses); can be updated to return bool, use flag like $hasChanged.

Yes, object is by reference, so you can verify if it has changed, no need to reassign on
This case.

Copy link
Contributor Author

@simonschaufi simonschaufi Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please do this refactoring in a separate isolated PR so that I can then use it in my PR? The return value is an array and unsetting a value in an array is not done by reference afaik. I would appreciate your help here!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the code, this PR seems cause various warning on unit test

WARNING: On fixture file "conflict_aliased_with_docblock.php.inc" for test "Rector\Tests\Issues\AutoImport\AutoImportTest"
File not changed but some Rector rules applied:
 * Rector\PostRector\Rector\ClassRenamingPostRector
.....
WARNING: On fixture file "skip_doctrine_aliased.php.inc" for test "Rector\Tests\Issues\AutoImport\AutoImportTest"
File not changed but some Rector rules applied:
 * Rector\PostRector\Rector\ClassRenamingPostRector

see:

https://github.com/rectorphp/rector-src/actions/runs/17867691953/job/50813890782?pr=7300#step:5:20

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try create alternative fresh PR, since NameImporter may also needs refactoring as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samsonasik
Copy link
Member

@samsonasik samsonasik closed this Sep 20, 2025
@simonschaufi simonschaufi deleted the issue-9373 branch September 20, 2025 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Post Rectors are not listed in the list of applied rules in the rectorsWithLineChanges list

2 participants