Skip to content

Commit

Permalink
[Php81] Fix property with attribute inlined on ReadOnlyPropertyRector (
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Feb 25, 2022
1 parent 961c3ae commit 4dee58b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;

final class WithAttribute
{
#[Serializer\Since(Option::SINCE_20211124)]
private string $name;

public function __construct(string $name)
{
$this->name = $name;
}

public function getName()
{
return $this->name;
}
}

?>
-----
<?php

namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;

final class WithAttribute
{
#[Serializer\Since(Option::SINCE_20211124)]
private readonly string $name;

public function __construct(string $name)
{
$this->name = $name;
}

public function getName()
{
return $this->name;
}
}

?>
7 changes: 7 additions & 0 deletions rules/Php81/Rector/Property/ReadOnlyPropertyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\Core\ValueObject\Visibility;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -117,6 +118,12 @@ private function refactorProperty(Property $property): ?Property
}

$this->visibilityManipulator->makeReadonly($property);

$attributeGroups = $property->attrGroups;
if ($attributeGroups !== []) {
$property->setAttribute(AttributeKey::ORIGINAL_NODE, null);
}

return $property;
}

Expand Down

0 comments on commit 4dee58b

Please sign in to comment.