Skip to content

Commit b31f801

Browse files
authored
[Php80] Handle mix still annotation and already attribute on AnnotationToAttributeRector (#7639)
* test: Prove bug in AnnotationToAttributeRector with mixed content 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. * fix: AttributeValueResolver should not drop values of annotations 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.
1 parent 9e7f77d commit b31f801

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Behat;
4+
5+
use Behat\Step\When;
6+
7+
final class WithMixedAttributeAndAnnotations
8+
{
9+
/**
10+
* @When /^I have a step using annotations$/
11+
*/
12+
public function hadAnnotations(): void
13+
{
14+
}
15+
16+
#[When('/^I have a step using attributes$/')]
17+
public function hadAttributes(): void
18+
{
19+
}
20+
}
21+
22+
?>
23+
-----
24+
<?php
25+
26+
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Behat;
27+
28+
use Behat\Step\When;
29+
30+
final class WithMixedAttributeAndAnnotations
31+
{
32+
#[\Behat\Step\When('/^I have a step using annotations$/')]
33+
public function hadAnnotations(): void
34+
{
35+
}
36+
37+
#[When('/^I have a step using attributes$/')]
38+
public function hadAttributes(): void
39+
{
40+
}
41+
}
42+
43+
?>

rules/Php80/Rector/Class_/AttributeValueResolver.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public function resolve(
3232
if ($phpDocTagNode->value instanceof DoctrineAnnotationTagValueNode) {
3333
$originalContent = (string) $phpDocTagNode->value->getOriginalContent();
3434

35-
if ($docValue !== '') {
35+
if ($docValue === '') {
36+
$docValue = $originalContent;
37+
} else {
3638
$attributeComment = ltrim($originalContent, $docValue);
3739
if ($attributeComment !== '') {
3840
$docValue .= "\n" . $attributeComment;

0 commit comments

Comments
 (0)