Skip to content

[Php80] Keep trailing doc comment after annotation in AnnotationToAttributeRector#8160

Merged
TomasVotruba merged 2 commits into
mainfrom
fix-annotation-to-attribute-trailing-comment
Jul 8, 2026
Merged

[Php80] Keep trailing doc comment after annotation in AnnotationToAttributeRector#8160
TomasVotruba merged 2 commits into
mainfrom
fix-annotation-to-attribute-trailing-comment

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Jul 8, 2026

Copy link
Copy Markdown
Member

Fixes #9786

When AnnotationToAttributeRector converts a Symfony/Doctrine annotation to an attribute, a doc comment placed on the line(s) below the annotation was deleted along with it.

The doctrine annotation swallows that trailing comment into its original content, so removing the converted tag removed the comment too. A comment placed above the annotation was already kept (it is a separate text node) — only the comment after was lost.

Fix: split the trailing comment back out into its own docblock text node instead of dropping it.

Before

     /**
      * @Route("/path", name="action")
-     * this comment must be kept
      */
-    public function action()
+    #[Route(path: '/path', name: 'action')]
+    public function action()

After

-    /**
-     * @Route("/path", name="action")
-     * this comment must be kept
-     */
+    /**
+     * this comment must be kept
+     */
+    #[Route(path: '/path', name: 'action')]
     public function action()

Works for multi-line annotations too, and does not misread parentheses inside string values ("/path/(foo)") or leftover annotation syntax (a stray )) as a comment.

…ributeRector

A doctrine annotation swallows the doc comment placed on the line(s) below
it into its original content, so removing the converted tag also removed the
comment. Split the trailing comment back out into a PhpDocTextNode instead.

Fixes rectorphp/rector#9786
@TomasVotruba

Copy link
Copy Markdown
Member Author

LGTM 👍

@TomasVotruba TomasVotruba merged commit e6fc207 into main Jul 8, 2026
65 checks passed
@TomasVotruba TomasVotruba deleted the fix-annotation-to-attribute-trailing-comment branch July 8, 2026 16:49
Sign up for free to 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.

1 participant