Skip to content

Commit

Permalink
Handle readonly class with attrs (#5736)
Browse files Browse the repository at this point in the history
  • Loading branch information
tugmaks committed Mar 19, 2024
1 parent b71d0fe commit b052ed9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rector\Tests\Php82\Rector\Class_\ReadOnlyClassRector\Fixture;

final class WithAttributeOnProperty
{
#[MyAttr]
public readonly string $id;
}

?>
-----
<?php

namespace Rector\Tests\Php82\Rector\Class_\ReadOnlyClassRector\Fixture;

final readonly class WithAttributeOnProperty
{
#[MyAttr]
public string $id;
}

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

namespace Rector\Tests\Php82\Rector\Class_\ReadOnlyClassRector\Fixture;

final class WithAttributeOnPropertyPromotion
{
private function __construct(
#[MyAttr]
private readonly string $id
){}
}

?>
-----
<?php

namespace Rector\Tests\Php82\Rector\Class_\ReadOnlyClassRector\Fixture;

final readonly class WithAttributeOnPropertyPromotion
{
private function __construct(
#[MyAttr]
private string $id
){}
}

?>
10 changes: 10 additions & 0 deletions rules/Php82/Rector/Class_/ReadOnlyClassRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,21 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
if ($constructClassMethod instanceof ClassMethod) {
foreach ($constructClassMethod->getParams() as $param) {
$this->visibilityManipulator->removeReadonly($param);

if ($param->attrGroups !== []) {
// invoke reprint with correct newline
$param->setAttribute(AttributeKey::ORIGINAL_NODE, null);
}
}
}

foreach ($node->getProperties() as $property) {
$this->visibilityManipulator->removeReadonly($property);

if ($property->attrGroups !== []) {
// invoke reprint with correct newline
$property->setAttribute(AttributeKey::ORIGINAL_NODE, null);
}
}

if ($node->attrGroups !== []) {
Expand Down

0 comments on commit b052ed9

Please sign in to comment.