From cdfbc09229d07d75bfd9539474150c442835195f Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Thu, 26 Dec 2019 15:41:11 +0300 Subject: [PATCH] - codestyle fixes --- CHANGELOG.md | 14 -- src/Atomizer/Atomizer.php | 2 +- src/Atomizer/Renderer.php | 177 +++++++++++++++++++------- src/Capsule.php | 10 +- src/Config/MigrationConfig.php | 10 -- src/Exception/BlueprintException.php | 3 - src/Exception/RepositoryException.php | 3 - src/FileRepository.php | 13 +- src/Migration.php | 4 +- src/Migrator.php | 50 +++++--- src/Operation/Column/Column.php | 4 +- src/Operation/Column/Drop.php | 2 +- src/Operation/Column/Rename.php | 14 +- src/Operation/ForeignKey/Add.php | 30 +++-- src/Operation/ForeignKey/Alter.php | 30 +++-- src/Operation/Index/Add.php | 2 +- src/Operation/Index/Alter.php | 2 +- src/Operation/Index/Drop.php | 2 +- src/Operation/Traits/OptionsTrait.php | 18 ++- src/State.php | 38 +++--- tests/Migrations/AtomizerTest.php | 4 +- 21 files changed, 250 insertions(+), 182 deletions(-) delete mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index 349aefc..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,14 +0,0 @@ -CHANGELOG for 0.9.0 RC -====================== - -2.0.0 (29.09.2018) ------ -- simplier interfaces -- migrations are now lock on one database at time -- new dbal component -- new core component -- more tests - -0.9.0 (03.02.2017) ------ -* DBAL, Pagination and Migration component split from component repository diff --git a/src/Atomizer/Atomizer.php b/src/Atomizer/Atomizer.php index d4a5027..72cf7fe 100644 --- a/src/Atomizer/Atomizer.php +++ b/src/Atomizer/Atomizer.php @@ -132,7 +132,7 @@ protected function sortedTables($reverse = false): array */ private function declareBlock(Source $source): void { - if (!empty($source->getLines())) { + if ($source->getLines() !== []) { $source->addLine(''); } } diff --git a/src/Atomizer/Renderer.php b/src/Atomizer/Renderer.php index 48ad0d2..a65b4ff 100644 --- a/src/Atomizer/Renderer.php +++ b/src/Atomizer/Renderer.php @@ -35,7 +35,11 @@ final class Renderer implements RendererInterface */ public function createTable(Source $source, AbstractTable $table): void { - $this->render($source, '$this->table(%s)', $table); + $this->render( + $source, + '$this->table(%s)', + $table + ); $comparator = $table->getComparator(); $this->declareColumns($source, $comparator); @@ -43,7 +47,11 @@ public function createTable(Source $source, AbstractTable $table): void $this->declareForeignKeys($source, $comparator, $table->getPrefix()); if (count($table->getPrimaryKeys())) { - $this->render($source, ' ->setPrimaryKeys(%s)', $table->getPrimaryKeys()); + $this->render( + $source, + ' ->setPrimaryKeys(%s)', + $table->getPrimaryKeys() + ); } //Finalization @@ -55,11 +63,19 @@ public function createTable(Source $source, AbstractTable $table): void */ public function updateTable(Source $source, AbstractTable $table): void { - $this->render($source, '$this->table(%s)', $table); + $this->render( + $source, + '$this->table(%s)', + $table + ); $comparator = $table->getComparator(); if ($comparator->isPrimaryChanged()) { - $this->render($source, ' ->setPrimaryKeys(%s)', $table->getPrimaryKeys()); + $this->render( + $source, + ' ->setPrimaryKeys(%s)', + $table->getPrimaryKeys() + ); } $this->declareColumns($source, $comparator); @@ -76,10 +92,14 @@ public function updateTable(Source $source, AbstractTable $table): void public function revertTable(Source $source, AbstractTable $table): void { //Get table blueprint - $this->render($source, '$this->table(%s)', $table); + $this->render( + $source, + '$this->table(%s)', + $table + ); $comparator = $table->getComparator(); - $this->revertForeigns($source, $comparator, $table->getPrefix()); + $this->revertForeignKeys($source, $comparator, $table->getPrefix()); $this->revertIndexes($source, $comparator); $this->revertColumns($source, $comparator); @@ -92,14 +112,18 @@ public function revertTable(Source $source, AbstractTable $table): void */ public function dropTable(Source $source, AbstractTable $table): void { - $this->render($source, '$this->table(%s)->drop();', $table); + $this->render( + $source, + '$this->table(%s)->drop();', + $table + ); } /** * @param Source $source * @param Comparator $comparator */ - protected function declareColumns(Source $source, Comparator $comparator): void + private function declareColumns(Source $source, Comparator $comparator): void { foreach ($comparator->addedColumns() as $column) { $this->render( @@ -112,11 +136,19 @@ protected function declareColumns(Source $source, Comparator $comparator): void } foreach ($comparator->alteredColumns() as $pair) { - $this->alterColumn($source, $pair[self::NEW_STATE], $pair[self::ORIGINAL_STATE]); + $this->alterColumn( + $source, + $pair[self::NEW_STATE], + $pair[self::ORIGINAL_STATE] + ); } foreach ($comparator->droppedColumns() as $column) { - $this->render($source, ' ->dropColumn(%s)', $column->getName()); + $this->render( + $source, + ' ->dropColumn(%s)', + $column->getName() + ); } } @@ -124,20 +156,34 @@ protected function declareColumns(Source $source, Comparator $comparator): void * @param Source $source * @param Comparator $comparator */ - protected function declareIndexes(Source $source, Comparator $comparator): void + private function declareIndexes(Source $source, Comparator $comparator): void { foreach ($comparator->addedIndexes() as $index) { - $this->render($source, ' ->addIndex(%s, %s)', $index->getColumns(), $index); + $this->render( + $source, + ' ->addIndex(%s, %s)', + $index->getColumns(), + $index + ); } foreach ($comparator->alteredIndexes() as $pair) { /** @var AbstractIndex $index */ $index = $pair[self::NEW_STATE]; - $this->render($source, ' ->alterIndex(%s, %s)', $index->getColumns(), $index); + $this->render( + $source, + ' ->alterIndex(%s, %s)', + $index->getColumns(), + $index + ); } foreach ($comparator->droppedIndexes() as $index) { - $this->render($source, ' ->dropIndex(%s)', $index->getColumns()); + $this->render( + $source, + ' ->dropIndex(%s)', + $index->getColumns() + ); } } @@ -146,11 +192,8 @@ protected function declareIndexes(Source $source, Comparator $comparator): void * @param Comparator $comparator * @param string $prefix Database isolation prefix */ - protected function declareForeignKeys( - Source $source, - Comparator $comparator, - string $prefix = '' - ): void { + private function declareForeignKeys(Source $source, Comparator $comparator, string $prefix = ''): void + { foreach ($comparator->addedForeignKeys() as $key) { $this->render( $source, @@ -176,7 +219,11 @@ protected function declareForeignKeys( } foreach ($comparator->droppedForeignKeys() as $key) { - $this->render($source, ' ->dropForeignKey(%s)', $key->getColumns()); + $this->render( + $source, + ' ->dropForeignKey(%s)', + $key->getColumns() + ); } } @@ -184,7 +231,7 @@ protected function declareForeignKeys( * @param Source $source * @param Comparator $comparator */ - protected function revertColumns(Source $source, Comparator $comparator): void + private function revertColumns(Source $source, Comparator $comparator): void { foreach ($comparator->droppedColumns() as $column) { $this->render( @@ -197,11 +244,19 @@ protected function revertColumns(Source $source, Comparator $comparator): void } foreach ($comparator->alteredColumns() as $pair) { - $this->alterColumn($source, $pair[self::ORIGINAL_STATE], $pair[self::NEW_STATE]); + $this->alterColumn( + $source, + $pair[self::ORIGINAL_STATE], + $pair[self::NEW_STATE] + ); } foreach ($comparator->addedColumns() as $column) { - $this->render($source, ' ->dropColumn(%s)', $column->getName()); + $this->render( + $source, + ' ->dropColumn(%s)', + $column->getName() + ); } } @@ -209,20 +264,34 @@ protected function revertColumns(Source $source, Comparator $comparator): void * @param Source $source * @param Comparator $comparator */ - protected function revertIndexes(Source $source, Comparator $comparator): void + private function revertIndexes(Source $source, Comparator $comparator): void { foreach ($comparator->droppedIndexes() as $index) { - $this->render($source, ' ->addIndex(%s, %s)', $index->getColumns(), $index); + $this->render( + $source, + ' ->addIndex(%s, %s)', + $index->getColumns(), + $index + ); } foreach ($comparator->alteredIndexes() as $pair) { /** @var AbstractIndex $index */ $index = $pair[self::ORIGINAL_STATE]; - $this->render($source, ' ->alterIndex(%s, %s)', $index->getColumns(), $index); + $this->render( + $source, + ' ->alterIndex(%s, %s)', + $index->getColumns(), + $index + ); } foreach ($comparator->addedIndexes() as $index) { - $this->render($source, ' ->dropIndex(%s)', $index->getColumns()); + $this->render( + $source, + ' ->dropIndex(%s)', + $index->getColumns() + ); } } @@ -231,11 +300,8 @@ protected function revertIndexes(Source $source, Comparator $comparator): void * @param Comparator $comparator * @param string $prefix Database isolation prefix. */ - protected function revertForeigns( - Source $source, - Comparator $comparator, - string $prefix = '' - ): void { + private function revertForeignKeys(Source $source, Comparator $comparator, string $prefix = ''): void + { foreach ($comparator->droppedForeignKeys() as $key) { $this->render( $source, @@ -275,7 +341,7 @@ protected function alterColumn( AbstractColumn $column, AbstractColumn $original ): void { - if ($column->getName() != $original->getName()) { + if ($column->getName() !== $original->getName()) { $name = $original->getName(); } else { $name = $column->getName(); @@ -289,8 +355,13 @@ protected function alterColumn( $column ); - if ($column->getName() != $original->getName()) { - $this->render($source, ' ->renameColumn(%s, %s)', $name, $column->getName()); + if ($column->getName() !== $original->getName()) { + $this->render( + $source, + ' ->renameColumn(%s, %s)', + $name, + $column->getName() + ); } } @@ -331,7 +402,7 @@ protected function render(Source $source, string $format, ...$values): void // numeric array if (is_array($value) && count($value) > 0 && is_numeric(array_keys($value)[0])) { - $rendered[] = '["' . join('", "', $value) . '"]'; + $rendered[] = '["' . implode('", "', $value) . '"]'; continue; } @@ -356,15 +427,15 @@ private function columnOptions(Serializer $serializer, AbstractColumn $column): 'default' => $column->getDefaultValue() ]; - if ($column->getAbstractType() == 'enum') { + if ($column->getAbstractType() === 'enum') { $options['values'] = $column->getEnumValues(); } - if ($column->getAbstractType() == 'string') { + if ($column->getAbstractType() === 'string') { $options['size'] = $column->getSize(); } - if ($column->getAbstractType() == 'decimal') { + if ($column->getAbstractType() === 'decimal') { $options['scale'] = $column->getScale(); $options['precision'] = $column->getPrecision(); } @@ -379,10 +450,14 @@ private function columnOptions(Serializer $serializer, AbstractColumn $column): */ private function indexOptions(Serializer $serializer, AbstractIndex $index): string { - return $this->mountIndents($serializer->serialize([ - 'name' => $index->getName(), - 'unique' => $index->isUnique() - ])); + return $this->mountIndents( + $serializer->serialize( + [ + 'name' => $index->getName(), + 'unique' => $index->isUnique() + ] + ) + ); } /** @@ -394,11 +469,15 @@ private function foreignKeyOptions( Serializer $serializer, AbstractForeignKey $reference ): string { - return $this->mountIndents($serializer->serialize([ - 'name' => $reference->getName(), - 'delete' => $reference->getDeleteRule(), - 'update' => $reference->getUpdateRule() - ])); + return $this->mountIndents( + $serializer->serialize( + [ + 'name' => $reference->getName(), + 'delete' => $reference->getDeleteRule(), + 'update' => $reference->getUpdateRule() + ] + ) + ); } /** @@ -415,6 +494,6 @@ private function mountIndents(string $serialized): string unset($line); } - return ltrim(join("\n", $lines)); + return ltrim(implode("\n", $lines)); } } diff --git a/src/Capsule.php b/src/Capsule.php index c063fcb..ad41ea1 100644 --- a/src/Capsule.php +++ b/src/Capsule.php @@ -75,10 +75,12 @@ public function execute(array $operations): void { foreach ($operations as $operation) { if (!$operation instanceof OperationInterface) { - throw new CapsuleException(sprintf( - 'Migration operation expected to be an instance of `OperationInterface`, `%s` given', - get_class($operation) - )); + throw new CapsuleException( + sprintf( + 'Migration operation expected to be an instance of `OperationInterface`, `%s` given', + get_class($operation) + ) + ); } $operation->execute($this); diff --git a/src/Config/MigrationConfig.php b/src/Config/MigrationConfig.php index 0cdb883..2e0151d 100644 --- a/src/Config/MigrationConfig.php +++ b/src/Config/MigrationConfig.php @@ -17,16 +17,6 @@ final class MigrationConfig extends InjectableConfig { public const CONFIG = 'migration'; - /** - * @var array - */ - protected $config = [ - 'directory' => '', - 'table' => 'migrations', - 'safe' => false, - 'namespace' => 'Migration', - ]; - /** * Migrations directory. * diff --git a/src/Exception/BlueprintException.php b/src/Exception/BlueprintException.php index a6739bc..490884d 100644 --- a/src/Exception/BlueprintException.php +++ b/src/Exception/BlueprintException.php @@ -11,9 +11,6 @@ namespace Spiral\Migrations\Exception; -/** - * Invalid blueprint configuration. - */ class BlueprintException extends MigrationException { } diff --git a/src/Exception/RepositoryException.php b/src/Exception/RepositoryException.php index bdeed4b..754968e 100644 --- a/src/Exception/RepositoryException.php +++ b/src/Exception/RepositoryException.php @@ -11,9 +11,6 @@ namespace Spiral\Migrations\Exception; -/** - * Repository related exceptions. - */ class RepositoryException extends MigrationException { } diff --git a/src/FileRepository.php b/src/FileRepository.php index c4d47db..cb612de 100644 --- a/src/FileRepository.php +++ b/src/FileRepository.php @@ -19,7 +19,6 @@ use Spiral\Migrations\Config\MigrationConfig; use Spiral\Migrations\Exception\RepositoryException; use Spiral\Tokenizer\Reflection\ReflectionFile; -use Spiral\Tokenizer\TokenizerInterface; /** * Stores migrations as files. @@ -33,13 +32,9 @@ final class FileRepository implements RepositoryInterface private const TIMESTAMP_FORMAT = 'Ymd.His'; /** @var MigrationConfig */ - private $config = null; + private $config; - /** - * Required when multiple migrations added at once. - * - * @var int - */ + /** @var int */ private $chunkID = 0; /** @var FactoryInterface */ @@ -99,13 +94,13 @@ public function registerMigration(string $name, string $class, string $body = nu $inflectedName = Inflector::tableize($name); foreach ($this->getMigrations() as $migration) { - if (get_class($migration) == $class) { + if (get_class($migration) === $class) { throw new RepositoryException( "Unable to register migration '{$class}', migration already exists" ); } - if ($migration->getState()->getName() == $inflectedName) { + if ($migration->getState()->getName() === $inflectedName) { throw new RepositoryException( "Unable to register migration '{$inflectedName}', migration under the same name already exists" ); diff --git a/src/Migration.php b/src/Migration.php index 88b066d..7830cb8 100644 --- a/src/Migration.php +++ b/src/Migration.php @@ -24,10 +24,10 @@ abstract class Migration implements MigrationInterface protected const DATABASE = null; /** @var State|null */ - private $state = null; + private $state; /** @var CapsuleInterface */ - private $capsule = null; + private $capsule; /** * {@inheritdoc} diff --git a/src/Migrator.php b/src/Migrator.php index f0bd65e..25dff5b 100644 --- a/src/Migrator.php +++ b/src/Migrator.php @@ -119,19 +119,23 @@ public function run(CapsuleInterface $capsule = null): ?MigrationInterface } foreach ($this->getMigrations() as $migration) { - if ($migration->getState()->getStatus() != State::STATUS_PENDING) { + if ($migration->getState()->getStatus() !== State::STATUS_PENDING) { continue; } $capsule = $capsule ?? new Capsule($this->dbal->database($migration->getDatabase())); - $capsule->getDatabase($migration->getDatabase())->transaction(function () use ($migration, $capsule): void { - $migration->withCapsule($capsule)->up(); - }); - - $this->migrationTable($migration->getDatabase())->insertOne([ - 'migration' => $migration->getState()->getName(), - 'time_executed' => new \DateTime('now') - ]); + $capsule->getDatabase($migration->getDatabase())->transaction( + static function () use ($migration, $capsule): void { + $migration->withCapsule($capsule)->up(); + } + ); + + $this->migrationTable($migration->getDatabase())->insertOne( + [ + 'migration' => $migration->getState()->getName(), + 'time_executed' => new \DateTime('now') + ] + ); return $migration->withState($this->resolveState($migration)); } @@ -155,18 +159,22 @@ public function rollback(CapsuleInterface $capsule = null): ?MigrationInterface /** @var MigrationInterface $migration */ foreach (array_reverse($this->getMigrations()) as $migration) { - if ($migration->getState()->getStatus() != State::STATUS_EXECUTED) { + if ($migration->getState()->getStatus() !== State::STATUS_EXECUTED) { continue; } $capsule = $capsule ?? new Capsule($this->dbal->database($migration->getDatabase())); - $capsule->getDatabase()->transaction(function () use ($migration, $capsule): void { - $migration->withCapsule($capsule)->down(); - }); - - $this->migrationTable($migration->getDatabase())->delete([ - 'migration' => $migration->getState()->getName() - ])->run(); + $capsule->getDatabase()->transaction( + static function () use ($migration, $capsule): void { + $migration->withCapsule($capsule)->down(); + } + ); + + $this->migrationTable($migration->getDatabase())->delete( + [ + 'migration' => $migration->getState()->getName() + ] + )->run(); return $migration->withState($this->resolveState($migration)); } @@ -186,9 +194,9 @@ protected function resolveState(MigrationInterface $migration): State //Fetch migration information from database $data = $this->migrationTable($migration->getDatabase()) - ->select('id', 'time_executed') - ->where(['migration' => $migration->getState()->getName()]) - ->run()->fetch(); + ->select('id', 'time_executed') + ->where(['migration' => $migration->getState()->getName()]) + ->run()->fetch(); if (empty($data['time_executed'])) { return $migration->getState()->withStatus(State::STATUS_PENDING); @@ -196,7 +204,7 @@ protected function resolveState(MigrationInterface $migration): State return $migration->getState()->withStatus( State::STATUS_EXECUTED, - new \DateTime($data['time_executed'], $db->getDriver()->getTimezone()) + new \DateTimeImmutable($data['time_executed'], $db->getDriver()->getTimezone()) ); } diff --git a/src/Operation/Column/Column.php b/src/Operation/Column/Column.php index 465e4fd..8fd86e5 100644 --- a/src/Operation/Column/Column.php +++ b/src/Operation/Column/Column.php @@ -33,10 +33,10 @@ abstract class Column extends AbstractOperation ]; /** @var string */ - protected $name = ''; + protected $name; /** @var string */ - protected $type = ''; + protected $type; /** * @param string $table diff --git a/src/Operation/Column/Drop.php b/src/Operation/Column/Drop.php index 68bb3a8..003dc09 100644 --- a/src/Operation/Column/Drop.php +++ b/src/Operation/Column/Drop.php @@ -22,7 +22,7 @@ final class Drop extends AbstractOperation * * @var string */ - private $name = ''; + private $name; /** * @param string $table diff --git a/src/Operation/Column/Rename.php b/src/Operation/Column/Rename.php index 5ff8f11..f6cd568 100644 --- a/src/Operation/Column/Rename.php +++ b/src/Operation/Column/Rename.php @@ -49,12 +49,14 @@ public function execute(CapsuleInterface $capsule): void } if ($schema->hasColumn($this->newName)) { - throw new ColumnException(sprintf( - "Unable to rename column '%s'.'%s', column '%s' already exists", - $schema->getName(), - $this->name, - $this->newName - )); + throw new ColumnException( + sprintf( + "Unable to rename column '%s'.'%s', column '%s' already exists", + $schema->getName(), + $this->name, + $this->newName + ) + ); } //Declaring column diff --git a/src/Operation/ForeignKey/Add.php b/src/Operation/ForeignKey/Add.php index 269ad36..d64ce3f 100644 --- a/src/Operation/ForeignKey/Add.php +++ b/src/Operation/ForeignKey/Add.php @@ -64,7 +64,7 @@ public function execute(CapsuleInterface $capsule): void $foreignSchema = $capsule->getSchema($this->foreignTable); - if ($this->foreignTable != $this->table && !$foreignSchema->exists()) { + if ($this->foreignTable !== $this->table && !$foreignSchema->exists()) { throw new ForeignKeyException( "Unable to add foreign key '{$schema->getName()}'.'{$this->columnNames()}', " . "foreign table '{$this->foreignTable}' does not exists" @@ -72,7 +72,7 @@ public function execute(CapsuleInterface $capsule): void } foreach ($this->foreignKeys as $fk) { - if ($this->foreignTable != $this->table && !$foreignSchema->hasColumn($fk)) { + if ($this->foreignTable !== $this->table && !$foreignSchema->hasColumn($fk)) { throw new ForeignKeyException( "Unable to add foreign key '{$schema->getName()}'.'{$this->columnNames()}'," . " foreign column '{$this->foreignTable}'.'{$fk}' does not exists" @@ -93,16 +93,20 @@ public function execute(CapsuleInterface $capsule): void * We are allowing both formats "NO_ACTION" and "NO ACTION". */ - $foreignKey->onDelete(str_replace( - '_', - ' ', - $this->getOption('delete', ForeignKeyInterface::NO_ACTION) - )); - - $foreignKey->onUpdate(str_replace( - '_', - ' ', - $this->getOption('update', ForeignKeyInterface::NO_ACTION) - )); + $foreignKey->onDelete( + str_replace( + '_', + ' ', + $this->getOption('delete', ForeignKeyInterface::NO_ACTION) + ) + ); + + $foreignKey->onUpdate( + str_replace( + '_', + ' ', + $this->getOption('update', ForeignKeyInterface::NO_ACTION) + ) + ); } } diff --git a/src/Operation/ForeignKey/Alter.php b/src/Operation/ForeignKey/Alter.php index 8dd7c1c..c6fb99d 100644 --- a/src/Operation/ForeignKey/Alter.php +++ b/src/Operation/ForeignKey/Alter.php @@ -21,10 +21,10 @@ final class Alter extends ForeignKey use OptionsTrait; /** @var string */ - protected $foreignTable = ''; + protected $foreignTable; /** @var array */ - protected $foreignKeys = []; + protected $foreignKeys; /** * @param string $table @@ -88,16 +88,20 @@ public function execute(CapsuleInterface $capsule): void * We are allowing both formats "NO_ACTION" and "NO ACTION". */ - $foreignKey->onDelete(str_replace( - '_', - ' ', - $this->getOption('delete', ForeignKeyInterface::NO_ACTION) - )); - - $foreignKey->onUpdate(str_replace( - '_', - ' ', - $this->getOption('update', ForeignKeyInterface::NO_ACTION) - )); + $foreignKey->onDelete( + str_replace( + '_', + ' ', + $this->getOption('delete', ForeignKeyInterface::NO_ACTION) + ) + ); + + $foreignKey->onUpdate( + str_replace( + '_', + ' ', + $this->getOption('update', ForeignKeyInterface::NO_ACTION) + ) + ); } } diff --git a/src/Operation/Index/Add.php b/src/Operation/Index/Add.php index 53051d8..9ba1ed5 100644 --- a/src/Operation/Index/Add.php +++ b/src/Operation/Index/Add.php @@ -38,7 +38,7 @@ public function execute(CapsuleInterface $capsule): void $schema = $capsule->getSchema($this->getTable()); if ($schema->hasIndex($this->columns)) { - $columns = join(',', $this->columns); + $columns = implode(',', $this->columns); throw new IndexException( "Unable to create index '{$schema->getName()}'.({$columns}), index already exists" ); diff --git a/src/Operation/Index/Alter.php b/src/Operation/Index/Alter.php index d78cdb7..427b256 100644 --- a/src/Operation/Index/Alter.php +++ b/src/Operation/Index/Alter.php @@ -38,7 +38,7 @@ public function execute(CapsuleInterface $capsule): void $schema = $capsule->getSchema($this->getTable()); if (!$schema->hasIndex($this->columns)) { - $columns = join(',', $this->columns); + $columns = implode(',', $this->columns); throw new IndexException( "Unable to alter index '{$schema->getName()}'.({$columns}), no such index" ); diff --git a/src/Operation/Index/Drop.php b/src/Operation/Index/Drop.php index 36f731b..3d3d8bb 100644 --- a/src/Operation/Index/Drop.php +++ b/src/Operation/Index/Drop.php @@ -24,7 +24,7 @@ public function execute(CapsuleInterface $capsule): void $schema = $capsule->getSchema($this->getTable()); if (!$schema->hasIndex($this->columns)) { - $columns = join(',', $this->columns); + $columns = implode(',', $this->columns); throw new IndexException( "Unable to drop index '{$schema->getName()}'.({$columns}), index does not exists" ); diff --git a/src/Operation/Traits/OptionsTrait.php b/src/Operation/Traits/OptionsTrait.php index e6d0089..5c82627 100644 --- a/src/Operation/Traits/OptionsTrait.php +++ b/src/Operation/Traits/OptionsTrait.php @@ -30,12 +30,14 @@ protected function hasOption(string $name): bool return true; } - if (!isset($this->aliases) || !isset($this->aliases[$name])) { + if (!isset($this->aliases[$name])) { return false; } - foreach ($this->aliases[$name] as $name) { - return $this->hasOption($name); + foreach ($this->aliases as $source => $aliases) { + if (in_array($name, $aliases, true)) { + return true; + } } return false; @@ -57,12 +59,14 @@ protected function getOption(string $name, $default = null) return $this->options[$name]; } - if (!isset($this->aliases) || !isset($this->aliases[$name])) { - return false; + if (!isset($this->aliases[$name])) { + return $default; } - foreach ($this->aliases[$name] as $name) { - return $this->hasOption($name); + foreach ($this->aliases as $source => $aliases) { + if (in_array($name, $aliases, true)) { + return $this->getOption($source); + } } return false; diff --git a/src/State.php b/src/State.php index 693f263..2363c64 100644 --- a/src/State.php +++ b/src/State.php @@ -22,28 +22,28 @@ final class State public const STATUS_EXECUTED = 1; /** @var string */ - private $name = ''; + private $name; /** @var int */ - private $status = self::STATUS_PENDING; + private $status; - /** @var \DateTime */ - private $timeCreated = null; + /** @var \DateTimeInterface */ + private $timeCreated; - /** @var \DateTime|null */ - private $timeExecuted = null; + /** @var \DateTimeInterface|null */ + private $timeExecuted; /** - * @param string $name - * @param \DateTime $timeCreated - * @param int $status - * @param \DateTime $timeExecuted + * @param string $name + * @param \DateTimeInterface $timeCreated + * @param int $status + * @param \DateTimeInterface $timeExecuted */ public function __construct( string $name, - \DateTime $timeCreated, + \DateTimeInterface $timeCreated, int $status = self::STATUS_UNDEFINED, - \DateTime $timeExecuted = null + \DateTimeInterface $timeExecuted = null ) { $this->name = $name; $this->status = $status; @@ -72,9 +72,9 @@ public function getStatus(): int /** * Get migration creation time. * - * @return \DateTime + * @return \DateTimeInterface */ - public function getTimeCreated(): \DateTime + public function getTimeCreated(): \DateTimeInterface { return $this->timeCreated; } @@ -82,20 +82,20 @@ public function getTimeCreated(): \DateTime /** * Get migration execution time if any. * - * @return \DateTime|null + * @return \DateTimeInterface|null */ - public function getTimeExecuted() + public function getTimeExecuted(): ?\DateTimeInterface { return $this->timeExecuted; } /** - * @param int $status - * @param \DateTime|null $timeExecuted + * @param int $status + * @param \DateTimeInterface|null $timeExecuted * * @return State */ - public function withStatus(int $status, \DateTime $timeExecuted = null): State + public function withStatus(int $status, \DateTimeInterface $timeExecuted = null): State { $state = clone $this; $state->status = $status; diff --git a/tests/Migrations/AtomizerTest.php b/tests/Migrations/AtomizerTest.php index 6345aa6..14e1b65 100644 --- a/tests/Migrations/AtomizerTest.php +++ b/tests/Migrations/AtomizerTest.php @@ -31,8 +31,8 @@ public function testCreateAndDiff(): void $this->assertInstanceOf(Migration::class, $migration); $this->assertSame(State::STATUS_EXECUTED, $migration->getState()->getStatus()); - $this->assertInstanceOf(\DateTime::class, $migration->getState()->getTimeCreated()); - $this->assertInstanceOf(\DateTime::class, $migration->getState()->getTimeExecuted()); + $this->assertInstanceOf(\DateTimeInterface::class, $migration->getState()->getTimeCreated()); + $this->assertInstanceOf(\DateTimeInterface::class, $migration->getState()->getTimeExecuted()); $this->assertTrue($this->db->hasTable('sample'));