Skip to content

Commit

Permalink
Rollback reindex node attributes before refactor (#6603)
Browse files Browse the repository at this point in the history
* Rollback reindex node attributes before refactor

* Rollback reindex node attributes before refactor

* Rollback reindex node attributes before refactor

* Rollback reindex node attributes before refactor

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
samsonasik and actions-user authored Dec 18, 2024
1 parent bdb4025 commit 4cafa91
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 52 deletions.
60 changes: 60 additions & 0 deletions src/Application/NodeAttributeReIndexer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Rector\Application;

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\New_;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\MatchArm;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\TryCatch;

final class NodeAttributeReIndexer
{
public static function reIndexNodeAttributes(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);
}
}

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);
}

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

return null;
}
}
54 changes: 2 additions & 52 deletions src/PHPStan/NodeVisitor/ReIndexNodeAttributeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,13 @@
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\New_;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\MatchArm;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\TryCatch;
use PhpParser\NodeVisitorAbstract;
use Rector\Application\NodeAttributeReIndexer;

final class ReIndexNodeAttributeVisitor extends NodeVisitorAbstract
{
public function enterNode(Node $node): ?Node
{
if ($node instanceof CallLike) {
/** @var FuncCall|MethodCall|New_|NullsafeMethodCall|StaticCall $node */
$node->args = array_values($node->args);
return $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 $node;
}

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

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

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

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

return null;
return NodeAttributeReIndexer::reIndexNodeAttributes($node);
}
}
3 changes: 3 additions & 0 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\Application\ChangedNodeScopeRefresher;
use Rector\Application\NodeAttributeReIndexer;
use Rector\Application\Provider\CurrentFileProvider;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
Expand Down Expand Up @@ -138,6 +139,8 @@ final public function enterNode(Node $node): int|Node|null
// ensure origNode pulled before refactor to avoid changed during refactor, ref https://3v4l.org/YMEGN
$originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE) ?? $node;

NodeAttributeReIndexer::reIndexNodeAttributes($node);

$refactoredNode = $this->refactor($node);

// @see NodeTraverser::* codes, e.g. removal of node of stopping the traversing
Expand Down

0 comments on commit 4cafa91

Please sign in to comment.