diff --git a/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php b/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php index ba69e27d..f323951b 100644 --- a/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php +++ b/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php @@ -4,6 +4,7 @@ namespace Rector\DowngradePhp80\Rector\Class_; +use PhpParser\Comment; use PhpParser\Node; use PhpParser\Node\Attribute; use PhpParser\Node\Param; @@ -19,6 +20,8 @@ use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\DowngradePhp80\ValueObject\DowngradeAttributeToAnnotation; use Rector\NodeFactory\DoctrineAnnotationFactory; +use Rector\NodeTypeResolver\Node\AttributeKey; +use Rector\PhpParser\Printer\BetterStandardPrinter; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -47,6 +50,7 @@ public function __construct( private readonly DoctrineAnnotationFactory $doctrineAnnotationFactory, private readonly DocBlockUpdater $docBlockUpdater, private readonly PhpDocInfoFactory $phpDocInfoFactory, + private readonly BetterStandardPrinter $betterStandardPrinter ) { } @@ -105,9 +109,22 @@ public function refactor(Node $node): ?Node $this->isDowngraded = false; $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); + $attributesAsComments = []; + $oldTokens = $this->file->getOldTokens(); + foreach ($node->attrGroups as $attrGroup) { foreach ($attrGroup->attrs as $key => $attribute) { if ($this->shouldSkipAttribute($attribute)) { + if (isset($oldTokens[$attrGroup->getEndTokenPos() + 1]) && ! str_contains( + (string) $oldTokens[$attrGroup->getEndTokenPos() + 1], + "\n" + )) { + $print = $this->betterStandardPrinter->print($attrGroup); + $attributesAsComments[] = new Comment($print); + + unset($attrGroup->attrs[$key]); + } + continue; } @@ -144,6 +161,12 @@ public function refactor(Node $node): ?Node // cleanup empty attr groups $this->cleanupEmptyAttrGroups($node); + if ($attributesAsComments !== []) { + $this->isDowngraded = true; + $currentComments = $node->getAttribute(AttributeKey::COMMENTS) ?? []; + $node->setAttribute(AttributeKey::COMMENTS, array_merge($currentComments, $attributesAsComments)); + } + if (! $this->isDowngraded) { return null; } diff --git a/tests/Issues/DowngradeAttributeSameLineWithPromotion/DowngradeAttributeSameLineWithPromotionTest.php b/tests/Issues/DowngradeAttributeSameLineWithPromotion/DowngradeAttributeSameLineWithPromotionTest.php new file mode 100644 index 00000000..74d9afa8 --- /dev/null +++ b/tests/Issues/DowngradeAttributeSameLineWithPromotion/DowngradeAttributeSameLineWithPromotionTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Issues/DowngradeAttributeSameLineWithPromotion/Fixture/fixture.php.inc b/tests/Issues/DowngradeAttributeSameLineWithPromotion/Fixture/fixture.php.inc new file mode 100644 index 00000000..a77731d6 --- /dev/null +++ b/tests/Issues/DowngradeAttributeSameLineWithPromotion/Fixture/fixture.php.inc @@ -0,0 +1,28 @@ + +----- +a = $a; + } +} + +?> diff --git a/tests/Issues/DowngradeAttributeSameLineWithPromotion/config/configured_rule.php b/tests/Issues/DowngradeAttributeSameLineWithPromotion/config/configured_rule.php new file mode 100644 index 00000000..563057ac --- /dev/null +++ b/tests/Issues/DowngradeAttributeSameLineWithPromotion/config/configured_rule.php @@ -0,0 +1,11 @@ +rules([DowngradeAttributeToAnnotationRector::class, DowngradePropertyPromotionRector::class]); +};