Skip to content

Commit

Permalink
[ReadWrite] Remove parent lookup on LocalPropertyFetchReadNodeAnalyzer (
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Jun 17, 2023
1 parent 8550c09 commit 5f976fa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Stmt\Class_;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\PhpParser\ClassLikeAstResolver;
use Rector\Core\PhpParser\NodeFinder\PropertyFetchFinder;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\ReadWrite\Contract\ReadNodeAnalyzerInterface;

Expand All @@ -22,7 +24,8 @@ public function __construct(
private readonly JustReadExprAnalyzer $justReadExprAnalyzer,
private readonly PropertyFetchFinder $propertyFetchFinder,
private readonly NodeNameResolver $nodeNameResolver,
private readonly BetterNodeFinder $betterNodeFinder
private readonly ReflectionResolver $reflectionResolver,
private readonly ClassLikeAstResolver $classLikeAstResolver
) {
}

Expand All @@ -33,8 +36,8 @@ public function supports(Expr $expr): bool

public function isRead(Expr $expr): bool
{
$class = $this->betterNodeFinder->findParentType($expr, Class_::class);
if (! $class instanceof Class_) {
$classReflection = $this->reflectionResolver->resolveClassReflection($expr);
if (! $classReflection instanceof ClassReflection || ! $classReflection->isClass()) {
// assume worse to keep node protected
return true;
}
Expand All @@ -45,7 +48,10 @@ public function isRead(Expr $expr): bool
return true;
}

/** @var Class_ $class */
$class = $this->classLikeAstResolver->resolveClassFromClassReflection($classReflection);
$propertyFetches = $this->propertyFetchFinder->findLocalPropertyFetchesByName($class, $propertyName);

foreach ($propertyFetches as $propertyFetch) {
if ($this->justReadExprAnalyzer->isReadContext($propertyFetch)) {
return true;
Expand Down
25 changes: 14 additions & 11 deletions src/PhpParser/NodeFinder/PropertyFetchFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\Core\PhpParser\NodeFinder;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
Expand Down Expand Up @@ -60,19 +61,21 @@ public function findPrivatePropertyFetches(Class_ $class, Property | Param $prop
*/
public function findLocalPropertyFetchesByName(Class_ $class, string $paramName): array
{
/** @var PropertyFetch[]|StaticPropertyFetch[] $propertyFetches */
$propertyFetches = $this->betterNodeFinder->findInstancesOf(
$class,
[PropertyFetch::class, StaticPropertyFetch::class]
);

$foundPropertyFetches = [];
/** @var PropertyFetch[]|StaticPropertyFetch[] $foundPropertyFetches */
$foundPropertyFetches = $this->betterNodeFinder->find(
$class->getMethods(),
function (Node $subNode) use ($paramName): bool {
if ($subNode instanceof PropertyFetch) {
return $this->propertyFetchAnalyzer->isLocalPropertyFetchName($subNode, $paramName);
}

if ($subNode instanceof StaticPropertyFetch) {
return $this->propertyFetchAnalyzer->isLocalPropertyFetchName($subNode, $paramName);
}

foreach ($propertyFetches as $propertyFetch) {
if ($this->propertyFetchAnalyzer->isLocalPropertyFetchName($propertyFetch, $paramName)) {
$foundPropertyFetches[] = $propertyFetch;
return false;
}
}
);

return $foundPropertyFetches;
}
Expand Down

0 comments on commit 5f976fa

Please sign in to comment.