From 38dae7d5ad9351761137fd79072cba1d89fff6d5 Mon Sep 17 00:00:00 2001 From: Adel Kaleche Date: Sat, 19 Sep 2026 18:05:39 +0200 Subject: [PATCH] [PostRector] Keep imports used in an earlier docblock of a node with stacked docblocks UnusedImportRemovingPostRector checks each docblock of a node on a clone with setDocComment(), but the clone keeps the PHP_DOC_INFO attribute already resolved for the original node (its last docblock), so the factory returns that PhpDocInfo instead of parsing the docblock: only the last docblock is scanned and the imports used by the earlier ones are removed. Clear the attribute on the clone. --- .../Rector/UnusedImportRemovingPostRector.php | 3 +++ ...stacked_docblocks_keep_used_import.php.inc | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 tests/Issues/NamespacedUseAutoImport/Fixture/stacked_docblocks_keep_used_import.php.inc diff --git a/src/PostRector/Rector/UnusedImportRemovingPostRector.php b/src/PostRector/Rector/UnusedImportRemovingPostRector.php index 120c30b3520..1f71cde1cb4 100644 --- a/src/PostRector/Rector/UnusedImportRemovingPostRector.php +++ b/src/PostRector/Rector/UnusedImportRemovingPostRector.php @@ -158,6 +158,9 @@ private function findNamesInDocBlocks(Namespace_|FileNode $rootNode): array $nodeToCheck = $totalDocs === 1 ? $node : clone $node; if ($totalDocs > 1) { $nodeToCheck->setDocComment($doc); + // the clone carries the PhpDocInfo already resolved for the original node (its last doc comment), + // so the factory would return it instead of parsing $doc + $nodeToCheck->setAttribute(AttributeKey::PHP_DOC_INFO, null); } $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($nodeToCheck); diff --git a/tests/Issues/NamespacedUseAutoImport/Fixture/stacked_docblocks_keep_used_import.php.inc b/tests/Issues/NamespacedUseAutoImport/Fixture/stacked_docblocks_keep_used_import.php.inc new file mode 100644 index 00000000000..c85fbb590b9 --- /dev/null +++ b/tests/Issues/NamespacedUseAutoImport/Fixture/stacked_docblocks_keep_used_import.php.inc @@ -0,0 +1,21 @@ +