Skip to content

Commit

Permalink
[Downgradephp80] Better add new line handling with insert \n prefix f…
Browse files Browse the repository at this point in the history
…or same line attribute DowngradeAttributeToAnnotationRector skipped class (#252)

* [Downgradephp80] Better add new line handling with insert \n prefix for same line attribute

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
samsonasik and actions-user authored Nov 25, 2024
1 parent 4201862 commit 207269f
Showing 1 changed file with 4 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\DowngradePhp80\Rector\Class_;

use PhpParser\Comment;
use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\Node\Param;
Expand All @@ -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;
Expand Down Expand Up @@ -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
) {
}

Expand Down Expand Up @@ -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) {
Expand All @@ -119,10 +114,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;
Expand Down Expand Up @@ -161,12 +155,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;
}
Expand Down

0 comments on commit 207269f

Please sign in to comment.