Skip to content

Commit

Permalink
[Php81] Skip ReadOnlyPropertyRector on read only class (#3236)
Browse files Browse the repository at this point in the history
* [Php81] Skip ReadOnlyPropertyRector on read only class

* fixture class

* [ci-review] Rector Rectify

* Final touch: eol

* Final touch: make check readonly class on last resort check to make it faster

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Dec 22, 2022
1 parent 72f57a1 commit ea22343
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

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

final readonly class SkipOnReadonlyClass
{
public function __construct(
private string $name
) {
}

public function getName()
{
return $this->name;
}
}
18 changes: 18 additions & 0 deletions rules/Php81/Rector/Property/ReadOnlyPropertyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ public function provideMinPhpVersion(): int
return PhpVersionFeature::READONLY_PROPERTY;
}

private function shouldSkipInReadonlyClass(Property|Param $node): bool
{
$class = $this->betterNodeFinder->findParentType($node, Class_::class);
if (! $class instanceof Class_) {
return true;
}

return $class->isReadonly();
}

private function refactorProperty(Property $property): ?Property
{
// 1. is property read-only?
Expand Down Expand Up @@ -137,6 +147,10 @@ private function refactorProperty(Property $property): ?Property
return null;
}

if ($this->shouldSkipInReadonlyClass($property)) {
return null;
}

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

$attributeGroups = $property->attrGroups;
Expand Down Expand Up @@ -174,6 +188,10 @@ private function refactorParam(Param $param): Param | null
return null;
}

if ($this->shouldSkipInReadonlyClass($param)) {
return null;
}

$this->visibilityManipulator->makeReadonly($param);
return $param;
}
Expand Down

0 comments on commit ea22343

Please sign in to comment.