public static function findRootAlias(string $alias, QueryBuilder $queryBuilder): string
    {
        if (\in_array($alias, $queryBuilder->getRootAliases(), true)) {
            return $alias;
        }

        foreach ($queryBuilder->getDQLPart('join') as $rootAlias => $joins) {
            foreach ($joins as $join) {
                if ($alias === $join->getAlias()) {
                    return $rootAlias;
                }
            }
        }

        throw new \LogicException(sprintf('The alias "%s" does not exist in the QueryBuilder.', $alias));
    }