Skip to content

Commit

Permalink
[NodeTypeResolver] Add AssignedToNodeVisitor (#3694)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Apr 27, 2023
1 parent 2996a6e commit f19c86c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor;

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\NodeVisitorAbstract;
use Rector\NodeTypeResolver\Node\AttributeKey;

/**
* Inspired by https://github.com/phpstan/phpstan-src/blob/1.7.x/src/Parser/NewAssignedToPropertyVisitor.php
*/
final class AssignedToNodeVisitor extends NodeVisitorAbstract
{
public function enterNode(Node $node): ?Node
{
if (! $node instanceof Assign) {
return null;
}

$node->expr->setAttribute(AttributeKey::ASSIGNED_TO, $node->var);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
use Rector\Core\Util\Reflection\PrivatesAccessor;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\AssignedToNodeVisitor;
use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\RemoveDeepChainMethodCallNodeVisitor;
use Webmozart\Assert\Assert;

Expand All @@ -72,6 +73,7 @@ public function __construct(
private readonly NodeScopeResolver $nodeScopeResolver,
private readonly ReflectionProvider $reflectionProvider,
RemoveDeepChainMethodCallNodeVisitor $removeDeepChainMethodCallNodeVisitor,
AssignedToNodeVisitor $assignedToNodeVisitor,
private readonly ScopeFactory $scopeFactory,
private readonly PrivatesAccessor $privatesAccessor,
private readonly NodeNameResolver $nodeNameResolver,
Expand All @@ -80,6 +82,7 @@ public function __construct(
) {
$this->nodeTraverser = new NodeTraverser();
$this->nodeTraverser->addVisitor($removeDeepChainMethodCallNodeVisitor);
$this->nodeTraverser->addVisitor($assignedToNodeVisitor);
}

/**
Expand Down

0 comments on commit f19c86c

Please sign in to comment.