Skip to content

Commit

Permalink
[Php82] Skip ReadOnlyClassRector on has parent non-readonly class (#3169
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoann-TYT committed Dec 7, 2022
1 parent 99ac002 commit 84b54c0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

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

class Foo
{

}

final class SkipIfClassExtendsFromAClassThatIsNotReadonly extends Foo
{
public function __construct()
{
}
}
17 changes: 16 additions & 1 deletion rules/Php82/Rector/Class_/ReadOnlyClassRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\MethodName;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\Core\ValueObject\Visibility;
Expand All @@ -31,7 +33,8 @@ final class ReadOnlyClassRector extends AbstractRector implements MinPhpVersionI
public function __construct(
private readonly ClassAnalyzer $classAnalyzer,
private readonly VisibilityManipulator $visibilityManipulator,
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer,
private readonly ReflectionResolver $reflectionResolver
) {
}

Expand Down Expand Up @@ -163,6 +166,18 @@ private function shouldSkipClass(Class_ $class): bool
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 84b54c0

Please sign in to comment.