From 2b6804aa507354e89f2cd108858fb3d74027e2e3 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Fri, 3 Feb 2017 22:11:31 +0100 Subject: [PATCH] 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. --- lib/PhpParser/NodeTraverser.php | 9 +++------ test/PhpParser/NodeTraverserTest.php | 11 +++++------ 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/lib/PhpParser/NodeTraverser.php b/lib/PhpParser/NodeTraverser.php index 6110488..a37f3f6 100644 --- a/lib/PhpParser/NodeTraverser.php +++ b/lib/PhpParser/NodeTraverser.php @@ -162,12 +162,7 @@ class NodeTraverser implements NodeTraverserInterface $doNodes = array(); foreach ($nodes as $i => &$node) { - if (is_array($node)) { - $node = $this->traverseArray($node); - if ($this->stopTraversal) { - break; - } - } elseif ($node instanceof Node) { + if ($node instanceof Node) { $traverseChildren = true; foreach ($this->visitors as $visitor) { $return = $visitor->enterNode($node); @@ -204,6 +199,8 @@ class NodeTraverser implements NodeTraverserInterface $node = $return; } } + } else if (is_array($node)) { + throw new \LogicException('Invalid node structure: Contains nested arrays'); } } diff --git a/test/PhpParser/NodeTraverserTest.php b/test/PhpParser/NodeTraverserTest.php index f4bade0..de59134 100644 --- a/test/PhpParser/NodeTraverserTest.php +++ b/test/PhpParser/NodeTraverserTest.php @@ -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'); $stmts = array(array(array($strNode))); - $visitor = $this->getMockBuilder('PhpParser\NodeVisitor')->getMock(); - $visitor->expects($this->at(1))->method('enterNode')->with($strNode); - $traverser = new NodeTraverser; - $traverser->addVisitor($visitor); - $this->assertEquals($stmts, $traverser->traverse($stmts)); }