Skip to content

Commit

Permalink
[Core] Ensure re-index node attributes (stmts, params, uses, args) on…
Browse files Browse the repository at this point in the history
… ChangedNodeScopeRefresher (#2458)
  • Loading branch information
samsonasik committed Jun 9, 2022
1 parent ff931e4 commit ba48cd2
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/Application/ChangedNodeScopeRefresher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,19 @@
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Property;
use PHPStan\Analyser\MutatingScope;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
Expand Down Expand Up @@ -70,14 +81,33 @@ public function refresh(Node $node, SmartFileInfo $smartFileInfo, ?MutatingScope
$node = new Property(0, [], [], null, [$attributeGroup]);
}

if ($node instanceof StmtsAwareInterface && $node->stmts !== null) {
$node->stmts = array_values($node->stmts);
}
$this->reIndexNodeAttributes($node);

$stmts = $this->resolveStmts($node);
$this->phpStanNodeScopeResolver->processNodes($stmts, $smartFileInfo, $mutatingScope);
}

private function reIndexNodeAttributes(Node $node): void
{
if (($node instanceof ClassLike || $node instanceof StmtsAwareInterface) && $node->stmts !== null) {
$node->stmts = array_values($node->stmts);
}

if ($node instanceof FunctionLike) {
/** @var ClassMethod|Function_|Closure $node */
$node->params = array_values($node->params);

if ($node instanceof Closure) {
$node->uses = array_values($node->uses);
}
}

if ($node instanceof CallLike) {
/** @var FuncCall|MethodCall|New_|NullsafeMethodCall|StaticCall $node */
$node->args = array_values($node->args);
}
}

/**
* @return Stmt[]
*/
Expand Down

0 comments on commit ba48cd2

Please sign in to comment.