Skip to content

Commit adbbd2c

Browse files
committed
[DowngradePhp80] Downgrade same line attribute on non-removed attribute on DowngradeAttributeToAnnotationRector
1 parent ecd33d6 commit adbbd2c

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php

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

55
namespace Rector\DowngradePhp80\Rector\Class_;
66

7+
use PhpParser\Comment;
78
use PhpParser\Node;
89
use PhpParser\Node\Attribute;
910
use PhpParser\Node\Param;
@@ -19,6 +20,8 @@
1920
use Rector\Contract\Rector\ConfigurableRectorInterface;
2021
use Rector\DowngradePhp80\ValueObject\DowngradeAttributeToAnnotation;
2122
use Rector\NodeFactory\DoctrineAnnotationFactory;
23+
use Rector\NodeTypeResolver\Node\AttributeKey;
24+
use Rector\PhpParser\Printer\BetterStandardPrinter;
2225
use Rector\Rector\AbstractRector;
2326
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
2427
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -47,6 +50,7 @@ public function __construct(
4750
private readonly DoctrineAnnotationFactory $doctrineAnnotationFactory,
4851
private readonly DocBlockUpdater $docBlockUpdater,
4952
private readonly PhpDocInfoFactory $phpDocInfoFactory,
53+
private readonly BetterStandardPrinter $betterStandardPrinter
5054
) {
5155
}
5256

@@ -105,9 +109,19 @@ public function refactor(Node $node): ?Node
105109
$this->isDowngraded = false;
106110

107111
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
112+
$attributesAsComments = [];
113+
$oldTokens = $this->file->getOldTokens();
114+
108115
foreach ($node->attrGroups as $attrGroup) {
109116
foreach ($attrGroup->attrs as $key => $attribute) {
110117
if ($this->shouldSkipAttribute($attribute)) {
118+
if (isset($oldTokens[$attrGroup->getEndTokenPos() + 1]) && ! str_contains((string) $oldTokens[$attrGroup->getEndTokenPos() + 1], "\n")) {
119+
$print = $this->betterStandardPrinter->print($attrGroup);
120+
$attributesAsComments[] = new Comment($print);
121+
122+
unset($attrGroup->attrs[$key]);
123+
}
124+
111125
continue;
112126
}
113127

@@ -144,6 +158,12 @@ public function refactor(Node $node): ?Node
144158
// cleanup empty attr groups
145159
$this->cleanupEmptyAttrGroups($node);
146160

161+
if ($attributesAsComments !== []) {
162+
$this->isDowngraded = true;
163+
$currentComments = $node->getAttribute(AttributeKey::COMMENTS) ?? [];
164+
$node->setAttribute(AttributeKey::COMMENTS, array_merge($currentComments, $attributesAsComments));
165+
}
166+
147167
if (! $this->isDowngraded) {
148168
return null;
149169
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Issues\DowngradeAttributeSameLineWithPromotion;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class DowngradeAttributeSameLineWithPromotionTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Rector\Tests\Issues\DowngradeAttributeSameLineWithPromotion\Fixture;
4+
5+
#[\Attribute]final class WithSameLine
6+
{
7+
public function __construct(public string $a)
8+
{
9+
}
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\Issues\DowngradeAttributeSameLineWithPromotion\Fixture;
17+
18+
#[\Attribute]
19+
final class WithSameLine
20+
{
21+
public string $a;
22+
public function __construct(string $a)
23+
{
24+
$this->a = $a;
25+
}
26+
}
27+
28+
?>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector;
7+
use Rector\DowngradePhp80\Rector\Class_\DowngradePropertyPromotionRector;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->rules([
11+
DowngradeAttributeToAnnotationRector::class,
12+
DowngradePropertyPromotionRector::class
13+
]);
14+
};

0 commit comments

Comments
 (0)