<commit_before><?php declare(strict_types=1);
/**
 * This file is part of the PHP_CompatInfoDB package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Bartlett\CompatInfoDb\Infrastructure\Persistence\Doctrine;

use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Proxy\AbstractProxyFactory;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\ORMSetup;

use function getenv;
use function implode;
use function str_replace;
use const DIRECTORY_SEPARATOR;
use const PATH_SEPARATOR;

/**
 * @since Release 3.0.0
 * @author <NAME>
 */
final class EntityManagerFactory
{
    /**
     * @param array<string, string> $connection
     * @param bool $isDevMode
     * @param string $proxyDir
     * @param Cache|null $cache
     * @return EntityManagerInterface
     * @throws ORMException
     */
    public static function create(array $connection, bool $isDevMode, string $proxyDir, ?Cache $cache = null): EntityManagerInterface
    {
        $paths = [implode(DIRECTORY_SEPARATOR, [__DIR__, 'Entity'])];
        $config = ORMSetup::createAnnotationMetadataConfiguration($paths, $isDevMode, $proxyDir, $cache);
        if ($isDevMode) {
            // suggested for DEV mode: see Doctrine ORM documentation
            // at https://www.doctrine-project.org/projects/doctrine-orm/en/2.8/reference/advanced-configuration.html#auto-generating-proxy-classes-optional
            $config->setAutogenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_ALWAYS);
        } else {
            // lazy generation on PROD or TEST modes (i.e: CI)
            $config->setAutogenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS);
        }

        return EntityManager::create(self::connection($connection), $config);
    }

    /**
     * @param array<string, string> $connection
     * @return array<string, string>
     */
    private static function connection(array $connection): array
    {
        $url = $connection['url'] ?? '';
        if (empty($url)) {
            return $connection;
        }

        if (PATH_SEPARATOR === ';') {
            // windows
            $userHome = getenv('USERPROFILE');
        } else {
            // unix
            $userHome = getenv('HOME');
        }

        $connection['url'] = str_replace(['${HOME}', '%HOME%'], $userHome, $connection['url']);
        return $connection;
    }
}
<commit_msg>fix signature error introduced by commit 92d2550a1e428f15b20583b99d6381948b712318 on doctrine/orm upgrade to 2.12<commit_after><?php declare(strict_types=1);
/**
 * This file is part of the PHP_CompatInfoDB package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Bartlett\CompatInfoDb\Infrastructure\Persistence\Doctrine;

use Doctrine\Common\Proxy\AbstractProxyFactory;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\ORMSetup;

use Psr\Cache\CacheItemPoolInterface;

use function getenv;
use function implode;
use function str_replace;
use const DIRECTORY_SEPARATOR;
use const PATH_SEPARATOR;

/**
 * @since Release 3.0.0
 * @author <NAME>
 */
final class EntityManagerFactory
{
    /**
     * @param array<string, string> $connection
     * @throws ORMException
     */
    public static function create(array $connection, bool $isDevMode, string $proxyDir, ?CacheItemPoolInterface $cache = null): EntityManagerInterface
    {
        $paths = [implode(DIRECTORY_SEPARATOR, [__DIR__, 'Entity'])];
        $config = ORMSetup::createAnnotationMetadataConfiguration($paths, $isDevMode, $proxyDir, $cache);
        if ($isDevMode) {
            // suggested for DEV mode: see Doctrine ORM documentation
            // at https://www.doctrine-project.org/projects/doctrine-orm/en/2.8/reference/advanced-configuration.html#auto-generating-proxy-classes-optional
            $config->setAutogenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_ALWAYS);
        } else {
            // lazy generation on PROD or TEST modes (i.e: CI)
            $config->setAutogenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS);
        }

        return EntityManager::create(self::connection($connection), $config);
    }

    /**
     * @param array<string, string> $connection
     * @return array<string, string>
     */
    private static function connection(array $connection): array
    {
        $url = $connection['url'] ?? '';
        if (empty($url)) {
            return $connection;
        }

        if (PATH_SEPARATOR === ';') {
            // windows
            $userHome = getenv('USERPROFILE');
        } else {
            // unix
            $userHome = getenv('HOME');
        }

        $connection['url'] = str_replace(['${HOME}', '%HOME%'], $userHome, $connection['url']);
        return $connection;
    }
}

{'fraction_non_alphanumeric': 0.11821782178217821, 'fraction_numerical': 0.009306930693069307, 'mean_word_length': 3.191701244813278, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '1889682', 'n_tokens_mistral': 1462, 'n_tokens_neox': 1426, 'n_words': 334}