<commit_before><?php

declare(strict_types=1);

/*
 * This file is part of the Sonata Project package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sonata\AdminBundle\Datagrid;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Sonata\AdminBundle\Util\TraversableToCollection;

/**
 * @final since sonata-project/admin-bundle 3.52
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 *
 * @phpstan-template T of ProxyQueryInterface
 * @phpstan-extends Pager<T>
 */
class SimplePager extends Pager
{
    /**
     * @var Collection<array-key, object>|null
     */
    protected $results;

    /**
     * @var bool
     */
    protected $haveToPaginate;

    /**
     * How many pages to look forward to create links to next pages.
     *
     * @var int
     */
    protected $threshold;

    /**
     * @var int
     */
    protected $thresholdCount;

    /**
     * The threshold parameter can be used to determine how far ahead the pager
     * should fetch results.
     *
     * If set to 1 which is the minimal value the pager will generate a link to the next page
     * If set to 2 the pager will generate links to the next two pages
     * If set to 3 the pager will generate links to the next three pages
     * etc.
     *
     * @param int $maxPerPage Number of records to display per page
     * @param int $threshold
     */
    public function __construct($maxPerPage = 10, $threshold = 1)
    {
        parent::__construct($maxPerPage);
        $this->setThreshold($threshold);
    }

    /**
     * NEXT_MAJOR: remove this method.
     */
    public function getNbResults()
    {
        @trigger_error(sprintf(
            'The method "%s()" is deprecated since sonata-project/admin-bundle 3.86 and will be removed in 4.0. Use countResults() instead.',
            __METHOD__
        ), \E_USER_DEPRECATED);

        return $this->countResults();
    }

    public function countResults(): int
    {
        $n = ($this->getLastPage() - 1) * $this->getMaxPerPage();
        if ($this->getLastPage() === $this->getPage()) {
            return $n + $this->thresholdCount;
        }

        return $n;
    }

    public function getCurrentPageResults(): iterable
    {
        if (null !== $this->results) {
            return $this->results;
        }

        $this->results = TraversableToCollection::transform($this->getQuery()->execute());
        $this->thresholdCount = $this->results->count();

        if ($this->thresholdCount > $this->getMaxPerPage()) {
            $this->haveToPaginate = true;

            $this->results = new ArrayCollection($this->results->slice(0, $this->getMaxPerPage()));
        } else {
            $this->haveToPaginate = false;
        }

        return $this->results;
    }

    /**
     * NEXT_MAJOR: Remove this method.
     *
     * @deprecated since sonata-project/admin-bundle 3.87. To be removed in 4.0. Use getCurrentPageResults() instead.
     */
    public function getResults($hydrationMode = null)
    {
        @trigger_error(sprintf(
            'The method "%s()" is deprecated since sonata-project/admin-bundle 3.87 and will be removed in 4.0. Use getCurrentPageResults() instead.',
            __METHOD__
        ), \E_USER_DEPRECATED);

        if (null !== $this->results) {
            return $this->results;
        }

        // @phpstan-ignore-next-line
        $this->results = TraversableToCollection::transform($this->getQuery()->execute([], $hydrationMode));
        $this->thresholdCount = $this->results->count();

        if ($this->thresholdCount > $this->getMaxPerPage()) {
            $this->haveToPaginate = true;

            $this->results = new ArrayCollection($this->results->slice(0, $this->getMaxPerPage()));
        } else {
            $this->haveToPaginate = false;
        }

        return $this->results;
    }

    public function haveToPaginate()
    {
        return $this->haveToPaginate || $this->getPage() > 1;
    }

    /**
     * {@inheritdoc}
     *
     * @throws \RuntimeException the query is uninitialized
     */
    public function init()
    {
        if (!$this->getQuery()) {
            throw new \RuntimeException('Uninitialized query');
        }

        // NEXT_MAJOR: Remove this line and uncomment the following one instead.
        $this->resetIterator('sonata_deprecation_mute');
//        $this->haveToPaginate = false;

        if (0 === $this->getPage() || 0 === $this->getMaxPerPage()) {
            $this->setLastPage(0);
            $this->getQuery()->setFirstResult(0);<commit_msg>Remove SimplePager::haveToPaginate implementation<commit_after><?php

declare(strict_types=1);

/*
 * This file is part of the Sonata Project package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sonata\AdminBundle\Datagrid;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Sonata\AdminBundle\Util\TraversableToCollection;

/**
 * @final since sonata-project/admin-bundle 3.52
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 *
 * @phpstan-template T of ProxyQueryInterface
 * @phpstan-extends Pager<T>
 */
class SimplePager extends Pager
{
    /**
     * @var Collection<array-key, object>|null
     */
    protected $results;

    /**
     * NEXT_MAJOR: Remove this property.
     *
     * @deprecated since sonata-project/admin-bundle 3.x
     *
     * @var bool
     */
    protected $haveToPaginate;

    /**
     * How many pages to look forward to create links to next pages.
     *
     * @var int
     */
    protected $threshold;

    /**
     * @var int
     */
    protected $thresholdCount;

    /**
     * The threshold parameter can be used to determine how far ahead the pager
     * should fetch results.
     *
     * If set to 1 which is the minimal value the pager will generate a link to the next page
     * If set to 2 the pager will generate links to the next two pages
     * If set to 3 the pager will generate links to the next three pages
     * etc.
     *
     * @param int $maxPerPage Number of records to display per page
     * @param int $threshold
     */
    public function __construct($maxPerPage = 10, $threshold = 1)
    {
        parent::__construct($maxPerPage);
        $this->setThreshold($threshold);
    }

    /**
     * NEXT_MAJOR: remove this method.
     */
    public function getNbResults()
    {
        @trigger_error(sprintf(
            'The method "%s()" is deprecated since sonata-project/admin-bundle 3.86 and will be removed in 4.0. Use countResults() instead.',
            __METHOD__
        ), \E_USER_DEPRECATED);

        return $this->countResults();
    }

    public function countResults(): int
    {
        $n = ($this->getLastPage() - 1) * $this->getMaxPerPage();
        if ($this->getLastPage() === $this->getPage()) {
            return $n + $this->thresholdCount;
        }

        return $n;
    }

    public function getCurrentPageResults(): iterable
    {
        if (null !== $this->results) {
            return $this->results;
        }

        $this->results = TraversableToCollection::transform($this->getQuery()->execute());
        $this->thresholdCount = $this->results->count();

        if ($this->thresholdCount > $this->getMaxPerPage()) {
            $this->haveToPaginate = true;

            $this->results = new ArrayCollection($this->results->slice(0, $this->getMaxPerPage()));
        } else {
            $this->haveToPaginate = false;
        }

        return $this->results;
    }

    /**
     * NEXT_MAJOR: Remove this method.
     *
     * @deprecated since sonata-project/admin-bundle 3.87. To be removed in 4.0. Use getCurrentPageResults() instead.
     */
    public function getResults($hydrationMode = null)
    {
        @trigger_error(sprintf(
            'The method "%s()" is deprecated since sonata-project/admin-bundle 3.87 and will be removed in 4.0. Use getCurrentPageResults() instead.',
            __METHOD__
        ), \E_USER_DEPRECATED);

        if (null !== $this->results) {
            return $this->results;
        }

        // @phpstan-ignore-next-line
        $this->results = TraversableToCollection::transform($this->getQuery()->execute([], $hydrationMode));
        $this->thresholdCount = $this->results->count();

        if ($this->thresholdCount > $this->getMaxPerPage()) {
            $this->haveToPaginate = true;

            $this->results = new ArrayCollection($this->results->slice(0, $this->getMaxPerPage()));
        } else {
            $this->haveToPaginate = false;
        }

        return $this->results;
    }

    /**
     * {@inheritdoc}
     *
     * @throws \RuntimeException the query is uninitialized
     */
    public function init()
    {
        if (!$this->getQuery()) {
            throw new \RuntimeException('Uninitialized query');
        }

        // NEXT_MAJOR: Remove this line and uncomment the following one instead.
        $this->resetIterator('sonata_deprecation_mute');
//        $this->haveToPaginate = false;

        if (0 === $this->getPage() || 0 === $this->getMaxPerPage()) {
            $this->setLastPage(0);
            $this->getQuery()->setFirstResult(0);
{'fraction_non_alphanumeric': 0.13051314673452077, 'fraction_numerical': 0.006997455470737914, 'mean_word_length': 2.3521677327647477, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 130, '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': '1181265', 'n_tokens_mistral': 2789, 'n_tokens_neox': 2715, 'n_words': 712}