<commit_before><?php

  namespace Xparse\RecursivePagination;

  use Doctrine\Instantiator\Exception\InvalidArgumentException;
  use Fiv\Parser\Exception;

  class RecursivePagination
  {

    protected $queue = array();

    protected $grabber = null;

    protected $defaultXpath = array();

    public function __construct(\Fiv\Parser\Grabber $grabber, $xpath = array())
    {
      $this->grabber = $grabber;
      if (!is_string($xpath) && !is_array($xpath)) {
        throw new \InvalidArgumentException('xPath should be an array or a string');
      }
      $xpath = (array)$xpath;
      foreach ($xpath as $path) {
        if (!is_string($path)) {
          throw new \InvalidArgumentException('Incorrect xPath, should be an array or a string');
        }
        $this->defaultXpath[] = $path;
      }
    }

    public function addToQueue($link, $state = false)
    {
      $this->queue[$link] = $state;
      return $this;
    }

    public function getNextPage()
    {
      $page = $this->grabber->getLastPage();
      if (!empty($page)) {
        foreach ($this->defaultXpath as $xPath) {
          $queueLinks = $page->attribute($xPath)->getItems();

          if (!empty($queueLinks)) {
            $queueLinks = array_combine($queueLinks, array_fill(0, count($queueLinks), false));
            $this->queue = array_merge($queueLinks, $this->queue);
          }
        }
      }

      $link = array_search(false, $this->queue, true);

      if (empty($link)) {
        return null;
      }

      $this->queue[$link] = true;
      return $this->grabber->getHtml($link);
    }
  }<commit_msg>issue #3 php doc comments added + formatting issue #5 removed unused statements<commit_after><?php

  namespace Xparse\RecursivePagination;


  use Fiv\Parser\Grabber;

  /**
   *
   * @package Xparse\RecursivePagination
   */
  class RecursivePagination {

    /**
     * @var array
     */
    protected $queue = [];

    /**
     * @var Grabber|null
     */
    protected $grabber = null;

    /**
     * @var array
     */
    protected $defaultXpath = [];


    /**
     * @param Grabber $grabber
     * @param array $xpath
     * @throws \Exception
     */
    public function __construct(Grabber $grabber, $xpath = []) {
      $this->grabber = $grabber;
      if (!is_string($xpath) && !is_array($xpath)) {
        throw new \Exception('xPath should be an array or a string');
      }
      $xpath = (array) $xpath;
      foreach ($xpath as $path) {
        if (!is_string($path)) {
          throw new \Exception('Incorrect xPath, should be an array or a string');
        }
        $this->defaultXpath[] = $path;
      }
    }


    /**
     * @param $link
     * @param bool $state
     * @return $this
     */
    public function addToQueue($link, $state = false) {
      $this->queue[$link] = $state;
      return $this;
    }


    /**
     * @return \Fiv\Parser\Dom\ElementFinder|null
     */
    public function getNextPage() {
      $page = $this->grabber->getLastPage();
      if (!empty($page)) {
        foreach ($this->defaultXpath as $xPath) {
          $queueLinks = $page->attribute($xPath)->getItems();

          if (!empty($queueLinks)) {
            $queueLinks = array_combine($queueLinks, array_fill(0, count($queueLinks), false));
            $this->queue = array_merge($queueLinks, $this->queue);
          }
        }
      }

      $link = array_search(false, $this->queue, true);

      if (empty($link)) {
        return null;
      }

      $this->queue[$link] = true;
      return $this->grabber->getHtml($link);
    }
  }
{'fraction_non_alphanumeric': 0.1499439461883408, 'fraction_numerical': 0.0011210762331838565, 'mean_word_length': 2.2743119266055047, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '213559', 'n_tokens_mistral': 1143, 'n_tokens_neox': 1113, 'n_words': 240}