Skip to content

Commit

Permalink
[NodeTypeResolver] Re-index node attributes on refresh process
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Dec 18, 2024
1 parent 6024d31 commit 3c2f735
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function refactor(Node $node): ?Node
$originalArgs = $node->args;
unset($originalArgs[0]);

$methodCall->args = array_values($originalArgs);
$methodCall->args = $originalArgs;
return $methodCall;
}
}
6 changes: 0 additions & 6 deletions rules/Php80/NodeFactory/MatchArmsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ public function createFromCondAndExprs(array $condAndExprs): array
]);
}

foreach ($matchArms as $matchArm) {
if (is_array($matchArm->conds)) {
$matchArm->conds = array_values($matchArm->conds);
}
}

return $matchArms;
}
}
29 changes: 0 additions & 29 deletions src/Application/ChangedNodeScopeRefresher.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,35 +66,6 @@ public function refresh(Node $node, string $filePath, ?MutatingScope $mutatingSc
$this->phpStanNodeScopeResolver->processNodes($stmts, $filePath, $mutatingScope);
}

public function reIndexNodeAttributes(Node $node): void
{
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);
}

if ($node instanceof If_) {
$node->elseifs = array_values($node->elseifs);
}

if ($node instanceof TryCatch) {
$node->catches = array_values($node->catches);
}

if ($node instanceof Switch_) {
$node->cases = array_values($node->cases);
}
}

/**
* @return Stmt[]
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\PHPStan\NodeVisitor\ReIndexNodeAttributeVisitor;
use Rector\PHPStan\NodeVisitor\UnreachableStatementNodeVisitor;
use Rector\PHPStan\NodeVisitor\WrappedNodeRestoringNodeVisitor;
use Rector\Util\Reflection\PrivatesAccessor;
Expand Down Expand Up @@ -158,6 +159,13 @@ public function processNodes(

Assert::allIsInstanceOf($stmts, Stmt::class);

// on refresh, early reindex node attributes
// due to PHPStan doing printing internally on process nodes
if ($formerMutatingScope instanceof MutatingScope) {
$nodeTraverser = new NodeTraverser(new ReIndexNodeAttributeVisitor());
$stmts = $nodeTraverser->traverse($stmts);
}

$this->nodeTraverser->traverse($stmts);

$scope = $formerMutatingScope ?? $this->scopeFactory->createFromFile($filePath);
Expand Down
63 changes: 63 additions & 0 deletions src/PHPStan/NodeVisitor/ReIndexNodeAttributeVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Rector\PHPStan\NodeVisitor;

use PhpParser\Node;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\MatchArm;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\TryCatch;
use PhpParser\NodeVisitorAbstract;

final class ReIndexNodeAttributeVisitor extends NodeVisitorAbstract
{
public function enterNode(Node $node): ?Node
{
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);
}

return null;
}

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

if ($node instanceof If_) {
$node->elseifs = array_values($node->elseifs);
return null;
}

if ($node instanceof TryCatch) {
$node->catches = array_values($node->catches);
return null;
}

if ($node instanceof Switch_) {
$node->cases = array_values($node->cases);
return null;
}

if ($node instanceof MatchArm && is_array($node->conds)) {
$node->conds = array_values($node->conds);
}

return null;
}
}
2 changes: 0 additions & 2 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ final public function enterNode(Node $node): int|Node|null
return null;
}

$this->changedNodeScopeRefresher->reIndexNodeAttributes($node);

// ensure origNode pulled before refactor to avoid changed during refactor, ref https://3v4l.org/YMEGN
$originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE) ?? $node;

Expand Down

0 comments on commit 3c2f735

Please sign in to comment.