Skip to content

Commit

Permalink
[Php82] Move consume trait property skip check to ReadOnlyClassRector (
Browse files Browse the repository at this point in the history
…#3316)

* [Php82] Move consume trait property to ReadOnlyClassRector

* cs fix

* add check property not readonly in trait

* [ci-review] Rector Rectify

* phsptan

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user authored Jan 29, 2023
1 parent 76a6df8 commit d2080c2
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 55 deletions.

This file was deleted.

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

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

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

final class SkipConsumeTraitProperty
{
use SomeTraitWithProperty;

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

public function getName()
{
return $this->name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Source;
namespace Rector\Tests\Php82\Rector\Class_\ReadOnlyClassRector\Source;

trait SomeTraitWithProperty
{
Expand Down
33 changes: 2 additions & 31 deletions rules/Php81/Rector/Property/ReadOnlyPropertyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PhpParser\NodeTraverser;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Core\NodeAnalyzer\ParamAnalyzer;
use Rector\Core\NodeManipulator\PropertyFetchAssignManipulator;
use Rector\Core\NodeManipulator\PropertyManipulator;
Expand All @@ -40,8 +39,7 @@ public function __construct(
private readonly PropertyManipulator $propertyManipulator,
private readonly PropertyFetchAssignManipulator $propertyFetchAssignManipulator,
private readonly ParamAnalyzer $paramAnalyzer,
private readonly VisibilityManipulator $visibilityManipulator,
private readonly ReflectionProvider $reflectionProvider
private readonly VisibilityManipulator $visibilityManipulator
) {
}

Expand Down Expand Up @@ -115,34 +113,7 @@ private function shouldSkipInReadonlyClass(Property|Param $node): bool
return true;
}

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

$traitUses = $class->getTraitUses();
if ($traitUses === []) {
return false;
}

foreach ($traitUses as $traitUse) {
foreach ($traitUse->traits as $trait) {
$traitName = $trait->toString();

// trait not autoloaded
if (! $this->reflectionProvider->hasClass($traitName)) {
return true;
}

$traitClassReflection = $this->reflectionProvider->getClass($traitName);
$nativeReflection = $traitClassReflection->getNativeReflection();

if ($nativeReflection->getProperties() !== []) {
return true;
}
}
}

return false;
return $class->isReadonly();
}

private function refactorProperty(Property $property): ?Property
Expand Down
47 changes: 46 additions & 1 deletion rules/Php82/Rector/Class_/ReadOnlyClassRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionProperty;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
Expand All @@ -34,7 +36,8 @@ public function __construct(
private readonly ClassAnalyzer $classAnalyzer,
private readonly VisibilityManipulator $visibilityManipulator,
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer,
private readonly ReflectionResolver $reflectionResolver
private readonly ReflectionResolver $reflectionResolver,
private readonly ReflectionProvider $reflectionProvider
) {
}

Expand Down Expand Up @@ -158,6 +161,10 @@ private function shouldSkip(Class_ $class): bool
return true;
}

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

$constructClassMethod = $class->getMethod(MethodName::CONSTRUCT);
if (! $constructClassMethod instanceof ClassMethod) {
// no __construct means no property promotion, skip if class has no property defined
Expand All @@ -173,6 +180,44 @@ private function shouldSkip(Class_ $class): bool
return $this->shouldSkipParams($params);
}

private function shouldSkipConsumeTraitProperty(Class_ $class): bool
{
$traitUses = $class->getTraitUses();
foreach ($traitUses as $traitUse) {
foreach ($traitUse->traits as $trait) {
$traitName = $trait->toString();

// trait not autoloaded
if (! $this->reflectionProvider->hasClass($traitName)) {
return true;
}

$traitClassReflection = $this->reflectionProvider->getClass($traitName);
$nativeReflection = $traitClassReflection->getNativeReflection();

if ($this->hasReadonlyProperty($nativeReflection->getProperties())) {
return true;
}
}
}

return false;
}

/**
* @param ReflectionProperty[] $properties
*/
private function hasReadonlyProperty(array $properties): bool
{
foreach ($properties as $property) {
if (! $property->isReadOnly()) {
return true;
}
}

return false;
}

/**
* @param ClassReflection[] $parents
*/
Expand Down

0 comments on commit d2080c2

Please sign in to comment.