Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions rules/Php82/NodeManipulator/ReadonlyClassManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
namespace Rector\Php82\NodeManipulator;

use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\Analyser\Scope;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionProperty;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\Php81\Enum\AttributeName;
use Rector\PHPStan\ScopeFetcher;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\ValueObject\MethodName;
use Rector\ValueObject\Visibility;
Expand All @@ -32,8 +31,9 @@ public function __construct(

public function process(Class_ $class): Class_|null
{
$scope = ScopeFetcher::fetch($class);
if ($this->shouldSkip($class, $scope)) {
$parentClassReflections = $this->resolveParentClassReflections($class);

if ($this->shouldSkip($class, $parentClassReflections)) {
return null;
}

Expand All @@ -55,16 +55,24 @@ public function process(Class_ $class): Class_|null
}

/**
* Resolve parents from the extends name, so anonymous classes (no name) work too.
*
* @return ClassReflection[]
*/
private function resolveParentClassReflections(Scope $scope): array
private function resolveParentClassReflections(Class_ $class): array
{
$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
if (! $class->extends instanceof Name) {
return [];
}

$parentName = $class->extends->toString();
if (! $this->reflectionProvider->hasClass($parentName)) {
return [];
}

return $classReflection->getParents();
$parentClassReflection = $this->reflectionProvider->getClass($parentName);

return [$parentClassReflection, ...$parentClassReflection->getParents()];
}

/**
Expand All @@ -75,25 +83,21 @@ private function hasNonTypedProperty(array $properties): bool
return array_any($properties, fn (Property $property): bool => ! $property->type instanceof Node);
}

private function shouldSkip(Class_ $class, Scope $scope): bool
/**
* @param ClassReflection[] $parentClassReflections
*/
private function shouldSkip(Class_ $class, array $parentClassReflections): bool
{
$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return true;
}

if ($this->shouldSkipClass($class)) {
return true;
}

$parents = $this->resolveParentClassReflections($scope);

if (! $class->isAnonymous() && ! $class->isFinal()) {
return ! $this->isExtendsReadonlyClass($parents);
return ! $this->isExtendsReadonlyClass($parentClassReflections);
}

foreach ($parents as $parent) {
if (! $parent->isReadOnly()) {
foreach ($parentClassReflections as $parentClassReflection) {
if (! $parentClassReflection->isReadOnly()) {
return true;
}
}
Expand Down
Loading