Skip to content

Commit

Permalink
[Privatization] Optimize ParentPropertyLookupGuard, make $class param…
Browse files Browse the repository at this point in the history
…eter required (#3252)

* [Privatization] Optimize ParentPropertyLookupGuard, make $class parameter required

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Dec 25, 2022
1 parent 9a0938d commit 55c9472
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion rules/Php74/Guard/PropertyTypeChangeGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ private function isSafeProtectedProperty(Property $property): bool
return false;
}

return $this->parentPropertyLookupGuard->isLegal($property);
return $this->parentPropertyLookupGuard->isLegal($property, $parentNode);
}
}
16 changes: 7 additions & 9 deletions rules/Privatization/Guard/ParentPropertyLookupGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\NodeManipulator\PropertyManipulator;
use Rector\Core\PhpParser\AstResolver;
Expand All @@ -28,21 +29,18 @@ public function __construct(
private readonly NodeNameResolver $nodeNameResolver,
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer,
private readonly AstResolver $astResolver,
private readonly PropertyManipulator $propertyManipulator
private readonly PropertyManipulator $propertyManipulator,
private readonly ClassAnalyzer $classAnalyzer,
) {
}

public function isLegal(Property $property, ?Class_ $class = null): bool
public function isLegal(Property $property, Class_ $class): bool
{
if (! $class instanceof Class_) {
// @todo optimize
$class = $this->betterNodeFinder->findParentType($property, Class_::class);
if (! $class instanceof Class_) {
return false;
}
if ($this->classAnalyzer->isAnonymousClass($class)) {
return false;
}

$classReflection = $this->reflectionResolver->resolveClassReflection($property);
$classReflection = $this->reflectionResolver->resolveClassReflection($class);
if (! $classReflection instanceof ClassReflection) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function refactor(Node $node): ?Node
continue;
}

if (! $this->parentPropertyLookupGuard->isLegal($property)) {
if (! $this->parentPropertyLookupGuard->isLegal($property, $node)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function refactor(Node $node): null|Class_
}

// include fault value in the type
if ($this->isConclictingDefaultExprType($property, $getterReturnType)) {
if ($this->isConflictingDefaultExprType($property, $getterReturnType)) {
continue;
}

Expand Down Expand Up @@ -148,7 +148,7 @@ private function decorateDefaultNull(Type $propertyType, Property $property): vo
$propertyProperty->default = $this->nodeFactory->createNull();
}

private function isConclictingDefaultExprType(Property $property, Type $getterReturnType): bool
private function isConflictingDefaultExprType(Property $property, Type $getterReturnType): bool
{
$onlyPropertyProperty = $property->props[0];
if (! $onlyPropertyProperty->default instanceof Expr) {
Expand Down

0 comments on commit 55c9472

Please sign in to comment.