<commit_before><?php
/**
 * Connection - A PDO based Database Connection.
 *
 * @author <NAME> - <EMAIL>
 * @version 3.0
 */

namespace Database;

use Core\Config;
use Core\Logger;
use Database\Query\Expression;
use Database\Query\Builder;

use \PDO;
use \DateTime;


class Connection
{
    /**
     * Connection instances
     *
     * @var Connection[]
     */
    private static $instances = array();

    /**
     * The active PDO Connection.
     *
     * @var PDO
     */
    protected $pdo;

    /**
     * The default fetch mode of the Connection.
     *
     * @var int
     */
    protected $fetchMode = PDO::FETCH_OBJ;

    /**
     * The number of active transactions.
     *
     * @var int
     */
    protected $transactions = 0;

    /**
     * The name of the connected Database.
     *
     * @var string
     */
    protected $database;

    /**
     * The table prefix for the Connection.
     *
     * @var string
     */
    protected $tablePrefix = '';

    /**
     * The database connection configuration options.
     *
     * @var array
     */
    protected $config = array();

    /**
     * Create a new Connection instance.
     *
     * @param  array  $config
     * @return void
     */
    public function __construct(array $config)
    {
        $this->pdo = $this->createConnection($config);

        $this->database = $config['dbname'];

        $this->tablePrefix = $config['prefix'];

        $this->config = $config;
    }

    /**
     * Retrieve an instance of the Connection.
     *
     * @param $name string|array Name of the Connection provided in the configuration or options array
     * @return \Database\Connection|null
     * @throws \Exception
     */
    public static function getInstance($config = 'default')
    {
        if (is_array($config) && isset($config['type'])) {
            // Was given as parameter a configuration in the Legacy Style.
            $config = array(
                'driver'    => $config['type'],
                'hostname'  => $config['host'],
                'dbname'    => $config['name'],
                'username'  => $config['user'],
                'password'  => $config['pass'],
                'prefix'    => PREFIX,
                'charset'   => 'utf8',
                'collation' => 'utf8_general_ci',
            );

            $connection = implode('.', array_values($config));
        } else {
            $connection = is_string($config) ? $config : 'default';

            // Retrieve the requested Connection options.
            $config = Config::get('database');

            if (isset($config[$connection]) && ! empty($config[$connection])) {
                $config = $config[$connection];
            } else {
                throw new \Exception("Connection name '$connection' is not defined in your configuration");
            }
        }

        // Prepare a Token for handling the instances.
        $token = md5($connection);

        // If there is already a Connection instantiated, return it.
        if (isset(static::$instances[$token])) {
            return static::$instances[$token];
        }

        // Create the Connection instance and return it.
        return static::$instances[$token] = new static($config);
    }
<commit_msg>Improve Database\Connection@getInstance<commit_after><?php
/**
 * Connection - A PDO based Database Connection.
 *
 * @author <NAME> - <EMAIL>
 * @version 3.0
 */

namespace Database;

use Core\Config;
use Core\Logger;
use Database\Query\Expression;
use Database\Query\Builder;

use \PDO;
use \DateTime;


class Connection
{
    /**
     * Connection instances
     *
     * @var Connection[]
     */
    private static $instances = array();

    /**
     * The active PDO Connection.
     *
     * @var PDO
     */
    protected $pdo;

    /**
     * The default fetch mode of the Connection.
     *
     * @var int
     */
    protected $fetchMode = PDO::FETCH_OBJ;

    /**
     * The number of active transactions.
     *
     * @var int
     */
    protected $transactions = 0;

    /**
     * The name of the connected Database.
     *
     * @var string
     */
    protected $database;

    /**
     * The table prefix for the Connection.
     *
     * @var string
     */
    protected $tablePrefix = '';

    /**
     * The database connection configuration options.
     *
     * @var array
     */
    protected $config = array();

    /**
     * Create a new Connection instance.
     *
     * @param  array  $config
     * @return void
     */
    public function __construct(array $config)
    {
        $this->pdo = $this->createConnection($config);

        $this->database = $config['dbname'];

        $this->tablePrefix = $config['prefix'];

        $this->config = $config;
    }

    /**
     * Retrieve an instance of the Connection.
     *
     * @param $name string|array Name of the Connection provided in the configuration or options array
     * @return \Database\Connection|null
     * @throws \Exception
     */
    public static function getInstance($config = 'default')
    {
        if (is_array($config) && isset($config['type'])) {
            // Was given as parameter a configuration in the Legacy Style.
            $connection = implode('.', array_values($config));

            $config = array(
                'driver'    => $config['type'],
                'hostname'  => $config['host'],
                'dbname'    => $config['name'],
                'username'  => $config['user'],
                'password'  => $<PASSWORD>['<PASSWORD>'],
                'prefix'    => PREFIX,
                'charset'   => 'utf8',
                'collation' => 'utf8_general_ci',
            );
        } else {
            $connection = is_string($config) ? $config : 'default';

            // Retrieve the requested Connection options.
            $config = Config::get('database');

            if (isset($config[$connection]) && ! empty($config[$connection])) {
                $config = $config[$connection];
            } else {
                throw new \Exception("Connection name '$connection' is not defined in your configuration");
            }
        }

        // Prepare a Token for handling the instances.
        $token = md5($connection);

        // If there is already a Connection instantiated, return it.
        if (isset(static::$instances[$token])) {
            return static::$instances[$token];
        }

        // Create the Connection instance and return it.
        return static::$instances[$token] = new static($config);
    }

{'fraction_non_alphanumeric': 0.13327233912778286, 'fraction_numerical': 0.0018298261665141812, 'mean_word_length': 1.8579520697167755, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 35, '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': '4488230', 'n_tokens_mistral': 1806, 'n_tokens_neox': 1743, 'n_words': 487}