<commit_before><?php

namespace PHP\Manipulator\TokenContainer;

use PHP\Manipulator\TokenContainer;
use PHP\Manipulator\Token;

class Iterator implements \Iterator, \Countable, \SeekableIterator
{

    /**
     * @var PHP\Manipulator\TokenContainer
     */
    protected $_container = null;

    /**
     * Current Position in the $this->_keys-array
     *
     * @var integer
     */
    protected $_pos = 0;

    /**
     * @var array
     */
    protected $_keys = array();

    /**
     * @param \PHP\Manipulator\TokenContainer $container
     */
    public function __construct(TokenContainer $container)
    {
        $this->_container = $container;
        $this->_keys = array_keys($container->toArray());
    }

    /**
     * @param integer $position
     * @return integer|false
     */
    protected function _getContainerKeyForPosition($position)
    {
        if (array_key_exists($position, $this->_keys)) {
            return $this->_keys[$position];
        } else {
            return false;
        }
    }

    /**
     * Implements SPL::Countable
     *
     * @return integer
     */
    public function count()
    {
        return count($this->_keys);
    }

    /**
     * @return \PHP\Manipulator\Token
     */
    public function current()
    {
        $key = $this->_getContainerKeyForPosition($this->_pos);
        if (false === $key) {
            throw new \OutOfBoundsException('Position not valid');
        }
        return $this->_container[$key];
    }

    /**
     * @return integer
     */
    public function key()
    {
        $key = $this->_getContainerKeyForPosition($this->_pos);
        if (false === $key) {
            throw new \OutOfBoundsException('Position not valid');
        }
        return $key;
    }

    public function next()
    {
        $this->_pos++;
    }

    public function previous()
    {
        $this->_pos--;
    }

    public function rewind()
    {
        $this->_pos = 0;
    }

    /**
     * @return boolean
     */
    public function valid()
    {
        return $this->_isValidPosition($this->_pos + 1);
    }

    /**
     * @param integer $position
     * @return boolean
     */
    protected function _isValidPosition($position)
    {
        if (array_key_exists($this->_pos, $this->_keys)) {
            return true;
        } else {
            return false;
        }
    }

    /**<commit_msg>Fixed internal Bug in \TokenContainer\Iterator<commit_after><?php

namespace PHP\Manipulator\TokenContainer;

use PHP\Manipulator\TokenContainer;
use PHP\Manipulator\Token;

class Iterator implements \Iterator, \Countable, \SeekableIterator
{

    /**
     * @var PHP\Manipulator\TokenContainer
     */
    protected $_container = null;

    /**
     * Current Position in the $this->_keys-array
     *
     * @var integer
     */
    protected $_pos = 0;

    /**
     * @var array
     */
    protected $_keys = array();

    /**
     * @param \PHP\Manipulator\TokenContainer $container
     */
    public function __construct(TokenContainer $container)
    {
        $this->_container = $container;
        $this->_keys = array_keys($container->toArray());
    }

    /**
     * @param integer $position
     * @return integer|false
     */
    protected function _getContainerKeyForPosition($position)
    {
        if (array_key_exists($position, $this->_keys)) {
            return $this->_keys[$position];
        } else {
            return false;
        }
    }

    /**
     * Implements SPL::Countable
     *
     * @return integer
     */
    public function count()
    {
        return count($this->_keys);
    }

    /**
     * @return \PHP\Manipulator\Token
     */
    public function current()
    {
        $key = $this->_getContainerKeyForPosition($this->_pos);
        if (false === $key) {
            throw new \OutOfBoundsException('Position not valid');
        }
        return $this->_container[$key];
    }

    /**
     * @return integer
     */
    public function key()
    {
        $key = $this->_getContainerKeyForPosition($this->_pos);
        if (false === $key) {
            throw new \OutOfBoundsException('Position not valid');
        }
        return $key;
    }

    public function next()
    {
        $this->_pos++;
    }

    public function previous()
    {
        $this->_pos--;
    }

    public function rewind()
    {
        $this->_pos = 0;
    }

    /**
     * @return boolean
     */
    public function valid()
    {
        return $this->_isValidPosition($this->_pos);
    }

    /**
     * @param integer $position
     * @return boolean
     */
    protected function _isValidPosition($position)
    {
        if (array_key_exists($position, $this->_keys)) {
            return true;
        } else {
            return false;
        }
    }

    /**
{'fraction_non_alphanumeric': 0.13351385128098314, 'fraction_numerical': 0.0010414496979795876, 'mean_word_length': 1.834710743801653, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2762236', 'n_tokens_mistral': 1392, 'n_tokens_neox': 1364, 'n_words': 283}