Skip to content

Commit

Permalink
[DowngradePhp80] Downgrade same line attribute on non-removed attribu…
Browse files Browse the repository at this point in the history
…te 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]>
  • Loading branch information
samsonasik and actions-user authored Nov 24, 2024
1 parent ecd33d6 commit 4201862
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\DowngradePhp80\Rector\Class_;

use PhpParser\Comment;
use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\Node\Param;
Expand All @@ -19,6 +20,8 @@
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 @@ -47,6 +50,7 @@ public function __construct(
private readonly DoctrineAnnotationFactory $doctrineAnnotationFactory,
private readonly DocBlockUpdater $docBlockUpdater,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly BetterStandardPrinter $betterStandardPrinter
) {
}

Expand Down Expand Up @@ -105,9 +109,22 @@ public function refactor(Node $node): ?Node
$this->isDowngraded = false;

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

foreach ($node->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $key => $attribute) {
if ($this->shouldSkipAttribute($attribute)) {
if (isset($oldTokens[$attrGroup->getEndTokenPos() + 1]) && ! str_contains(
(string) $oldTokens[$attrGroup->getEndTokenPos() + 1],
"\n"
)) {
$print = $this->betterStandardPrinter->print($attrGroup);
$attributesAsComments[] = new Comment($print);

unset($attrGroup->attrs[$key]);
}

continue;
}

Expand Down Expand Up @@ -144,6 +161,12 @@ 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Issues\DowngradeAttributeSameLineWithPromotion;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DowngradeAttributeSameLineWithPromotionTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Rector\Tests\Issues\DowngradeAttributeSameLineWithPromotion\Fixture;

#[\Attribute]final class WithSameLine
{
public function __construct(public string $a)
{
}
}

?>
-----
<?php

namespace Rector\Tests\Issues\DowngradeAttributeSameLineWithPromotion\Fixture;

#[\Attribute]
final class WithSameLine
{
public string $a;
public function __construct(string $a)
{
$this->a = $a;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector;
use Rector\DowngradePhp80\Rector\Class_\DowngradePropertyPromotionRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([DowngradeAttributeToAnnotationRector::class, DowngradePropertyPromotionRector::class]);
};

0 comments on commit 4201862

Please sign in to comment.