Add NodeTraverser::removeVisitor()

This commit is contained in:
Charles Sprayberry 2013-09-20 08:39:42 -04:00 committed by nikic
parent 52aa17fa68
commit 700847e295
4 changed files with 42 additions and 1 deletions

View File

@ -1,7 +1,8 @@
Version 0.9.5-dev
-----------------
Nothing yet.
* Added `NodeTraverser::removeVisitor()` method, which removes a visitor from the node traverser. This also modifies the
corresponding `NodeTraverserInterface`.
Version 0.9.4 (25.08.2013)
--------------------------

View File

@ -23,6 +23,20 @@ class PHPParser_NodeTraverser implements PHPParser_NodeTraverserInterface
$this->visitors[] = $visitor;
}
/**
* Removes an added visitor.
*
* @param PHPParser_NodeVisitor $visitor
*/
public function removeVisitor(PHPParser_NodeVisitor $visitor) {
foreach ($this->visitors as $index => $storedVisitor) {
if ($storedVisitor === $visitor) {
unset($this->visitors[$index]);
break;
}
}
}
/**
* Traverses an array of nodes using the registered visitors.
*

View File

@ -9,6 +9,13 @@ interface PHPParser_NodeTraverserInterface
*/
function addVisitor(PHPParser_NodeVisitor $visitor);
/**
* Removes an added visitor.
*
* @param PHPParser_NodeVisitor $visitor
*/
function removeVisitor(PHPParser_NodeVisitor $visitor);
/**
* Traverses an array of nodes using the registered visitors.
*

View File

@ -122,4 +122,23 @@ class PHPParser_Tests_NodeTraverserTest extends PHPUnit_Framework_TestCase
$this->assertEquals($stmts, $traverser->traverse($stmts));
}
public function testRemovingVisitor() {
$visitor1 = $this->getMock('PHPParser_NodeVisitor');
$visitor2 = $this->getMock('PHPParser_NodeVisitor');
$visitor3 = $this->getMock('PHPParser_NodeVisitor');
$traverser = new PHPParser_NodeTraverser;
$traverser->addVisitor($visitor1);
$traverser->addVisitor($visitor2);
$traverser->addVisitor($visitor3);
$preExpected = array($visitor1, $visitor2, $visitor3);
$this->assertAttributeSame($preExpected, 'visitors', $traverser, 'The appropriate visitors have not been added');
$traverser->removeVisitor($visitor2);
$postExpected = array(0 => $visitor1, 2 => $visitor3);
$this->assertAttributeSame($postExpected, 'visitors', $traverser, 'The appropriate visitors are not present after removal');
}
}