Skip to content

Commit 4201862

Browse files
[DowngradePhp80] Downgrade same line attribute on non-removed attribute on DowngradeAttributeToAnnotationRector (#251)
* [DowngradePhp80] Downgrade same line attribute on non-removed attribute on DowngradeAttributeToAnnotationRector * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <[email protected]>
1 parent ecd33d6 commit 4201862

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

rules/DowngradePhp80/Rector/Class_/DowngradeAttributeToAnnotationRector.php

Lines changed: 23 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,22 @@ 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(
119+
(string) $oldTokens[$attrGroup->getEndTokenPos() + 1],
120+
"\n"
121+
)) {
122+
$print = $this->betterStandardPrinter->print($attrGroup);
123+
$attributesAsComments[] = new Comment($print);
124+
125+
unset($attrGroup->attrs[$key]);
126+
}
127+
111128
continue;
112129
}
113130

@@ -144,6 +161,12 @@ public function refactor(Node $node): ?Node
144161
// cleanup empty attr groups
145162
$this->cleanupEmptyAttrGroups($node);
146163

164+
if ($attributesAsComments !== []) {
165+
$this->isDowngraded = true;
166+
$currentComments = $node->getAttribute(AttributeKey::COMMENTS) ?? [];
167+
$node->setAttribute(AttributeKey::COMMENTS, array_merge($currentComments, $attributesAsComments));
168+
}
169+
147170
if (! $this->isDowngraded) {
148171
return null;
149172
}
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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([DowngradeAttributeToAnnotationRector::class, DowngradePropertyPromotionRector::class]);
11+
};

0 commit comments

Comments
 (0)