<commit_before><?php

namespace AppBundle\Service;

use AppBundle\Entity\Test;
use AppBundle\Helper\Pager;
use Doctrine\ORM\Tools\Pagination\Paginator;

class TestService extends AbstractService
{
    /**
     * @var int
     */
    private $perPage;

    private $testRepo;

    public function __construct($doctrine, int $perPage)
    {
        parent::__construct($doctrine);

        $this->perPage = $perPage;
        $this->testRepo = $this->em->getRepository("AppBundle:Test");
    }

    /**
     * Creates pager object
     *
     * @param int $page
     * @param string|null $search
     * @return Pager
     */
    public function createPager(int $page, string $search = null)
    {
        $totalPages = $this->testRepo->getTotalCount($search);

        $pager = new Pager($page, $totalPages);
        $pager->setPerPage($this->perPage);

        return $pager;
    }

    /**
     * Finds test items for current page and search phrase
     *
     * @param int $page
     * @param string|null $search
     * @return Paginator|null
     */
    public function findByPage(int $page, string $search = null)
    {
        $queryBuilder = $this->em
            ->createQueryBuilder()
            ->select(['test', 'tags'])
            ->from('AppBundle:Test', 'test')
            ->leftJoin('test.tags', 'tags')
            ->where('test.status = :status')
            ->setParameter('status', Test::STATUS_PUBLISHED)
            ->orderBy('test.created', 'DESC')
            ->setFirstResult( ($page - 1) * $this->perPage )
            ->setMaxResults($this->perPage);

        if (!empty($search)) {
            $searchIds = $this->testRepo->searchIds($search);
            if (empty($searchIds)) {
                return null;
            }
            $queryBuilder->where('test.id IN (:ids)')
                ->setParameter('ids', $searchIds);
        }

        return new Paginator($queryBuilder, $fetchJoin = true);
    }

    public function getQuestionsCount(int $testId)
    {
        return $this->testRepo->getQuestionsCount($testId);
    }

    public function getTotalPoints(int $testId)
    {
        return $this->testRepo->getTotalPoints($testId);
    }

    public function findById(int $testId)
    {
        return $this->testRepo->find($testId);
    }

}
<commit_msg>fix bug with search: wrong use of where clause<commit_after><?php

namespace AppBundle\Service;

use AppBundle\Entity\Test;
use AppBundle\Helper\Pager;
use Doctrine\ORM\Tools\Pagination\Paginator;

class TestService extends AbstractService
{
    /**
     * @var int
     */
    private $perPage;

    private $testRepo;

    public function __construct($doctrine, int $perPage)
    {
        parent::__construct($doctrine);

        $this->perPage = $perPage;
        $this->testRepo = $this->em->getRepository("AppBundle:Test");
    }

    /**
     * Creates pager object
     *
     * @param int $page
     * @param string|null $search
     * @return Pager
     */
    public function createPager(int $page, string $search = null)
    {
        $totalPages = $this->testRepo->getTotalCount($search);

        $pager = new Pager($page, $totalPages);
        $pager->setPerPage($this->perPage);

        return $pager;
    }

    /**
     * Finds test items for current page and search phrase
     *
     * @param int $page
     * @param string|null $search
     * @return Paginator|null
     */
    public function findByPage(int $page, string $search = null)
    {
        $queryBuilder = $this->em
            ->createQueryBuilder()
            ->select(['test', 'tags'])
            ->from('AppBundle:Test', 'test')
            ->leftJoin('test.tags', 'tags')
            ->where('test.status = :status')
            ->setParameter('status', Test::STATUS_PUBLISHED)
            ->orderBy('test.created', 'DESC')
            ->setFirstResult( ($page - 1) * $this->perPage )
            ->setMaxResults($this->perPage);

        if (!empty($search)) {
            $searchIds = $this->testRepo->searchIds($search);
            if (empty($searchIds)) {
                return null;
            }
            $queryBuilder->andWhere('test.id IN (:ids)')
                ->setParameter('ids', $searchIds);
        }

        return new Paginator($queryBuilder, $fetchJoin = true);
    }

    public function getQuestionsCount(int $testId)
    {
        return $this->testRepo->getQuestionsCount($testId);
    }

    public function getTotalPoints(int $testId)
    {
        return $this->testRepo->getTotalPoints($testId);
    }

    public function findById(int $testId)
    {
        return $this->testRepo->find($testId);
    }

}

{'fraction_non_alphanumeric': 0.141961970613656, 'fraction_numerical': 0.000432152117545376, 'mean_word_length': 2.2302861130495466, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, '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': '3853385', 'n_tokens_mistral': 1391, 'n_tokens_neox': 1364, 'n_words': 280}