<commit_before><?php

declare(strict_types=1);

namespace Doctrine\Migrations;

use Doctrine\DBAL\Connection;
use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\Exception\DuplicateMigrationVersion;
use Doctrine\Migrations\Exception\MigrationClassNotFound;
use Doctrine\Migrations\Exception\MigrationException;
use Doctrine\Migrations\Exception\UnknownMigrationVersion;
use Doctrine\Migrations\Finder\MigrationFinder;
use const SORT_STRING;
use function array_diff;
use function array_keys;
use function array_map;
use function array_search;
use function array_unshift;
use function class_exists;
use function count;
use function end;
use function get_class;
use function implode;
use function is_array;
use function ksort;
use function sprintf;
use function substr;

/**
 * @internal
 */
class MigrationRepository
{
    /** @var Configuration */
    private $configuration;

    /** @var Connection */
    private $connection;

    /** @var MigrationFinder */
    private $migrationFinder;

    /** @var VersionFactory */
    private $versionFactory;

    /** @var Version[] */
    private $versions = [];

    public function __construct(
        Configuration $configuration,
        Connection $connection,
        MigrationFinder $migrationFinder,
        VersionFactory $versionFactory
    ) {
        $this->configuration   = $configuration;
        $this->connection      = $connection;
        $this->migrationFinder = $migrationFinder;
        $this->versionFactory  = $versionFactory;
    }

    /**
     * @return string[]
     */
    public function findMigrations(string $path) : array
    {
        return $this->migrationFinder->findMigrations($path);
    }

    /** @return Version[] */
    public function registerMigrationsFromDirectory(string $path) : array
    {
        return $this->registerMigrations($this->findMigrations($path));
    }

    /** @throws MigrationException */
    public function registerMigration(string $version, string $migrationClassName) : Version
    {
        $this->ensureMigrationClassExists($migrationClassName);

        if (isset($this->versions[$version])) {
            throw DuplicateMigrationVersion::new(
                $version,
                get_class($this->versions[$version])
            );
        }

        $version = $this->versionFactory->createVersion($version, $migrationClassName);

        $this->versions[$version->getVersion()] = $version;

        ksort($this->versions, SORT_STRING);

        return $version;
    }

    /**
     * @param string[] $migrations
     *
     * @return Version[]
     */
    public function registerMigrations(array $migrations) : array
    {
        $versions = [];

        foreach ($migrations as $version => $class) {
            $versions[] = $this->registerMigration((string) $version, $class);
        }

        return $versions;
    }

    public function getCurrentVersion() : string
    {
        $this->configuration->createMigrationTable();

        if (! $this->configuration->isMigrationTableCreated() && $this->configuration->isDryRun()) {
            return '0';
        }

        $this->configuration->connect();

        $this->loadMigrationsFromDirectory();

        $where = null;

        if (count($this->versions) !== 0) {
            $migratedVersions = [];

            foreach ($this->versions as $migration) {
                $migratedVersions[] = sprintf("'%s'", $migration->getVersion());
            }

            $where = sprintf(
                ' WHERE %s IN (%s)',
                $this->configuration->getQuotedMigrationsColumnName(),
                implode(', ', $migratedVersions)
            );
        }

        $sql = sprintf(
            'SELECT %s FROM %s%s ORDER BY %s DESC',
            $this->configuration->getQuotedMigrationsColumnName(),
            $this->configuration->getMigrationsTableName(),
            $where,
            $this->configuration->getQuotedMigrationsColumnName()
        );

        $sql    = $this->connection->getDatabasePlatform()->modifyLimitQuery($sql, 1);
        $result = $this->connection->fetchColumn($sql);

        return $result !== false ? (string) $result : '0';
    }

    public function getVersion(string $version) : Version
    {
        $this->loadMigrationsFromDirectory();

        if (! isset($this->versions[$version])) {
            throw UnknownMigrationVersion::new($version);
        }

        return $this->versions[$version];
    }

    public function hasVersion(string $version) : bool
    {
        $this->loadMigrationsFromDirectory();

        return isset($this->versions[$version]);
    }

    public function hasVersionMigrated(Version $version) : bool
    {
        return $this->getVersionData($version) !== null;
    }

    /**
     * @return mixed[]|null
     */
    public function getVersionData(Version $version) : ?array
    {
        $this->configuration->connect();
        $this->configuration->createMigrationTable();

        $sql = sprintf(
            'SELECT %s, %s FROM %s WHERE %s = ?',
            $this->configuration->getQuotedMigrationsColumnName(),
            $this->configuration->getQuotedMigrationsExecutedAtColumnName(),
            $this->configuration->getMigrationsTableName(),
            $this->configuration->getQuotedMigrationsColumnName()
        );

        $data = $this->connection->fetchAssoc($sql, [$version->getVersion()]);

        return is_array($data) ? $data : null;
    }

    /**
     * @return Version[]
     */
    public function getMigrations() : array
    {
        $this->loadMigrationsFromDirectory();

        return $this->versions;
    }

    /** @return string[] */
    public function getAvailableVersions() : array
    {
        $availableVersions = [];

        $this->loadMigrationsFromDirectory();

        foreach ($this->versions as $migration) {
            $availableVersions[] = $migration->getVersion();
        }

        return $availableVersions;
    }

    /** @return string[] */
    public function getNewVersions() : array
    {
        $availableMigrations = $this->getAvailableVersions();
        $executedMigrations  = $this->getMigratedVersions();

        return array_diff($availableMigrations, $executedMigrations);
    }

    /** @return string[] */
    public function getMigratedVersions() : array
    {
        $this->configuration->createMigrationTable();

        if (! $this->configuration->isMigrationTableCreated() && $this->configuration->isDryRun()) {
            return [];
        }

        $this->configuration->connect();

        $sql = sprintf(
            'SELECT %s FROM %s',
            $this->configuration->getQuotedMigrationsColumnName(),
            $this->configuration->getMigrationsTableName()
        );

        $result = $this->connection->fetchAll($sql);

        return array_map('current', $result);
    }

    /**
     * @return string[]
     */
    public function getExecutedUnavailableMigrations() : array
    {
        $executedMigrations  = $this->getMigratedVersions();
        $availableMigrations = $this->getAvailableVersions();

        return array_diff($executedMigrations, $availableMigrations);
    }

    public function getNumberOfAvailableMigrations() : int
    {
        $this->loadMigrationsFromDirectory();

        return count($this->versions);
    }

    public function getLatestVersion() : string
    {
        $this->loadMigrationsFromDirectory();

        $versions = array_keys($this->versions);
        $latest   = end($versions);

        return $latest !== false ? (string) $latest : '0';
    }

    public function getNumberOfExecutedMigrations() : int
    {
        $this->configuration->connect();
        $this->configuration->createMigrationTable();

        $sql = sprintf(
            'SELECT COUNT(%s) FROM %s',
            $this->configuration->getQuotedMigrationsColumnName(),
            $this->configuration->getMigrationsTableName()
        );

        $result = $this->connection->fetchColumn($sql);

        return $result !== false ? (int) $result : 0;
    }

    public function getRelativeVersion(string $version, int $delta) : ?string
    {
        $this->loadMigrationsFromDirectory();

        $versions = array_map('strval', array_keys($this->versions));

        array_unshift($versions, '0');

        $offset = array_search($version, $versions, true);

        if ($offset === false || ! isset($versions[$offset + $delta])) {
            // Unknown version or delta out of bounds.
            return null;
        }

        return $versions[$offset + $delta];
    }

    public function getDeltaVersion(string $delta) : ?string
    {
        $symbol = substr($delta, 0, 1);
        $number = (int) substr($delta, 1);

        if ($number <= 0) {
            return null;
        }

        if ($symbol === '+' || $symbol === '-') {
            return $this->getRelativeVersion($this->getCurrentVersion(), (int) $delta);
        }

        return null;
    }

    public function getPrevVersion() : ?string
    {
        return $this->getRelativeVersion($this->getCurrentVersion(), -1);
    }

    public function getNextVersion() : ?string
    {
        return $this->getRelativeVersion($this->getCurrentVersion(), 1);
    }

    private function loadMigrationsFromDirectory() : void
    {
        $migrationsDirectory = $this->configuration->getMigrationsDirectory();

        if (count($this->versions) !== 0 || $migrationsDirectory === null) {
            return;
        }

        $this->registerMigrationsFromDirectory($migrationsDirectory);
    }

    /** @throws MigrationException */
    private function ensureMigrationClassExists(string $class) : void
    {
        if (! class_exists($class)) {
            throw MigrationClassNotFound::new(
                $class,
                $this->configuration->getMigrationsNamespace()
            );
        }
    }
}
<commit_msg>Pass the Namespace to Finder in MigrationRepository<commit_after><?php

declare(strict_types=1);

namespace Doctrine\Migrations;

use Doctrine\DBAL\Connection;
use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\Exception\DuplicateMigrationVersion;
use Doctrine\Migrations\Exception\MigrationClassNotFound;
use Doctrine\Migrations\Exception\MigrationException;
use Doctrine\Migrations\Exception\UnknownMigrationVersion;
use Doctrine\Migrations\Finder\MigrationFinder;
use const SORT_STRING;
use function array_diff;
use function array_keys;
use function array_map;
use function array_search;
use function array_unshift;
use function class_exists;
use function count;
use function end;
use function get_class;
use function implode;
use function is_array;
use function ksort;
use function sprintf;
use function substr;

/**
 * @internal
 */
class MigrationRepository
{
    /** @var Configuration */
    private $configuration;

    /** @var Connection */
    private $connection;

    /** @var MigrationFinder */
    private $migrationFinder;

    /** @var VersionFactory */
    private $versionFactory;

    /** @var Version[] */
    private $versions = [];

    public function __construct(
        Configuration $configuration,
        Connection $connection,
        MigrationFinder $migrationFinder,
        VersionFactory $versionFactory
    ) {
        $this->configuration   = $configuration;
        $this->connection      = $connection;
        $this->migrationFinder = $migrationFinder;
        $this->versionFactory  = $versionFactory;
    }

    /**
     * @return string[]
     */
    public function findMigrations(string $path) : array
    {
        return $this->migrationFinder->findMigrations(
            $path,
            $this->configuration->getMigrationsNamespace()
        );
    }

    /** @return Version[] */
    public function registerMigrationsFromDirectory(string $path) : array
    {
        return $this->registerMigrations($this->findMigrations($path));
    }

    /** @throws MigrationException */
    public function registerMigration(string $version, string $migrationClassName) : Version
    {
        $this->ensureMigrationClassExists($migrationClassName);

        if (isset($this->versions[$version])) {
            throw DuplicateMigrationVersion::new(
                $version,
                get_class($this->versions[$version])
            );
        }

        $version = $this->versionFactory->createVersion($version, $migrationClassName);

        $this->versions[$version->getVersion()] = $version;

        ksort($this->versions, SORT_STRING);

        return $version;
    }

    /**
     * @param string[] $migrations
     *
     * @return Version[]
     */
    public function registerMigrations(array $migrations) : array
    {
        $versions = [];

        foreach ($migrations as $version => $class) {
            $versions[] = $this->registerMigration((string) $version, $class);
        }

        return $versions;
    }

    public function getCurrentVersion() : string
    {
        $this->configuration->createMigrationTable();

        if (! $this->configuration->isMigrationTableCreated() && $this->configuration->isDryRun()) {
            return '0';
        }

        $this->configuration->connect();

        $this->loadMigrationsFromDirectory();

        $where = null;

        if (count($this->versions) !== 0) {
            $migratedVersions = [];

            foreach ($this->versions as $migration) {
                $migratedVersions[] = sprintf("'%s'", $migration->getVersion());
            }

            $where = sprintf(
                ' WHERE %s IN (%s)',
                $this->configuration->getQuotedMigrationsColumnName(),
                implode(', ', $migratedVersions)
            );
        }

        $sql = sprintf(
            'SELECT %s FROM %s%s ORDER BY %s DESC',
            $this->configuration->getQuotedMigrationsColumnName(),
            $this->configuration->getMigrationsTableName(),
            $where,
            $this->configuration->getQuotedMigrationsColumnName()
        );

        $sql    = $this->connection->getDatabasePlatform()->modifyLimitQuery($sql, 1);
        $result = $this->connection->fetchColumn($sql);

        return $result !== false ? (string) $result : '0';
    }

    public function getVersion(string $version) : Version
    {
        $this->loadMigrationsFromDirectory();

        if (! isset($this->versions[$version])) {
            throw UnknownMigrationVersion::new($version);
        }

        return $this->versions[$version];
    }

    public function hasVersion(string $version) : bool
    {
        $this->loadMigrationsFromDirectory();

        return isset($this->versions[$version]);
    }

    public function hasVersionMigrated(Version $version) : bool
    {
        return $this->getVersionData($version) !== null;
    }

    /**
     * @return mixed[]|null
     */
    public function getVersionData(Version $version) : ?array
    {
        $this->configuration->connect();
        $this->configuration->createMigrationTable();

        $sql = sprintf(
            'SELECT %s, %s FROM %s WHERE %s = ?',
            $this->configuration->getQuotedMigrationsColumnName(),
            $this->configuration->getQuotedMigrationsExecutedAtColumnName(),
            $this->configuration->getMigrationsTableName(),
            $this->configuration->getQuotedMigrationsColumnName()
        );

        $data = $this->connection->fetchAssoc($sql, [$version->getVersion()]);

        return is_array($data) ? $data : null;
    }

    /**
     * @return Version[]
     */
    public function getMigrations() : array
    {
        $this->loadMigrationsFromDirectory();

        return $this->versions;
    }

    /** @return string[] */
    public function getAvailableVersions() : array
    {
        $availableVersions = [];

        $this->loadMigrationsFromDirectory();

        foreach ($this->versions as $migration) {
            $availableVersions[] = $migration->getVersion();
        }

        return $availableVersions;
    }

    /** @return string[] */
    public function getNewVersions() : array
    {
        $availableMigrations = $this->getAvailableVersions();
        $executedMigrations  = $this->getMigratedVersions();

        return array_diff($availableMigrations, $executedMigrations);
    }

    /** @return string[] */
    public function getMigratedVersions() : array
    {
        $this->configuration->createMigrationTable();

        if (! $this->configuration->isMigrationTableCreated() && $this->configuration->isDryRun()) {
            return [];
        }

        $this->configuration->connect();

        $sql = sprintf(
            'SELECT %s FROM %s',
            $this->configuration->getQuotedMigrationsColumnName(),
            $this->configuration->getMigrationsTableName()
        );

        $result = $this->connection->fetchAll($sql);

        return array_map('current', $result);
    }

    /**
     * @return string[]
     */
    public function getExecutedUnavailableMigrations() : array
    {
        $executedMigrations  = $this->getMigratedVersions();
        $availableMigrations = $this->getAvailableVersions();

        return array_diff($executedMigrations, $availableMigrations);
    }

    public function getNumberOfAvailableMigrations() : int
    {
        $this->loadMigrationsFromDirectory();

        return count($this->versions);
    }

    public function getLatestVersion() : string
    {
        $this->loadMigrationsFromDirectory();

        $versions = array_keys($this->versions);
        $latest   = end($versions);

        return $latest !== false ? (string) $latest : '0';
    }

    public function getNumberOfExecutedMigrations() : int
    {
        $this->configuration->connect();
        $this->configuration->createMigrationTable();

        $sql = sprintf(
            'SELECT COUNT(%s) FROM %s',
            $this->configuration->getQuotedMigrationsColumnName(),
            $this->configuration->getMigrationsTableName()
        );

        $result = $this->connection->fetchColumn($sql);

        return $result !== false ? (int) $result : 0;
    }

    public function getRelativeVersion(string $version, int $delta) : ?string
    {
        $this->loadMigrationsFromDirectory();

        $versions = array_map('strval', array_keys($this->versions));

        array_unshift($versions, '0');

        $offset = array_search($version, $versions, true);

        if ($offset === false || ! isset($versions[$offset + $delta])) {
            // Unknown version or delta out of bounds.
            return null;
        }

        return $versions[$offset + $delta];
    }

    public function getDeltaVersion(string $delta) : ?string
    {
        $symbol = substr($delta, 0, 1);
        $number = (int) substr($delta, 1);

        if ($number <= 0) {
            return null;
        }

        if ($symbol === '+' || $symbol === '-') {
            return $this->getRelativeVersion($this->getCurrentVersion(), (int) $delta);
        }

        return null;
    }

    public function getPrevVersion() : ?string
    {
        return $this->getRelativeVersion($this->getCurrentVersion(), -1);
    }

    public function getNextVersion() : ?string
    {
        return $this->getRelativeVersion($this->getCurrentVersion(), 1);
    }

    private function loadMigrationsFromDirectory() : void
    {
        $migrationsDirectory = $this->configuration->getMigrationsDirectory();

        if (count($this->versions) !== 0 || $migrationsDirectory === null) {
            return;
        }

        $this->registerMigrationsFromDirectory($migrationsDirectory);
    }

    /** @throws MigrationException */
    private function ensureMigrationClassExists(string $class) : void
    {
        if (! class_exists($class)) {
            throw MigrationClassNotFound::new(
                $class,
                $this->configuration->getMigrationsNamespace()
            );
        }
    }
}

{'fraction_non_alphanumeric': 0.13271140535660547, 'fraction_numerical': 0.001504664459825459, 'mean_word_length': 2.5277777777777777, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 235, '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': '3623681', 'n_tokens_mistral': 5412, 'n_tokens_neox': 5304, 'n_words': 1022}