Skip to content

Commit

Permalink
move reindex
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Dec 18, 2024
1 parent ba2ca77 commit db9f844
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
29 changes: 19 additions & 10 deletions src/Application/NodeAttributeReIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,35 @@
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\MatchArm;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Declare_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\TryCatch;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;

final class NodeAttributeReIndexer
{
public static function reIndexNodeAttributes(Node $node): ?Node
{
if (($node instanceof StmtsAwareInterface || $node instanceof ClassLike || $node instanceof Declare_) && $node->stmts !== null) {
$node->stmts = array_values($node->stmts);

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

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

return $node;
}

if ($node instanceof CallLike) {
/** @var FuncCall|MethodCall|New_|NullsafeMethodCall|StaticCall $node */
$node->args = array_values($node->args);
Expand All @@ -41,16 +60,6 @@ public static function reIndexNodeAttributes(Node $node): ?Node
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public function enterNode(Node $node): ?Node
return null;
}

$node->stmts = array_values($node->stmts);

// re-index stmt key under current node
foreach ($node->stmts as $key => $childStmt) {
$childStmt->setAttribute(AttributeKey::STMT_KEY, $key);
Expand Down

0 comments on commit db9f844

Please sign in to comment.