Skip to content

Commit 207269f

Browse files
[Downgradephp80] Better add new line handling with insert \n prefix for 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]>
1 parent 4201862 commit 207269f

File tree

1 file changed

+4
-16
lines changed

1 file changed

+4
-16
lines changed

rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Rector\DowngradePhp80\Rector\Class_;
66

7-
use PhpParser\Comment;
87
use PhpParser\Node;
98
use PhpParser\Node\Attribute;
109
use PhpParser\Node\Param;
@@ -20,8 +19,6 @@
2019
use Rector\Contract\Rector\ConfigurableRectorInterface;
2120
use Rector\DowngradePhp80\ValueObject\DowngradeAttributeToAnnotation;
2221
use Rector\NodeFactory\DoctrineAnnotationFactory;
23-
use Rector\NodeTypeResolver\Node\AttributeKey;
24-
use Rector\PhpParser\Printer\BetterStandardPrinter;
2522
use Rector\Rector\AbstractRector;
2623
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
2724
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -49,8 +46,7 @@ final class DowngradeAttributeToAnnotationRector extends AbstractRector implemen
4946
public function __construct(
5047
private readonly DoctrineAnnotationFactory $doctrineAnnotationFactory,
5148
private readonly DocBlockUpdater $docBlockUpdater,
52-
private readonly PhpDocInfoFactory $phpDocInfoFactory,
53-
private readonly BetterStandardPrinter $betterStandardPrinter
49+
private readonly PhpDocInfoFactory $phpDocInfoFactory
5450
) {
5551
}
5652

@@ -109,7 +105,6 @@ public function refactor(Node $node): ?Node
109105
$this->isDowngraded = false;
110106

111107
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
112-
$attributesAsComments = [];
113108
$oldTokens = $this->file->getOldTokens();
114109

115110
foreach ($node->attrGroups as $attrGroup) {
@@ -119,10 +114,9 @@ public function refactor(Node $node): ?Node
119114
(string) $oldTokens[$attrGroup->getEndTokenPos() + 1],
120115
"\n"
121116
)) {
122-
$print = $this->betterStandardPrinter->print($attrGroup);
123-
$attributesAsComments[] = new Comment($print);
124-
125-
unset($attrGroup->attrs[$key]);
117+
// add new line
118+
$oldTokens[$attrGroup->getEndTokenPos() + 1]->text = "\n" . $oldTokens[$attrGroup->getEndTokenPos() + 1]->text;
119+
$this->isDowngraded = true;
126120
}
127121

128122
continue;
@@ -161,12 +155,6 @@ public function refactor(Node $node): ?Node
161155
// cleanup empty attr groups
162156
$this->cleanupEmptyAttrGroups($node);
163157

164-
if ($attributesAsComments !== []) {
165-
$this->isDowngraded = true;
166-
$currentComments = $node->getAttribute(AttributeKey::COMMENTS) ?? [];
167-
$node->setAttribute(AttributeKey::COMMENTS, array_merge($currentComments, $attributesAsComments));
168-
}
169-
170158
if (! $this->isDowngraded) {
171159
return null;
172160
}

0 commit comments

Comments
 (0)