Skip to content

Commit

Permalink
[NodeManipulator] Remove parent attribute usage on AssignManipulator (#…
Browse files Browse the repository at this point in the history
…4394)

* [NodeManipulator] Remove parent attribute usage on AssignManipulator

* Fix
  • Loading branch information
samsonasik committed Jul 1, 2023
1 parent 07859a1 commit 51cc067
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignOp;
use PhpParser\NodeVisitorAbstract;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface;
Expand All @@ -17,7 +18,7 @@ final class AssignedToNodeVisitor extends NodeVisitorAbstract implements ScopeRe
{
public function enterNode(Node $node): ?Node
{
if (! $node instanceof Assign) {
if (! $node instanceof Assign && ! $node instanceof AssignOp) {
return null;
}

Expand Down
77 changes: 2 additions & 75 deletions src/NodeManipulator/AssignManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,23 @@
namespace Rector\Core\NodeManipulator;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignOp;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\List_;
use PhpParser\Node\Expr\PostDec;
use PhpParser\Node\Expr\PostInc;
use PhpParser\Node\Expr\PreDec;
use PhpParser\Node\Expr\PreInc;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\FunctionLike;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Util\MultiInstanceofChecker;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;

final class AssignManipulator
{
/**
* @var array<class-string<Expr>>
*/
private const MODIFYING_NODE_TYPES = [
Assign::class,
AssignOp::class,
PreDec::class,
PostDec::class,
PreInc::class,
PostInc::class,
];

public function __construct(
private readonly NodeNameResolver $nodeNameResolver,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer,
private readonly MultiInstanceofChecker $multiInstanceofChecker,
private readonly NodeComparator $nodeComparator
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer
) {
}

Expand All @@ -69,41 +44,7 @@ public function isListToEachAssign(Assign $assign): bool

public function isLeftPartOfAssign(Node $node): bool
{
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof Node && $this->multiInstanceofChecker->isInstanceOf(
$parentNode,
self::MODIFYING_NODE_TYPES
)) {
/**
* @var Assign|AssignOp|PreDec|PostDec|PreInc|PostInc $parentNode
*
* Compare same node to ensure php_doc_info info not be checked
*/
return $this->nodeComparator->areSameNode($parentNode->var, $node);
}

if ($this->isOnArrayDestructuring($parentNode)) {
return true;
}

// traverse up to array dim fetches
if ($parentNode instanceof ArrayDimFetch) {
$previousParent = $parentNode;
while ($parentNode instanceof ArrayDimFetch) {
$previousParent = $parentNode;
$parentNode = $parentNode->getAttribute(AttributeKey::PARENT_NODE);
}

if ($parentNode instanceof Node && $this->multiInstanceofChecker->isInstanceOf(
$parentNode,
self::MODIFYING_NODE_TYPES
)) {
/** @var Assign|AssignOp|PreDec|PostDec|PreInc|PostInc $parentNode */
return $parentNode->var === $previousParent;
}
}

return false;
return $node->getAttribute(AttributeKey::IS_BEING_ASSIGNED) === true;
}

/**
Expand All @@ -120,18 +61,4 @@ public function resolveAssignsToLocalPropertyFetches(FunctionLike $functionLike)
return $this->isLeftPartOfAssign($node);
});
}

private function isOnArrayDestructuring(?Node $parentNode): bool
{
if (! $parentNode instanceof ArrayItem) {
return false;
}

$node = $parentNode->getAttribute(AttributeKey::PARENT_NODE);
if (! $node instanceof Array_) {
return false;
}

return $node->getAttribute(AttributeKey::IS_BEING_ASSIGNED) === true;
}
}

0 comments on commit 51cc067

Please sign in to comment.