Skip to content

Commit

Permalink
[Privatization] Use PropertyManipulator::isUsedByTrait() on ParentPro…
Browse files Browse the repository at this point in the history
…pertyLookupGuard for PrivatizeFinalClassPropertyRector (#3185)
  • Loading branch information
samsonasik committed Dec 11, 2022
1 parent 802d98e commit 5ccf468
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 52 deletions.
17 changes: 11 additions & 6 deletions rules/Privatization/Guard/ParentPropertyLookupGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPStan\Reflection\ClassReflection;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\NodeManipulator\PropertyManipulator;
use Rector\Core\PhpParser\AstResolver;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Reflection\ReflectionResolver;
Expand All @@ -26,7 +27,8 @@ public function __construct(
private readonly ReflectionResolver $reflectionResolver,
private readonly NodeNameResolver $nodeNameResolver,
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer,
private readonly AstResolver $astResolver
private readonly AstResolver $astResolver,
private readonly PropertyManipulator $propertyManipulator
) {
}

Expand All @@ -38,18 +40,21 @@ public function isLegal(Property $property): bool
return false;
}

if ($class->extends === null) {
return true;
}

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

$propertyName = $this->nodeNameResolver->getName($property);
$className = $classReflection->getName();
if ($this->propertyManipulator->isUsedByTrait($classReflection, $propertyName)) {
return false;
}

if ($class->extends === null) {
return true;
}

$className = $classReflection->getName();
$parents = $classReflection->getParents();

// parent class not autoloaded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\TraitUse;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Core\Rector\AbstractRector;
use Rector\Privatization\Guard\ParentPropertyLookupGuard;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
Expand All @@ -23,7 +21,6 @@ final class PrivatizeFinalClassPropertyRector extends AbstractRector
public function __construct(
private readonly VisibilityManipulator $visibilityManipulator,
private readonly ParentPropertyLookupGuard $parentPropertyLookupGuard,
private readonly ReflectionProvider $reflectionProvider,
) {
}

Expand Down Expand Up @@ -65,12 +62,9 @@ public function refactor(Node $node): ?Node
return null;
}

$traitPropertyNames = $this->resolveTraitPropertyNames($node);

$hasChanged = false;

foreach ($node->getProperties() as $property) {
if ($this->shouldSkipProperty($property, $traitPropertyNames)) {
if ($this->shouldSkipProperty($property)) {
continue;
}

Expand All @@ -89,49 +83,12 @@ public function refactor(Node $node): ?Node
return null;
}

/**
* @return string[]
*/
private function resolveTraitPropertyNames(Class_ $class): array
{
$traitPropertyNames = [];

foreach ($class->stmts as $classStmt) {
if (! $classStmt instanceof TraitUse) {
continue;
}

foreach ($classStmt->traits as $trait) {
$traitName = $this->getName($trait);
if (! $this->reflectionProvider->hasClass($traitName)) {
continue;
}

$traitReflection = $this->reflectionProvider->getClass($traitName);
$nativeTraitReflection = $traitReflection->getNativeReflection();

foreach ($nativeTraitReflection->getProperties() as $propertyReflection) {
$traitPropertyNames[] = $propertyReflection->getName();
}
}
}

return $traitPropertyNames;
}

/**
* @param string[] $traitPropertyNames
*/
private function shouldSkipProperty(Property $property, array $traitPropertyNames): bool
private function shouldSkipProperty(Property $property): bool
{
if (count($property->props) !== 1) {
return true;
}

if (! $property->isProtected()) {
return true;
}

return $this->isNames($property, $traitPropertyNames);
return ! $property->isProtected();
}
}

0 comments on commit 5ccf468

Please sign in to comment.