Skip to content
Merged
Show file tree
Hide file tree
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
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]);
};
Loading