[Php80] Handle mix still annotation and already attribute on AnnotationToAttributeRector#7639
Merged
TomasVotruba merged 2 commits intorectorphp:mainfrom Nov 19, 2025
Conversation
If a file already contains a mix of annotations and attributes, the current AnnotationToAttributeRector implementation drops the annotation value when generating the new Attribute tag. This could happen, for example, with a Behat context in a legacy project that has a mix of old and new step definitions.
If the file being parsed has already imported a class matching the name of the annotation tag, then the annotation tag will have been parsed as a DoctrineAnnotationTagValueNode. For some tags - e.g. Behat steps - the `$phpDocTagNode->value` will cast to an empty string (because the annotation text does not actually contain valid Doctrine annotation values). In this case, the actual annotation value needs to be retrieved from `$phpDocTagNode->value->getOriginalContent()`. This was causing AnnotationToAttributeRector to drop the attribute values when converting from annotations to attributes in a file that already contained some attributes. This was previously implemented, but was reverted more recently as part of dropping support for comments on these nodes.
samsonasik
approved these changes
Nov 19, 2025
Member
|
Thank you for the fix, great job 👍 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
If a file already contains a mix of annotations and attributes, the current AnnotationToAttributeRector implementation drops the annotation value when generating the new Attribute tag and therefore produces an invalid result.
This could happen, for example, with a Behat context in a legacy project that has a mix of old and new step definitions.
AFAICS this is because:
@Whenattribute is parsed as aPhpDocTagNodeuse Behat\Step\When) then the@Whenattribute is parsed as aDoctrineAnnotationTagValueNode.The original annotation value is stored differently in these two tag node classes.
It looks like this was originally supported / handled through a fix in #6582. That was subsequently modified in #6589 to add support for additional comments following the attribute value.
More recently #7582 removed the comment logic, but also removed handling for the original case where the node is an instance of
DoctrineAnnotationTagValueNodebut its->valuecasts to an empty string.I have added a new fixture to prove the bug and will push again with a second commit to show that it is now fixed.