Skip to content

Commit

Permalink
[Php82] Handle parent already readonly on ReadOnlyClassRector (#3199)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Dec 14, 2022
1 parent c21cafa commit be3bdd0
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

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

use Rector\Tests\Php82\Rector\Class_\ReadOnlyClassRector\Source\ParentAlreadyReadonly;

/**
* Even class is not final, if extends class that already readonly, it must be readonly as well
*/
class ChildExtendsParentAlreadyReadonly extends ParentAlreadyReadonly
{
}

?>
-----
<?php

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

use Rector\Tests\Php82\Rector\Class_\ReadOnlyClassRector\Source\ParentAlreadyReadonly;

/**
* Even class is not final, if extends class that already readonly, it must be readonly as well
*/
readonly class ChildExtendsParentAlreadyReadonly extends ParentAlreadyReadonly
{
}

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

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

readonly abstract class ParentAlreadyReadonly
{
}
76 changes: 55 additions & 21 deletions rules/Php82/Rector/Class_/ReadOnlyClassRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,58 @@ public function provideMinPhpVersion(): int
return PhpVersionFeature::READONLY_CLASS;
}

/**
* @return ClassReflection[]
*/
private function resolveParentClassReflections(Class_ $class): array
{
$classReflection = $this->reflectionResolver->resolveClassReflection($class);
if (! $classReflection instanceof ClassReflection) {
return [];
}

return $classReflection->getParents();
}

/**
* @param Property[] $properties
*/
private function hasNonTypedProperty(array $properties): bool
{
foreach ($properties as $property) {
// properties of readonly class must always have type
if ($property->type === null) {
return true;
}
}

return false;
}

private function shouldSkip(Class_ $class): bool
{
if ($this->shouldSkipClass($class)) {
return true;
}

$parents = $this->resolveParentClassReflections($class);
if (! $class->isFinal()) {
return ! $this->isExtendsReadonlyClass($parents);
}

foreach ($parents as $parent) {
if (! $parent->isReadOnly()) {
return true;
}
}

$properties = $class->getProperties();
if ($this->hasWritableProperty($properties)) {
return true;
}

foreach ($properties as $property) {
// properties of readonly class must always have type
if ($property->type === null) {
return true;
}
if ($this->hasNonTypedProperty($properties)) {
return true;
}

$constructClassMethod = $class->getMethod(MethodName::CONSTRUCT);
Expand All @@ -137,6 +173,20 @@ private function shouldSkip(Class_ $class): bool
return $this->shouldSkipParams($params);
}

/**
* @param ClassReflection[] $parents
*/
private function isExtendsReadonlyClass(array $parents): bool
{
foreach ($parents as $parent) {
if ($parent->isReadOnly()) {
return true;
}
}

return false;
}

/**
* @param Property[] $properties
*/
Expand All @@ -162,22 +212,6 @@ private function shouldSkipClass(Class_ $class): bool
return true;
}

if (! $class->isFinal()) {
return true;
}

$classReflection = $this->reflectionResolver->resolveClassReflection($class);
if (! $classReflection instanceof ClassReflection) {
return true;
}

$parents = $classReflection->getParents();
foreach ($parents as $parent) {
if (! $parent->isReadOnly()) {
return true;
}
}

return $this->phpAttributeAnalyzer->hasPhpAttribute($class, AttributeName::ALLOW_DYNAMIC_PROPERTIES);
}

Expand Down

0 comments on commit be3bdd0

Please sign in to comment.