<commit_before><?php

namespace Knp\DoctrineBehaviors\ORM\Tree;

use Doctrine\ORM\QueryBuilder;

trait Tree
{
    /**
     * Returns all root nodes
     *
     * @api
     *
     * @param string $rootAlias
     *
     * @return array
     */
    public function getRootNodes($rootAlias = 't')
    {
        $class         = $this->getClassName();
        $pathSeparator = $class::getMaterializedPathSeparator();

        $qb = $this->createQueryBuilder($rootAlias)
            ->andWhere($rootAlias.'.path NOT LIKE :separator')
            ->setParameter('separator', $pathSeparator.'%')
        ;

        return $qb->getQuery()->execute();
    }

    /**
     * Returns a node hydrated with its children and parents
     *
     * @api
     *
     * @param string $path
     * @param string $rootAlias
     *
     * @return NodeInterface a node
     */
    public function getTree($path = '', $rootAlias = 't')
    {
        $results = $this->getFlatTree($path, $rootAlias);

        return $this->buildTree($results);
    }

    public function getTreeExceptNodeAndItsChildrenQB($entity, $rootAlias = 't')
    {
        return $this->getFlatTreeQB('', $rootAlias)
            ->andWhere($rootAlias.'.materializedPath NOT LIKE :except_path')
            ->setParameter('except_path', $entity->getMaterializedPath().'%')
        ;
    }

    /**
     * Extracts the root node and constructs a tree using flat resultset
     *
     * @param Iterable|array $results a flat resultset
     *
     * @return NodeInterface
     */
    protected function buildTree($results)
    {
        if (!count($results)) {
            return;<commit_msg>fix repository queries for paths without emdeded ids<commit_after><?php

namespace Knp\DoctrineBehaviors\ORM\Tree;

use Doctrine\ORM\QueryBuilder;

trait Tree
{
    /**
     * Returns all root nodes
     *
     * @api
     *
     * @param string $rootAlias
     *
     * @return array
     */
    public function getRootNodes($rootAlias = 't')
    {
        $qb = $this->createQueryBuilder($rootAlias)
            ->andWhere($rootAlias.'.materializedPath = :empty')
            ->setParameter('empty', '')
        ;

        return $qb->getQuery()->execute();
    }

    /**
     * Returns a node hydrated with its children and parents
     *
     * @api
     *
     * @param string $path
     * @param string $rootAlias
     *
     * @return NodeInterface a node
     */
    public function getTree($path = '', $rootAlias = 't')
    {
        $results = $this->getFlatTree($path, $rootAlias);

        return $this->buildTree($results);
    }

    public function getTreeExceptNodeAndItsChildrenQB($entity, $rootAlias = 't')
    {
        return $this->getFlatTreeQB('', $rootAlias)
            ->andWhere($rootAlias.'.materializedPath NOT LIKE :except_path')
            ->andWhere($rootAlias.'.id != :id')
            ->setParameter('except_path', $entity->getRealMaterializedPath().'%')
            ->setParameter('id', $entity->getId())
        ;
    }

    /**
     * Extracts the root node and constructs a tree using flat resultset
     *
     * @param Iterable|array $results a flat resultset
     *
     * @return NodeInterface
     */
    protected function buildTree($results)
    {
        if (!count($results)) {
            return;
{'fraction_non_alphanumeric': 0.13341455985379228, 'fraction_numerical': 0.0, 'mean_word_length': 2.219607843137255, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '5097328', 'n_tokens_mistral': 974, 'n_tokens_neox': 952, 'n_words': 211}