Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Downgradephp80] Better add new line handling with insert \n prefix for same line attribute DowngradeAttributeToAnnotationRector skipped class #252

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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) {
@@ -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;
@@ -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;
}
Loading