Chris@14: Chris@14: * Chris@14: * For the full copyright and license information, please view the LICENSE Chris@14: * file that was distributed with this source code. Chris@14: */ Chris@14: Chris@14: namespace SebastianBergmann\CodeCoverage\Node; Chris@14: Chris@14: /** Chris@14: * Recursive iterator for node object graphs. Chris@14: */ Chris@14: class Iterator implements \RecursiveIterator Chris@14: { Chris@14: /** Chris@14: * @var int Chris@14: */ Chris@14: private $position; Chris@14: Chris@14: /** Chris@14: * @var AbstractNode[] Chris@14: */ Chris@14: private $nodes; Chris@14: Chris@14: /** Chris@14: * @param Directory $node Chris@14: */ Chris@14: public function __construct(Directory $node) Chris@14: { Chris@14: $this->nodes = $node->getChildNodes(); Chris@14: } Chris@14: Chris@14: /** Chris@14: * Rewinds the Iterator to the first element. Chris@14: */ Chris@14: public function rewind() Chris@14: { Chris@14: $this->position = 0; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Checks if there is a current element after calls to rewind() or next(). Chris@14: * Chris@14: * @return bool Chris@14: */ Chris@14: public function valid() Chris@14: { Chris@14: return $this->position < \count($this->nodes); Chris@14: } Chris@14: Chris@14: /** Chris@14: * Returns the key of the current element. Chris@14: * Chris@14: * @return int Chris@14: */ Chris@14: public function key() Chris@14: { Chris@14: return $this->position; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Returns the current element. Chris@14: * Chris@14: * @return \PHPUnit_Framework_Test Chris@14: */ Chris@14: public function current() Chris@14: { Chris@14: return $this->valid() ? $this->nodes[$this->position] : null; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Moves forward to next element. Chris@14: */ Chris@14: public function next() Chris@14: { Chris@14: $this->position++; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Returns the sub iterator for the current element. Chris@14: * Chris@14: * @return Iterator Chris@14: */ Chris@14: public function getChildren() Chris@14: { Chris@14: return new self( Chris@14: $this->nodes[$this->position] Chris@14: ); Chris@14: } Chris@14: Chris@14: /** Chris@14: * Checks whether the current element has children. Chris@14: * Chris@14: * @return bool Chris@14: */ Chris@14: public function hasChildren() Chris@14: { Chris@14: return $this->nodes[$this->position] instanceof Directory; Chris@14: } Chris@14: }