Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split and move around exceptions, make MigrationException an interface #636

Merged
merged 1 commit into from
May 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions lib/Doctrine/Migrations/AbortMigrationException.php

This file was deleted.

13 changes: 8 additions & 5 deletions lib/Doctrine/Migrations/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\Exception\AbortMigration;
use Doctrine\Migrations\Exception\IrreversibleMigration;
use Doctrine\Migrations\Exception\SkipMigration;
use function sprintf;

abstract class AbstractMigration
Expand Down Expand Up @@ -73,22 +76,22 @@ public function warnIf(bool $condition, string $message = '') : void
}

/**
* @throws AbortMigrationException
* @throws AbortMigration
*/
public function abortIf(bool $condition, string $message = '') : void
{
if ($condition) {
throw new AbortMigrationException($message ?: 'Unknown Reason');
throw new AbortMigration($message ?: 'Unknown Reason');
}
}

/**
* @throws SkipMigrationException
* @throws SkipMigration
*/
public function skipIf(bool $condition, string $message = '') : void
{
if ($condition) {
throw new SkipMigrationException($message ?: 'Unknown Reason');
throw new SkipMigration($message ?: 'Unknown Reason');
}
}

Expand Down Expand Up @@ -134,6 +137,6 @@ protected function throwIrreversibleMigrationException(?string $message = null)
$message = 'This migration is irreversible and cannot be reverted.';
}

throw new IrreversibleMigrationException($message);
throw new IrreversibleMigration($message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

namespace Doctrine\Migrations\Configuration;

use Doctrine\Migrations\MigrationException;
use Doctrine\Migrations\Configuration\Exception\FileAlreadyLoaded;
use Doctrine\Migrations\Configuration\Exception\InvalidConfigurationKey;
use Doctrine\Migrations\Configuration\Exception\UnknownConfigurationValue;
use Doctrine\Migrations\Exception\MigrationException;
use InvalidArgumentException;
use function dirname;
use function file_exists;
use function getcwd;
use function in_array;
use function realpath;
use function sprintf;
use function strcasecmp;
use function var_export;

abstract class AbstractFileConfiguration extends Configuration
{
Expand Down Expand Up @@ -42,9 +43,7 @@ protected function setConfiguration(array $config) : void
{
foreach ($config as $configurationKey => $configurationValue) {
if (! in_array($configurationKey, self::ALLOWED_CONFIGURATION_KEYS, true)) {
$msg = sprintf('Migrations configuration key "%s" does not exist.', $configurationKey);

throw MigrationException::configurationNotValid($msg);
throw InvalidConfigurationKey::new($configurationKey);
}
}

Expand Down Expand Up @@ -107,16 +106,15 @@ private function setMigrationOrganization(string $migrationOrganization) : void
} elseif (strcasecmp($migrationOrganization, static::VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH) === 0) {
$this->setMigrationsAreOrganizedByYearAndMonth();
} else {
$msg = 'Unknown ' . var_export($migrationOrganization, true) . ' for configuration "organize_migrations".';
throw MigrationException::configurationNotValid($msg);
throw UnknownConfigurationValue::new('organize_migrations', $migrationOrganization);
}
}

/** @throws MigrationException */
public function load(string $file) : void
{
if ($this->loaded) {
throw MigrationException::configurationFileAlreadyLoaded();
throw FileAlreadyLoaded::new();
}

$path = getcwd() . '/' . $file;
Expand Down
22 changes: 14 additions & 8 deletions lib/Doctrine/Migrations/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type;
use Doctrine\Migrations\Configuration\Exception\MigrationsNamespaceRequired;
use Doctrine\Migrations\Configuration\Exception\ParameterIncompatibleWithFinder;
use Doctrine\Migrations\Exception\DuplicateMigrationVersion;
use Doctrine\Migrations\Exception\MigrationClassNotFound;
use Doctrine\Migrations\Exception\MigrationException;
use Doctrine\Migrations\Exception\MigrationsDirectoryRequired;
use Doctrine\Migrations\Exception\UnknownMigrationVersion;
use Doctrine\Migrations\FileQueryWriter;
use Doctrine\Migrations\Finder\MigrationDeepFinder;
use Doctrine\Migrations\Finder\MigrationFinder;
use Doctrine\Migrations\Finder\RecursiveRegexFinder;
use Doctrine\Migrations\MigrationException;
use Doctrine\Migrations\OutputWriter;
use Doctrine\Migrations\QueryWriter;
use Doctrine\Migrations\Version;
Expand Down Expand Up @@ -119,11 +125,11 @@ public function areMigrationsOrganizedByYearAndMonth() : bool
public function validate() : void
{
if ($this->migrationsNamespace === null) {
throw MigrationException::migrationsNamespaceRequired();
throw MigrationsNamespaceRequired::new();
}

if ($this->migrationsDirectory === null) {
throw MigrationException::migrationsDirectoryRequired();
throw MigrationsDirectoryRequired::new();
}
}

Expand Down Expand Up @@ -224,7 +230,7 @@ public function setMigrationsFinder(MigrationFinder $finder) : void
{
if (($this->migrationsAreOrganizedByYear || $this->migrationsAreOrganizedByYearAndMonth)
&& ! ($finder instanceof MigrationDeepFinder)) {
throw MigrationException::configurationIncompatibleWithFinder(
throw ParameterIncompatibleWithFinder::new(
'organize-migrations',
$finder
);
Expand All @@ -247,7 +253,7 @@ public function registerMigration(string $version, string $class) : Version
$this->ensureMigrationClassExists($class);

if (isset($this->migrations[$version])) {
throw MigrationException::duplicateMigrationVersion(
throw DuplicateMigrationVersion::new(
$version,
get_class($this->migrations[$version])
);
Expand Down Expand Up @@ -291,7 +297,7 @@ public function getVersion(string $version) : Version
$this->loadMigrationsFromDirectory();

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

return $this->migrations[$version];
Expand Down Expand Up @@ -644,7 +650,7 @@ protected function connect() : bool
private function ensureOrganizeMigrationsIsCompatibleWithFinder() : void
{
if (! ($this->migrationFinder instanceof MigrationDeepFinder)) {
throw MigrationException::configurationIncompatibleWithFinder(
throw ParameterIncompatibleWithFinder::new(
'organize-migrations',
$this->migrationFinder
);
Expand Down Expand Up @@ -683,7 +689,7 @@ private function shouldExecuteMigration(
private function ensureMigrationClassExists(string $class) : void
{
if (! class_exists($class)) {
throw MigrationException::migrationClassNotFound(
throw MigrationClassNotFound::new(
$class,
$this->getMigrationsNamespace()
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Exception;

use Doctrine\Migrations\Exception\MigrationException;

interface ConfigurationException extends MigrationException
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Exception;

use LogicException;

final class FileAlreadyLoaded extends LogicException implements ConfigurationException
{
public static function new() : self
{
return new self('Migrations configuration file already loaded', 8);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Exception;

use LogicException;
use function sprintf;

final class InvalidConfigurationKey extends LogicException implements ConfigurationException
{
public static function new(string $key) : self
{
return new self(sprintf('Migrations configuration key "%s" does not exist.', $key), 10);
}
}
15 changes: 15 additions & 0 deletions lib/Doctrine/Migrations/Configuration/Exception/JsonNotValid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Exception;

use LogicException;

final class JsonNotValid extends LogicException implements ConfigurationException
{
public static function new() : self
{
return new self('Configuration is not valid JSON.', 10);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Exception;

use LogicException;

final class MigrationsNamespaceRequired extends LogicException implements ConfigurationException
{
public static function new() : self
{
return new self(
'Migrations namespace must be configured in order to use Doctrine migrations.',
2
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Exception;

use Doctrine\Migrations\Finder\MigrationFinder;
use LogicException;
use function get_class;
use function sprintf;

final class ParameterIncompatibleWithFinder extends LogicException implements ConfigurationException
{
public static function new(string $configurationParameterName, MigrationFinder $finder) : self
{
return new self(
sprintf(
'Configuration-parameter "%s" cannot be used with finder of type "%s"',
$configurationParameterName,
get_class($finder)
),
9
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Exception;

use LogicException;
use function sprintf;
use function var_export;

final class UnknownConfigurationValue extends LogicException implements ConfigurationException
{
/**
* @param mixed $value
*/
public static function new(string $key, $value) : self
{
return new self(
sprintf(
'Unknown %s for configuration "%s".',
var_export($value, true),
$key
),
10
);
}
}
15 changes: 15 additions & 0 deletions lib/Doctrine/Migrations/Configuration/Exception/XmlNotValid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Exception;

use LogicException;

final class XmlNotValid extends LogicException implements ConfigurationException
{
public static function new() : self
{
return new self('XML configuration did not pass the validation test.', 10);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Exception;

use LogicException;

final class YamlNotAvailable extends LogicException implements ConfigurationException
{
public static function new() : self
{
return new self(
'Unable to load yaml configuration files, please run '
. '`composer require symfony/yaml` to load yaml configuration files.'
);
}
}
15 changes: 15 additions & 0 deletions lib/Doctrine/Migrations/Configuration/Exception/YamlNotValid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Exception;

use LogicException;

final class YamlNotValid extends LogicException implements ConfigurationException
{
public static function new() : self
{
return new self('Configuration is not valid YAML.', 10);
}
}
4 changes: 2 additions & 2 deletions lib/Doctrine/Migrations/Configuration/JsonConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Doctrine\Migrations\Configuration;

use Doctrine\Migrations\MigrationException;
use Doctrine\Migrations\Configuration\Exception\JsonNotValid;
use function file_get_contents;
use function json_decode;

Expand All @@ -16,7 +16,7 @@ protected function doLoad(string $file) : void
$config = json_decode(file_get_contents($file), true);

if ($config === false) {
throw MigrationException::configurationNotValid('Configuration is not valid JSON.');
throw JsonNotValid::new();
}

if (isset($config['migrations_directory'])) {
Expand Down
Loading