1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2025-01-22 13:51:12 +01:00

Throw on nested array in NodeTraverser

The AST never uses deeply nested arrays, so don't support this in
the node traverser. Throw an exception instead.
This commit is contained in:
Nikita Popov 2017-02-03 22:11:31 +01:00
parent d9911c8da5
commit 2b6804aa50
2 changed files with 8 additions and 12 deletions

View File

@ -162,12 +162,7 @@ class NodeTraverser implements NodeTraverserInterface
$doNodes = array(); $doNodes = array();
foreach ($nodes as $i => &$node) { foreach ($nodes as $i => &$node) {
if (is_array($node)) { if ($node instanceof Node) {
$node = $this->traverseArray($node);
if ($this->stopTraversal) {
break;
}
} elseif ($node instanceof Node) {
$traverseChildren = true; $traverseChildren = true;
foreach ($this->visitors as $visitor) { foreach ($this->visitors as $visitor) {
$return = $visitor->enterNode($node); $return = $visitor->enterNode($node);
@ -204,6 +199,8 @@ class NodeTraverser implements NodeTraverserInterface
$node = $return; $node = $return;
} }
} }
} else if (is_array($node)) {
throw new \LogicException('Invalid node structure: Contains nested arrays');
} }
} }

View File

@ -115,16 +115,15 @@ class NodeTraverserTest extends \PHPUnit_Framework_TestCase
); );
} }
public function testDeepArray() { /**
* @expectedException \LogicException
* @expectedExceptionMessage Invalid node structure: Contains nested arrays
*/
public function testInvalidDeepArray() {
$strNode = new String_('Foo'); $strNode = new String_('Foo');
$stmts = array(array(array($strNode))); $stmts = array(array(array($strNode)));
$visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock();
$visitor->expects($this->at(1))->method('enterNode')->with($strNode);
$traverser = new NodeTraverser; $traverser = new NodeTraverser;
$traverser->addVisitor($visitor);
$this->assertEquals($stmts, $traverser->traverse($stmts)); $this->assertEquals($stmts, $traverser->traverse($stmts));
} }