From 3922433e0764f1f372178b104d08c70340d36d51 Mon Sep 17 00:00:00 2001 From: Michael Moravec Date: Tue, 5 Jun 2018 21:04:31 +0200 Subject: [PATCH] Throw only specialized package-only exceptions --- .../AbstractFileConfiguration.php | 9 +++--- .../ArrayConnectionConfigurationLoader.php | 8 ++--- .../Loader/Exception/InvalidConfiguration.php | 15 ++++++++++ .../Loader/Exception/LoaderException.php | 11 +++++++ .../Configuration/Exception/FileNotFound.php | 15 ++++++++++ .../Migrations/Exception/NoTablesFound.php | 15 ++++++++++ .../Migrations/Exception/RollupFailed.php | 20 +++++++++++++ .../Finder/Exception/FInderException.php | 9 ++++++ .../Finder/Exception/InvalidDirectory.php | 16 ++++++++++ .../Finder/Exception/NameIsReserved.php | 22 ++++++++++++++ lib/Doctrine/Migrations/Finder/Finder.php | 21 ++++++-------- .../Migrations/Generator/DiffGenerator.php | 7 +++-- .../Exception/GeneratorException.php | 11 +++++++ .../Exception/InvalidTemplateSpecified.php | 26 +++++++++++++++++ .../Generator/Exception/NoChangesDetected.php | 15 ++++++++++ .../Migrations/Generator/Generator.php | 27 +++++------------ .../Provider/Exception/NoMappingFound.php | 15 ++++++++++ .../Provider/Exception/ProviderException.php | 11 +++++++ .../Migrations/Provider/OrmSchemaProvider.php | 7 +++-- lib/Doctrine/Migrations/Rollup.php | 9 ++++-- lib/Doctrine/Migrations/SchemaDumper.php | 7 +++-- .../Tools/Console/Command/DiffCommand.php | 7 +++-- .../Console/Command/DumpSchemaCommand.php | 7 +++-- .../Tools/Console/Command/VersionCommand.php | 29 +++++++++---------- .../Tools/Console/ConnectionLoader.php | 6 ++-- .../Exception/ConnectionNotSpecified.php | 17 +++++++++++ .../Console/Exception/ConsoleException.php | 11 +++++++ .../Exception/DirectoryDoesNotExist.php | 16 ++++++++++ .../Exception/FileTypeNotSupported.php | 15 ++++++++++ .../Console/Exception/InvalidOptionUsage.php | 15 ++++++++++ .../SchemaDumpRequiresNoMigrations.php | 15 ++++++++++ .../Exception/VersionAlreadyExists.php | 17 +++++++++++ .../Console/Exception/VersionDoesNotExist.php | 17 +++++++++++ .../Console/Helper/ConfigurationHelper.php | 8 +++-- .../Helper/MigrationDirectoryHelper.php | 9 +++--- 35 files changed, 405 insertions(+), 80 deletions(-) create mode 100644 lib/Doctrine/Migrations/Configuration/Connection/Loader/Exception/InvalidConfiguration.php create mode 100644 lib/Doctrine/Migrations/Configuration/Connection/Loader/Exception/LoaderException.php create mode 100644 lib/Doctrine/Migrations/Configuration/Exception/FileNotFound.php create mode 100644 lib/Doctrine/Migrations/Exception/NoTablesFound.php create mode 100644 lib/Doctrine/Migrations/Exception/RollupFailed.php create mode 100644 lib/Doctrine/Migrations/Finder/Exception/FInderException.php create mode 100644 lib/Doctrine/Migrations/Finder/Exception/InvalidDirectory.php create mode 100644 lib/Doctrine/Migrations/Finder/Exception/NameIsReserved.php create mode 100644 lib/Doctrine/Migrations/Generator/Exception/GeneratorException.php create mode 100644 lib/Doctrine/Migrations/Generator/Exception/InvalidTemplateSpecified.php create mode 100644 lib/Doctrine/Migrations/Generator/Exception/NoChangesDetected.php create mode 100644 lib/Doctrine/Migrations/Provider/Exception/NoMappingFound.php create mode 100644 lib/Doctrine/Migrations/Provider/Exception/ProviderException.php create mode 100644 lib/Doctrine/Migrations/Tools/Console/Exception/ConnectionNotSpecified.php create mode 100644 lib/Doctrine/Migrations/Tools/Console/Exception/ConsoleException.php create mode 100644 lib/Doctrine/Migrations/Tools/Console/Exception/DirectoryDoesNotExist.php create mode 100644 lib/Doctrine/Migrations/Tools/Console/Exception/FileTypeNotSupported.php create mode 100644 lib/Doctrine/Migrations/Tools/Console/Exception/InvalidOptionUsage.php create mode 100644 lib/Doctrine/Migrations/Tools/Console/Exception/SchemaDumpRequiresNoMigrations.php create mode 100644 lib/Doctrine/Migrations/Tools/Console/Exception/VersionAlreadyExists.php create mode 100644 lib/Doctrine/Migrations/Tools/Console/Exception/VersionDoesNotExist.php diff --git a/lib/Doctrine/Migrations/Configuration/AbstractFileConfiguration.php b/lib/Doctrine/Migrations/Configuration/AbstractFileConfiguration.php index b69ad270ab..b4b63928aa 100644 --- a/lib/Doctrine/Migrations/Configuration/AbstractFileConfiguration.php +++ b/lib/Doctrine/Migrations/Configuration/AbstractFileConfiguration.php @@ -5,10 +5,9 @@ namespace Doctrine\Migrations\Configuration; use Doctrine\Migrations\Configuration\Exception\FileAlreadyLoaded; +use Doctrine\Migrations\Configuration\Exception\FileNotFound; 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; @@ -58,7 +57,9 @@ abstract class AbstractFileConfiguration extends Configuration /** @var bool */ private $loaded = false; - /** @throws MigrationException */ + /** + * @throws FileNotFound + */ public function load(string $file) : void { if ($this->loaded) { @@ -74,7 +75,7 @@ public function load(string $file) : void $this->file = $file; if (! file_exists($file)) { - throw new InvalidArgumentException('Given config file does not exist'); + throw FileNotFound::new(); } $this->doLoad($file); diff --git a/lib/Doctrine/Migrations/Configuration/Connection/Loader/ArrayConnectionConfigurationLoader.php b/lib/Doctrine/Migrations/Configuration/Connection/Loader/ArrayConnectionConfigurationLoader.php index c46618f63d..261aed860a 100644 --- a/lib/Doctrine/Migrations/Configuration/Connection/Loader/ArrayConnectionConfigurationLoader.php +++ b/lib/Doctrine/Migrations/Configuration/Connection/Loader/ArrayConnectionConfigurationLoader.php @@ -7,7 +7,7 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\DriverManager; use Doctrine\Migrations\Configuration\Connection\ConnectionLoaderInterface; -use InvalidArgumentException; +use Doctrine\Migrations\Configuration\Connection\Loader\Exception\InvalidConfiguration; use function file_exists; use function is_array; @@ -31,7 +31,7 @@ public function __construct(?string $filename) * Read the input and return a Configuration, returns null if the config * is not supported. * - * @throws InvalidArgumentException + * @throws InvalidConfiguration */ public function chosen() : ?Connection { @@ -46,9 +46,7 @@ public function chosen() : ?Connection $params = include $this->filename; if (! is_array($params)) { - throw new InvalidArgumentException( - 'The connection file has to return an array with database configuration parameters.' - ); + throw InvalidConfiguration::invalidArrayConfiguration(); } return DriverManager::getConnection($params); diff --git a/lib/Doctrine/Migrations/Configuration/Connection/Loader/Exception/InvalidConfiguration.php b/lib/Doctrine/Migrations/Configuration/Connection/Loader/Exception/InvalidConfiguration.php new file mode 100644 index 0000000000..3b14f939a8 --- /dev/null +++ b/lib/Doctrine/Migrations/Configuration/Connection/Loader/Exception/InvalidConfiguration.php @@ -0,0 +1,15 @@ +getShortName(), 7); if ($version === '0') { - throw new InvalidArgumentException(sprintf( - 'Cannot load a migrations with the name "%s" because it is a reserved number by doctrine migrations' . PHP_EOL . - 'It\'s used to revert all migrations including the first one.', - $version - )); + throw NameIsReserved::new($version); } $versions[$version] = $class->getName(); diff --git a/lib/Doctrine/Migrations/Generator/DiffGenerator.php b/lib/Doctrine/Migrations/Generator/DiffGenerator.php index 8174ea5f29..bf6aef2d77 100644 --- a/lib/Doctrine/Migrations/Generator/DiffGenerator.php +++ b/lib/Doctrine/Migrations/Generator/DiffGenerator.php @@ -8,8 +8,8 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\Schema; +use Doctrine\Migrations\Generator\Exception\NoChangesDetected; use Doctrine\Migrations\Provider\SchemaProviderInterface; -use RuntimeException; use function preg_match; use function strpos; use function substr; @@ -56,6 +56,9 @@ public function __construct( $this->migrationSqlGenerator = $migrationSqlGenerator; } + /** + * @throws NoChangesDetected + */ public function generate( string $versionNumber, ?string $filterExpression, @@ -83,7 +86,7 @@ public function generate( ); if ($up === '' && $down === '') { - throw new RuntimeException('No changes detected in your mapping information.'); + throw NoChangesDetected::new(); } return $this->migrationGenerator->generateMigration( diff --git a/lib/Doctrine/Migrations/Generator/Exception/GeneratorException.php b/lib/Doctrine/Migrations/Generator/Exception/GeneratorException.php new file mode 100644 index 0000000000..5e90fbc8ec --- /dev/null +++ b/lib/Doctrine/Migrations/Generator/Exception/GeneratorException.php @@ -0,0 +1,11 @@ +template; } + /** + * @throws InvalidTemplateSpecified + */ private function loadCustomTemplate() : ?string { $customTemplate = $this->configuration->getCustomTemplate(); @@ -124,32 +126,17 @@ private function loadCustomTemplate() : ?string } if (! is_file($customTemplate) || ! is_readable($customTemplate)) { - throw new InvalidArgumentException( - sprintf( - 'The specified template "%s" cannot be found or is not readable.', - $customTemplate - ) - ); + throw InvalidTemplateSpecified::notFoundOrNotReadable($customTemplate); } $content = file_get_contents($customTemplate); if ($content === false) { - throw new InvalidArgumentException( - sprintf( - 'The specified template "%s" could not be read.', - $customTemplate - ) - ); + throw InvalidTemplateSpecified::notReadable($customTemplate); } if (trim($content) === '') { - throw new InvalidArgumentException( - sprintf( - 'The specified template "%s" is empty.', - $customTemplate - ) - ); + throw InvalidTemplateSpecified::empty($customTemplate); } return $content; diff --git a/lib/Doctrine/Migrations/Provider/Exception/NoMappingFound.php b/lib/Doctrine/Migrations/Provider/Exception/NoMappingFound.php new file mode 100644 index 0000000000..eded25a06f --- /dev/null +++ b/lib/Doctrine/Migrations/Provider/Exception/NoMappingFound.php @@ -0,0 +1,15 @@ +entityManager = $em; } + /** + * @throws NoMappingFound + */ public function createSchema() : Schema { $metadata = $this->entityManager->getMetadataFactory()->getAllMetadata(); if (count($metadata) === 0) { - throw new UnexpectedValueException('No mapping information to process'); + throw NoMappingFound::new(); } $tool = new SchemaTool($this->entityManager); diff --git a/lib/Doctrine/Migrations/Rollup.php b/lib/Doctrine/Migrations/Rollup.php index 495d12da3c..d2b9c3a164 100644 --- a/lib/Doctrine/Migrations/Rollup.php +++ b/lib/Doctrine/Migrations/Rollup.php @@ -6,8 +6,8 @@ use Doctrine\DBAL\Connection; use Doctrine\Migrations\Configuration\Configuration; +use Doctrine\Migrations\Exception\RollupFailed; use Doctrine\Migrations\Version\Version; -use RuntimeException; use function count; use function current; use function sprintf; @@ -41,16 +41,19 @@ public function __construct( $this->migrationRepository = $migrationRepository; } + /** + * @throws RollupFailed + */ public function rollup() : Version { $versions = $this->migrationRepository->getVersions(); if (count($versions) === 0) { - throw new RuntimeException('No migrations found.'); + throw RollupFailed::noMigrationsFound(); } if (count($versions) > 1) { - throw new RuntimeException('Too many migrations.'); + throw RollupFailed::tooManyMigrations(); } $sql = sprintf( diff --git a/lib/Doctrine/Migrations/SchemaDumper.php b/lib/Doctrine/Migrations/SchemaDumper.php index 21df4f021e..ae92bbd483 100644 --- a/lib/Doctrine/Migrations/SchemaDumper.php +++ b/lib/Doctrine/Migrations/SchemaDumper.php @@ -6,9 +6,9 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Schema\AbstractSchemaManager; +use Doctrine\Migrations\Exception\NoTablesFound; use Doctrine\Migrations\Generator\Generator; use Doctrine\Migrations\Generator\SqlGenerator; -use RuntimeException; use function count; use function implode; @@ -46,6 +46,9 @@ public function __construct( $this->migrationSqlGenerator = $migrationSqlGenerator; } + /** + * @throws NoTablesFound + */ public function dump( string $versionNumber, bool $formatted = false, @@ -85,7 +88,7 @@ public function dump( } if (count($up) === 0) { - throw new RuntimeException('Your database schema does not contain any tables.'); + throw NoTablesFound::new(); } $up = implode("\n", $up); diff --git a/lib/Doctrine/Migrations/Tools/Console/Command/DiffCommand.php b/lib/Doctrine/Migrations/Tools/Console/Command/DiffCommand.php index ffe460aa45..822657197c 100644 --- a/lib/Doctrine/Migrations/Tools/Console/Command/DiffCommand.php +++ b/lib/Doctrine/Migrations/Tools/Console/Command/DiffCommand.php @@ -7,7 +7,7 @@ use Doctrine\Migrations\Generator\DiffGenerator; use Doctrine\Migrations\Provider\OrmSchemaProvider; use Doctrine\Migrations\Provider\SchemaProviderInterface; -use InvalidArgumentException; +use Doctrine\Migrations\Tools\Console\Exception\InvalidOptionUsage; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -76,6 +76,9 @@ protected function configure() : void ; } + /** + * @throws InvalidOptionUsage + */ public function execute( InputInterface $input, OutputInterface $output @@ -86,7 +89,7 @@ public function execute( if ($formatted) { if (! class_exists('SqlFormatter')) { - throw new InvalidArgumentException( + throw InvalidOptionUsage::new( 'The "--formatted" option can only be used if the sql formatter is installed. Please run "composer require jdorn/sql-formatter".' ); } diff --git a/lib/Doctrine/Migrations/Tools/Console/Command/DumpSchemaCommand.php b/lib/Doctrine/Migrations/Tools/Console/Command/DumpSchemaCommand.php index c287ce6290..afc45c09b1 100644 --- a/lib/Doctrine/Migrations/Tools/Console/Command/DumpSchemaCommand.php +++ b/lib/Doctrine/Migrations/Tools/Console/Command/DumpSchemaCommand.php @@ -4,7 +4,7 @@ namespace Doctrine\Migrations\Tools\Console\Command; -use RuntimeException; +use Doctrine\Migrations\Tools\Console\Exception\SchemaDumpRequiresNoMigrations; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -57,6 +57,9 @@ protected function configure() : void ; } + /** + * @throws SchemaDumpRequiresNoMigrations + */ public function execute( InputInterface $input, OutputInterface $output @@ -68,7 +71,7 @@ public function execute( $versions = $this->migrationRepository->getVersions(); if (count($versions) > 0) { - throw new RuntimeException('Delete your old historical migrations before dumping your schema.'); + throw SchemaDumpRequiresNoMigrations::new(); } $versionNumber = $this->configuration->generateVersionNumber(); diff --git a/lib/Doctrine/Migrations/Tools/Console/Command/VersionCommand.php b/lib/Doctrine/Migrations/Tools/Console/Command/VersionCommand.php index 81064249ef..8aa88b9826 100644 --- a/lib/Doctrine/Migrations/Tools/Console/Command/VersionCommand.php +++ b/lib/Doctrine/Migrations/Tools/Console/Command/VersionCommand.php @@ -5,7 +5,9 @@ namespace Doctrine\Migrations\Tools\Console\Command; use Doctrine\Migrations\Exception\UnknownMigrationVersion; -use InvalidArgumentException; +use Doctrine\Migrations\Tools\Console\Exception\InvalidOptionUsage; +use Doctrine\Migrations\Tools\Console\Exception\VersionAlreadyExists; +use Doctrine\Migrations\Tools\Console\Exception\VersionDoesNotExist; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -90,12 +92,13 @@ protected function configure() : void parent::configure(); } + /** + * @throws InvalidOptionUsage + */ public function execute(InputInterface $input, OutputInterface $output) : ?int { if ($input->getOption('add') === false && $input->getOption('delete') === false) { - throw new InvalidArgumentException( - 'You must specify whether you want to --add or --delete the specified version.' - ); + throw InvalidOptionUsage::new('You must specify whether you want to --add or --delete the specified version.'); } $this->markMigrated = (bool) $input->getOption('add'); @@ -118,8 +121,7 @@ public function execute(InputInterface $input, OutputInterface $output) : ?int } /** - * @throws InvalidArgumentException - * @throws UnknownMigrationVersion + * @throws InvalidOptionUsage */ private function markVersions(InputInterface $input, OutputInterface $output) : void { @@ -129,13 +131,13 @@ private function markVersions(InputInterface $input, OutputInterface $output) : $rangeToOption = $input->getOption('range-to'); if ($allOption === true && ($rangeFromOption !== null || $rangeToOption !== null)) { - throw new InvalidArgumentException( + throw InvalidOptionUsage::new( 'Options --all and --range-to/--range-from both used. You should use only one of them.' ); } if ($rangeFromOption !== null ^ $rangeToOption !== null) { - throw new InvalidArgumentException( + throw InvalidOptionUsage::new( 'Options --range-to and --range-from should be used together.' ); } @@ -162,7 +164,8 @@ private function markVersions(InputInterface $input, OutputInterface $output) : } /** - * @throws InvalidArgumentException + * @throws VersionAlreadyExists + * @throws VersionDoesNotExist * @throws UnknownMigrationVersion */ private function mark(OutputInterface $output, string $version, bool $all = false) : void @@ -177,9 +180,7 @@ private function mark(OutputInterface $output, string $version, bool $all = fals if ($this->markMigrated && $this->migrationRepository->hasVersionMigrated($version)) { if (! $all) { - throw new InvalidArgumentException( - sprintf('The version "%s" already exists in the version table.', $version) - ); + throw VersionAlreadyExists::new($version); } $marked = true; @@ -187,9 +188,7 @@ private function mark(OutputInterface $output, string $version, bool $all = fals if (! $this->markMigrated && ! $this->migrationRepository->hasVersionMigrated($version)) { if (! $all) { - throw new InvalidArgumentException( - sprintf('The version "%s" does not exist in the version table.', $version) - ); + throw VersionDoesNotExist::new($version); } $marked = true; diff --git a/lib/Doctrine/Migrations/Tools/Console/ConnectionLoader.php b/lib/Doctrine/Migrations/Tools/Console/ConnectionLoader.php index d4769a8d25..92fbadf6db 100644 --- a/lib/Doctrine/Migrations/Tools/Console/ConnectionLoader.php +++ b/lib/Doctrine/Migrations/Tools/Console/ConnectionLoader.php @@ -11,7 +11,7 @@ use Doctrine\Migrations\Configuration\Connection\Loader\ConnectionConfigurationChainLoader; use Doctrine\Migrations\Configuration\Connection\Loader\ConnectionConfigurationLoader; use Doctrine\Migrations\Configuration\Connection\Loader\ConnectionHelperLoader; -use InvalidArgumentException; +use Doctrine\Migrations\Tools\Console\Exception\ConnectionNotSpecified; use Symfony\Component\Console\Helper\HelperSet; use Symfony\Component\Console\Input\InputInterface; @@ -39,9 +39,7 @@ public function getConnection(InputInterface $input, HelperSet $helperSet) : Con return $connection; } - throw new InvalidArgumentException( - 'You have to specify a --db-configuration file or pass a Database Connection as a dependency to the Migrations.' - ); + throw ConnectionNotSpecified::new(); } protected function createConnectionConfigurationChainLoader( diff --git a/lib/Doctrine/Migrations/Tools/Console/Exception/ConnectionNotSpecified.php b/lib/Doctrine/Migrations/Tools/Console/Exception/ConnectionNotSpecified.php new file mode 100644 index 0000000000..098c841efc --- /dev/null +++ b/lib/Doctrine/Migrations/Tools/Console/Exception/ConnectionNotSpecified.php @@ -0,0 +1,17 @@ +configuration = $configuration; } - /** @throws InvalidArgumentException */ + /** + * @throws DirectoryDoesNotExist + */ public function getMigrationDirectory() : string { $dir = $this->configuration->getMigrationsDirectory(); @@ -37,7 +38,7 @@ public function getMigrationDirectory() : string $dir = rtrim($dir, '/'); if (! file_exists($dir)) { - throw new InvalidArgumentException(sprintf('Migrations directory "%s" does not exist.', $dir)); + throw DirectoryDoesNotExist::new($dir); } if ($this->configuration->areMigrationsOrganizedByYear()) {