Skip to content

Commit

Permalink
Throw only specialized package-only exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Majkl578 committed Jun 5, 2018
1 parent 38e5cd6 commit 3922433
Show file tree
Hide file tree
Showing 35 changed files with 405 additions and 80 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
Expand Up @@ -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;

Expand All @@ -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
{
Expand All @@ -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);
Expand Down
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Connection\Loader\Exception;

use InvalidArgumentException;

final class InvalidConfiguration extends InvalidArgumentException implements LoaderException
{
public static function invalidArrayConfiguration() : self
{
return new self('The connection file has to return an array with database configuration parameters.');
}
}
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Connection\Loader\Exception;

use Doctrine\Migrations\Exception\MigrationException;

interface LoaderException extends MigrationException
{
}
15 changes: 15 additions & 0 deletions lib/Doctrine/Migrations/Configuration/Exception/FileNotFound.php
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Exception;

use InvalidArgumentException;

final class FileNotFound extends InvalidArgumentException implements ConfigurationException
{
public static function new() : self
{
return new self('Given config file does not exist');
}
}
15 changes: 15 additions & 0 deletions lib/Doctrine/Migrations/Exception/NoTablesFound.php
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Exception;

use RuntimeException;

final class NoTablesFound extends RuntimeException implements MigrationException
{
public static function new() : self
{
return new self('Your database schema does not contain any tables.');
}
}
20 changes: 20 additions & 0 deletions lib/Doctrine/Migrations/Exception/RollupFailed.php
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Exception;

use RuntimeException;

final class RollupFailed extends RuntimeException implements MigrationException
{
public static function noMigrationsFound() : self
{
return new self('No migrations found.');
}

public static function tooManyMigrations() : self
{
return new self('Too many migrations.');
}
}
9 changes: 9 additions & 0 deletions lib/Doctrine/Migrations/Finder/Exception/FInderException.php
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Finder\Exception;

interface FInderException
{
}
16 changes: 16 additions & 0 deletions lib/Doctrine/Migrations/Finder/Exception/InvalidDirectory.php
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Finder\Exception;

use InvalidArgumentException;
use function sprintf;

final class InvalidDirectory extends InvalidArgumentException implements FInderException
{
public static function new(string $directory) : self
{
return new self(sprintf('Cannot load migrations from "%s" because it is not a valid directory', $directory));
}
}
22 changes: 22 additions & 0 deletions lib/Doctrine/Migrations/Finder/Exception/NameIsReserved.php
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Finder\Exception;

use InvalidArgumentException;
use const PHP_EOL;
use function sprintf;

final class NameIsReserved extends InvalidArgumentException implements FInderException
{
public static function new(string $version) : self
{
return new self(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
));
}
}
21 changes: 9 additions & 12 deletions lib/Doctrine/Migrations/Finder/Finder.php
Expand Up @@ -4,16 +4,15 @@

namespace Doctrine\Migrations\Finder;

use InvalidArgumentException;
use Doctrine\Migrations\Finder\Exception\InvalidDirectory;
use Doctrine\Migrations\Finder\Exception\NameIsReserved;
use ReflectionClass;
use const PHP_EOL;
use const SORT_STRING;
use function get_declared_classes;
use function in_array;
use function is_dir;
use function ksort;
use function realpath;
use function sprintf;
use function strlen;
use function strncmp;
use function substr;
Expand All @@ -28,15 +27,15 @@ protected static function requireOnce(string $path) : void
require_once $path;
}

/**
* @throws InvalidDirectory
*/
protected function getRealPath(string $directory) : string
{
$dir = realpath($directory);

if ($dir === false || ! is_dir($dir)) {
throw new InvalidArgumentException(sprintf(
'Cannot load migrations from "%s" because it is not a valid directory',
$directory
));
throw InvalidDirectory::new($directory);
}

return $dir;
Expand All @@ -46,6 +45,8 @@ protected function getRealPath(string $directory) : string
* @param string[] $files
*
* @return string[]
*
* @throws NameIsReserved
*/
protected function loadMigrations(array $files, ?string $namespace) : array
{
Expand All @@ -61,11 +62,7 @@ protected function loadMigrations(array $files, ?string $namespace) : array
$version = substr($class->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();
Expand Down
7 changes: 5 additions & 2 deletions lib/Doctrine/Migrations/Generator/DiffGenerator.php
Expand Up @@ -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;
Expand Down Expand Up @@ -56,6 +56,9 @@ public function __construct(
$this->migrationSqlGenerator = $migrationSqlGenerator;
}

/**
* @throws NoChangesDetected
*/
public function generate(
string $versionNumber,
?string $filterExpression,
Expand Down Expand Up @@ -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(
Expand Down
11 changes: 11 additions & 0 deletions lib/Doctrine/Migrations/Generator/Exception/GeneratorException.php
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Generator\Exception;

use Doctrine\Migrations\Exception\MigrationException;

interface GeneratorException extends MigrationException
{
}
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Generator\Exception;

use InvalidArgumentException;
use function sprintf;

final class InvalidTemplateSpecified extends InvalidArgumentException implements GeneratorException
{
public static function notFoundOrNotReadable(string $path) : self
{
return new self(sprintf('The specified template "%s" cannot be found or is not readable.', $path));
}

public static function notReadable(string $path) : self
{
return new self(sprintf('The specified template "%s" could not be read.', $path));
}

public static function empty(string $path) : self
{
return new self(sprintf('The specified template "%s" is empty.', $path));
}
}
15 changes: 15 additions & 0 deletions lib/Doctrine/Migrations/Generator/Exception/NoChangesDetected.php
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Generator\Exception;

use RuntimeException;

final class NoChangesDetected extends RuntimeException implements GeneratorException
{
public static function new() : self
{
return new self('No changes detected in your mapping information.');
}
}

0 comments on commit 3922433

Please sign in to comment.