<commit_before><?php

namespace Bolt\Storage\Database\Schema\Comparison;

use Bolt\Storage\Database\Schema\SchemaCheck;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Psr\Log\LoggerInterface;

/**
 * Base class for handling table comparison.
 *
 * @author <NAME> <<EMAIL>>
 */
abstract class BaseComparator
{
    /** @var \Doctrine\DBAL\Connection */
    protected $connection;
    /** @var string */
    protected $prefix;
    /** @var \Psr\Log\LoggerInterface */
    protected $systemLog;

    /** @var \Doctrine\DBAL\Schema\TableDiff[] */
    protected $diffs;
    /** @var \Doctrine\DBAL\Schema\Table[] */
    protected $tablesCreate;
    /** @var \Doctrine\DBAL\Schema\TableDiff[] */
    protected $tablesAlter;
    /** @var IgnoredChange[] */
    protected $ignoredChanges = [];
    /** @var boolean */
    protected $pending;
    /** @var \Bolt\Storage\Database\Schema\SchemaCheck */
    protected $response;

    /**
     * Constructor.
     *
     * @param Connection      $connection
     * @param string          $prefix
     * @param LoggerInterface $systemLog
     */
    public function __construct(Connection $connection, $prefix, LoggerInterface $systemLog)
    {
        $this->connection = $connection;
        $this->prefix = $prefix;
        $this->systemLog = $systemLog;
        $this->setIgnoredChanges();
    }

    /**
     * Are database updates required.
     *
     * @param Table[] $fromTables
     * @param Table[] $toTables
     * @param array   $protectedTableNames
     *
     * @return bool
     */
    public function hasPending($fromTables, $toTables, array $protectedTableNames)
    {
        if ($this->pending !== null) {
            return $this->pending;
        }
        $this->compare($fromTables, $toTables, $protectedTableNames);

        return $this->pending;
    }

    /**
     * Run the update checks and flag if we need an update.
     *
     * @param Table[] $fromTables
     * @param Table[] $toTables
     * @param array   $protectedTableNames
     * @param bool    $force
     *
     * @return SchemaCheck
     */
    public function compare($fromTables, $toTables, array $protectedTableNames, $force = false)
    {
        if ($this->response !== null && $force === false) {
            return $this->getResponse();
        }

        $this->checkTables($fromTables, $toTables);

        // If we have diffs, check if they need to be modified
        if ($this->diffs !== null) {
            $this->adjustDiffs($protectedTableNames);
            $this->addAlterResponses();
        }

        return $this->getResponse();
    }

    /**
     * Get the schema check response object.
     *
     * @return \Bolt\Storage\Database\Schema\SchemaCheck
     */
    public function getResponse()
    {
        if ($this->response !== null) {
            return $this->response;
        }

        return $this->response = new SchemaCheck();
    }

    /**
     * Get the table creation SQL queries.
     *
     * @return string[]
     */
    public function getCreates()
    {
        if ($this->tablesCreate === null) {
            return [];
        }

        $queries = [];
        foreach ($this->tablesCreate as $tableName => $table) {
            $queries[$tableName] = $this->connection->getDatabasePlatform()
                ->getCreateTableSQL($table, AbstractPlatform::CREATE_INDEXES | AbstractPlatform::CREATE_FOREIGNKEYS);
        }

        return $queries;
    }

    /**
     * Get the table alteration SQL queries.
     *
     * @return string[]
     */
    public function getAlters()
    {
        $queries = [];
        if ($this->tablesAlter === null) {
            return $queries;
        }

        /** @var $tableDiff TableDiff */
        foreach ($this->tablesAlter as $tableName => $tableDiff) {
            $queries[$tableName] = $this->connection->getDatabasePlatform()
                ->getAlterTableSQL($tableDiff);
        }

        return $queries;
    }

    /**
     * Create a list of changes this platform will ignore.
     */<commit_msg>Getter for unmodified table diffs from the comparitor<commit_after><?php

namespace Bolt\Storage\Database\Schema\Comparison;

use Bolt\Storage\Database\Schema\SchemaCheck;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Psr\Log\LoggerInterface;

/**
 * Base class for handling table comparison.
 *
 * @author <NAME> <<EMAIL>>
 */
abstract class BaseComparator
{
    /** @var \Doctrine\DBAL\Connection */
    protected $connection;
    /** @var string */
    protected $prefix;
    /** @var \Psr\Log\LoggerInterface */
    protected $systemLog;

    /** @var \Doctrine\DBAL\Schema\TableDiff[] */
    protected $diffs;
    /** @var \Doctrine\DBAL\Schema\Table[] */
    protected $tablesCreate;
    /** @var \Doctrine\DBAL\Schema\TableDiff[] */
    protected $tablesAlter;
    /** @var IgnoredChange[] */
    protected $ignoredChanges = [];
    /** @var boolean */
    protected $pending;
    /** @var \Bolt\Storage\Database\Schema\SchemaCheck */
    protected $response;

    /**
     * Constructor.
     *
     * @param Connection      $connection
     * @param string          $prefix
     * @param LoggerInterface $systemLog
     */
    public function __construct(Connection $connection, $prefix, LoggerInterface $systemLog)
    {
        $this->connection = $connection;
        $this->prefix = $prefix;
        $this->systemLog = $systemLog;
        $this->setIgnoredChanges();
    }

    /**
     * Are database updates required.
     *
     * @param Table[] $fromTables
     * @param Table[] $toTables
     * @param array   $protectedTableNames
     *
     * @return bool
     */
    public function hasPending($fromTables, $toTables, array $protectedTableNames)
    {
        if ($this->pending !== null) {
            return $this->pending;
        }
        $this->compare($fromTables, $toTables, $protectedTableNames);

        return $this->pending;
    }

    /**
     * Run the update checks and flag if we need an update.
     *
     * @param Table[] $fromTables
     * @param Table[] $toTables
     * @param array   $protectedTableNames
     * @param bool    $force
     *
     * @return SchemaCheck
     */
    public function compare($fromTables, $toTables, array $protectedTableNames, $force = false)
    {
        if ($this->response !== null && $force === false) {
            return $this->getResponse();
        }

        $this->checkTables($fromTables, $toTables);

        // If we have diffs, check if they need to be modified
        if ($this->diffs !== null) {
            $this->adjustDiffs($protectedTableNames);
            $this->addAlterResponses();
        }

        return $this->getResponse();
    }

    /**
     * Get the schema check response object.
     *
     * @return \Bolt\Storage\Database\Schema\SchemaCheck
     */
    public function getResponse()
    {
        if ($this->response !== null) {
            return $this->response;
        }

        return $this->response = new SchemaCheck();
    }

    /**
     * Get the table creation SQL queries.
     *
     * @return string[]
     */
    public function getCreates()
    {
        if ($this->tablesCreate === null) {
            return [];
        }

        $queries = [];
        foreach ($this->tablesCreate as $tableName => $table) {
            $queries[$tableName] = $this->connection->getDatabasePlatform()
                ->getCreateTableSQL($table, AbstractPlatform::CREATE_INDEXES | AbstractPlatform::CREATE_FOREIGNKEYS);
        }

        return $queries;
    }

    /**
     * Get the table alteration SQL queries.
     *
     * @return string[]
     */
    public function getAlters()
    {
        $queries = [];
        if ($this->tablesAlter === null) {
            return $queries;
        }

        /** @var $tableDiff TableDiff */
        foreach ($this->tablesAlter as $tableName => $tableDiff) {
            $queries[$tableName] = $this->connection->getDatabasePlatform()
                ->getAlterTableSQL($tableDiff);
        }

        return $queries;
    }

    /**
     * Get the unmodified table diffs.
     *
     * @return TableDiff[]
     */
    public function getDiffs()
    {
        return $this->diffs;
    }

    /**
     * Create a list of changes this platform will ignore.
     */
{'fraction_non_alphanumeric': 0.1343808630393996, 'fraction_numerical': 0.0, 'mean_word_length': 2.1729910714285716, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 70, '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': '2272864', 'n_tokens_mistral': 2435, 'n_tokens_neox': 2402, 'n_words': 553}