Skip to content

Commit

Permalink
Ensure removing visitor does not leave holes
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed May 21, 2023
1 parent fb2c3ac commit 8bc6982
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
8 changes: 3 additions & 5 deletions lib/PhpParser/NodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ public function addVisitor(NodeVisitor $visitor): void {
* @param NodeVisitor $visitor
*/
public function removeVisitor(NodeVisitor $visitor): void {
foreach ($this->visitors as $index => $storedVisitor) {
if ($storedVisitor === $visitor) {
unset($this->visitors[$index]);
break;
}
$index = array_search($visitor, $this->visitors);
if ($index !== false) {
array_splice($this->visitors, $index, 1, []);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/PhpParser/NodeTraverserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public function testRemovingVisitor() {

$traverser->removeVisitor($visitor2);

$postExpected = [0 => $visitor1, 2 => $visitor3];
$postExpected = [$visitor1, $visitor3];
$this->assertSame($postExpected, $getVisitors());
}

Expand Down

0 comments on commit 8bc6982

Please sign in to comment.