1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-30 04:19:30 +01:00

Fix NodeTraverser by putting to-be-deleted and to-be-merged nodes into the same queue. Additionally do not further iterate queued nodes.

This commit is contained in:
nikic 2011-08-19 17:19:27 +02:00
parent ca51472c2f
commit 299893066d
2 changed files with 16 additions and 21 deletions

View File

@ -45,8 +45,7 @@ class PHPParser_NodeTraverser
return;
}
$delNodes = array();
$mrgNodes = array();
$doNodes = array();
foreach ($node as $subNodeKey => &$subNode) {
if ($subNode instanceof PHPParser_NodeAbstract) {
@ -62,33 +61,29 @@ class PHPParser_NodeTraverser
$return = $visitor->leaveNode($subNode);
if (false === $return) {
$delNodes[] = $subNodeKey;
$doNodes[] = array($subNodeKey, array());
break;
} elseif (is_array($return)) {
$mrgNodes[] = array($subNodeKey, $return);
$doNodes[] = array($subNodeKey, $return);
break;
}
}
}
}
if (!empty($delNodes)) {
if (!empty($doNodes)) {
if (is_array($node)) {
while ($delKey = array_pop($delNodes)) {
array_splice($node, $delKey, 1, array());
while (list($key, $replace) = array_pop($doNodes)) {
array_splice($node, $key, 1, $replace);
}
} else {
while ($delKey = array_pop($delNodes)) {
unset($node[$delKey]);
}
}
}
while (list($key, $replace) = array_pop($doNodes)) {
if (!empty($replace)) {
throw new Exception('Nodes can only be merged if the parent is an array');
}
if (!empty($mrgNodes)) {
if (is_array($node)) {
while (list($mrgKey, $mrgItems) = array_pop($mrgNodes)) {
array_splice($node, $mrgKey, 1, $mrgItems);
unset($node[$key]);
}
} else {
throw new Exception('Nodes can only be merged if the parent is an array');
}
}
}

View File

@ -2,7 +2,7 @@
class PHPParser_Tests_NodeTraverserTest extends PHPUnit_Framework_TestCase
{
function getTestNode() {
public function getTestNode() {
return array(
new PHPParser_Node_Stmt_Namespace(array(
'name' => new PHPParser_Node_Name(array('Foo', 'Bar')),
@ -18,7 +18,7 @@ class PHPParser_Tests_NodeTraverserTest extends PHPUnit_Framework_TestCase
);
}
function testTraverse() {
public function testTraverse() {
$node = $this->getTestNode();
$visitor = new PHPParser_Tests_NodeVisitor;
@ -56,7 +56,7 @@ class PHPParser_Tests_NodeTraverserTest extends PHPUnit_Framework_TestCase
$this->assertEquals($node, $visitor->afterTraverseNode);
}
function testModifyingTraverse() {
public function testModifyingTraverse() {
$node = $this->getTestNode();
$visitor = new PHPParser_Tests_ModifyingNodeVisitor;