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:
parent
d9911c8da5
commit
2b6804aa50
@ -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');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user