Skip to content

Commit 1c7d2f0

Browse files
committed
alternative fix: reindex in after refactor only, but via traverser
1 parent b70e9b5 commit 1c7d2f0

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/Application/ChangedNodeScopeRefresher.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,18 @@ public function refresh(Node $node, string $filePath, ?MutatingScope $mutatingSc
5858
throw new ShouldNotHappenException($errorMessage);
5959
}
6060

61-
NodeAttributeReIndexer::reIndexNodeAttributes($node);
61+
/**
62+
* The reindex is needed to:
63+
* - be used by PHPStan processNodes() that relies on indexed arrays start from 0
64+
* - use traverser to avoid issues when multiples rules apply, and higher node reindex deep node,
65+
* which the next rule use deep node, for example:
66+
* - first rule: - Class_ → ClassMethod → remove stmt with index 0
67+
* - second rule: - ClassMethod → here fetch the index 0 that no longer exists
68+
*/
69+
SimpleCallableNodeTraverser::traverse(
70+
$node,
71+
fn (Node $subNode): ?Node => NodeAttributeReIndexer::reIndexNodeAttributes($subNode)
72+
);
6273

6374
$stmts = $this->resolveStmts($node);
6475
$this->phpStanNodeScopeResolver->processNodes($stmts, $filePath, $mutatingScope);

0 commit comments

Comments
 (0)