From 393221ce632b9ac300682e8d6b1a749b64053793 Mon Sep 17 00:00:00 2001 From: nikic Date: Wed, 31 Aug 2011 21:19:31 +0200 Subject: [PATCH] Some minor improvements (performance and exception wise) for the node traverser --- lib/PHPParser/NodeTraverser.php | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/lib/PHPParser/NodeTraverser.php b/lib/PHPParser/NodeTraverser.php index 5e5476f..4a1deaf 100644 --- a/lib/PHPParser/NodeTraverser.php +++ b/lib/PHPParser/NodeTraverser.php @@ -29,6 +29,10 @@ class PHPParser_NodeTraverser * @param array|PHPParser_NodeAbstract $node Node or array */ public function traverse(&$node) { + if (!is_array($node) && !$node instanceof PHPParser_NodeAbstract) { + throw new InvalidArgumentException('Can only traverse nodes and arrays'); + } + foreach ($this->visitors as $visitor) { $visitor->beforeTraverse($node); } @@ -41,22 +45,18 @@ class PHPParser_NodeTraverser } protected function _traverse(&$node, array $visitors) { - if (!is_array($node) && !$node instanceof Traversable) { - return; - } - $doNodes = array(); foreach ($node as $subNodeKey => &$subNode) { - if ($subNode instanceof PHPParser_NodeAbstract) { + if (is_array($subNode)) { + $this->_traverse($subNode, $visitors); + } elseif ($subNode instanceof PHPParser_NodeAbstract) { foreach ($visitors as $visitor) { $visitor->enterNode($subNode); } - } - $this->_traverse($subNode, $visitors); + $this->_traverse($subNode, $visitors); - if ($subNode instanceof PHPParser_NodeAbstract) { foreach ($visitors as $i => $visitor) { $return = $visitor->leaveNode($subNode); @@ -78,18 +78,12 @@ class PHPParser_NodeTraverser } if (!empty($doNodes)) { - if (is_array($node)) { - while (list($key, $replace) = array_pop($doNodes)) { - array_splice($node, $key, 1, $replace); - } - } else { - 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 (!is_array($node)) { + throw new Exception('Nodes can only be merged if the parent is an array'); + } - unset($node[$key]); - } + while (list($key, $replace) = array_pop($doNodes)) { + array_splice($node, $key, 1, $replace); } } }