From 3116c2b239b44d51aa7c39f25cbb5976adc44c3b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 25 Nov 2024 15:46:24 +0700 Subject: [PATCH 1/2] [Downgradephp80] Better add new line handling with insert \n prefix for same line attribute --- .../Class_/DowngradeAttributeToAnnotationRector.php | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php b/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php index f323951b..dbc1fae5 100644 --- a/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php +++ b/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php @@ -119,10 +119,9 @@ public function refactor(Node $node): ?Node (string) $oldTokens[$attrGroup->getEndTokenPos() + 1], "\n" )) { - $print = $this->betterStandardPrinter->print($attrGroup); - $attributesAsComments[] = new Comment($print); - - unset($attrGroup->attrs[$key]); + // add new line + $oldTokens[$attrGroup->getEndTokenPos() + 1]->text = "\n" . $oldTokens[$attrGroup->getEndTokenPos() + 1]->text; + $this->isDowngraded = true; } continue; @@ -161,12 +160,6 @@ 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; } From 4c5c4472b7d0ec51180e85e59d7bd3619c2120f3 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Mon, 25 Nov 2024 08:48:03 +0000 Subject: [PATCH 2/2] [ci-review] Rector Rectify --- .../Rector/Class_/DowngradeAttributeToAnnotationRector.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php b/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php index dbc1fae5..c20e2f00 100644 --- a/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php +++ b/rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php @@ -4,7 +4,6 @@ namespace Rector\DowngradePhp80\Rector\Class_; -use PhpParser\Comment; use PhpParser\Node; use PhpParser\Node\Attribute; use PhpParser\Node\Param; @@ -20,8 +19,6 @@ 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; @@ -49,8 +46,7 @@ final class DowngradeAttributeToAnnotationRector extends AbstractRector implemen public function __construct( private readonly DoctrineAnnotationFactory $doctrineAnnotationFactory, private readonly DocBlockUpdater $docBlockUpdater, - private readonly PhpDocInfoFactory $phpDocInfoFactory, - private readonly BetterStandardPrinter $betterStandardPrinter + private readonly PhpDocInfoFactory $phpDocInfoFactory ) { } @@ -109,7 +105,6 @@ public function refactor(Node $node): ?Node $this->isDowngraded = false; $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); - $attributesAsComments = []; $oldTokens = $this->file->getOldTokens(); foreach ($node->attrGroups as $attrGroup) {