Ensure removing visitor does not leave holes

This commit is contained in:
Nikita Popov 2023-05-21 12:07:21 +02:00
parent fb2c3ac97c
commit 8bc698248d
2 changed files with 4 additions and 6 deletions

View File

@ -48,11 +48,9 @@ class NodeTraverser implements NodeTraverserInterface {
* @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, []);
}
}

View File

@ -348,7 +348,7 @@ class NodeTraverserTest extends \PHPUnit\Framework\TestCase {
$traverser->removeVisitor($visitor2);
$postExpected = [0 => $visitor1, 2 => $visitor3];
$postExpected = [$visitor1, $visitor3];
$this->assertSame($postExpected, $getVisitors());
}