diff --git a/src/Migrations.php b/src/Migrations.php index 157bfe0..3944161 100644 --- a/src/Migrations.php +++ b/src/Migrations.php @@ -338,7 +338,7 @@ public static function run(array $options) continue; } - //Directory depends on Forward or Back Migration + // Directory depends on Forward or Back Migration if (ModelMigration::DIRECTION_BACK === $direction) { $migrationsDir = $initialVersion->getPath(); $directoryIterator = $migrationsDir . DIRECTORY_SEPARATOR . $initialVersion->getVersion(); @@ -566,7 +566,7 @@ private static function connectionSetup(array $options): void * @param array $options Applications options * @return IncrementalItem|TimestampedItem */ - public static function getCurrentVersion($options) + public static function getCurrentVersion(array $options) { self::connectionSetup($options); @@ -603,10 +603,9 @@ public static function getCurrentVersion($options) * * @param array $options Applications options * @param string $version Migration version to store - * @param string $startTime Migration start timestamp - * @throws DbException + * @param string|null $startTime Migration start timestamp */ - public static function addCurrentVersion(array $options, string $version, string $startTime = null): void + public static function addCurrentVersion(array $options, string $version, ?string $startTime = null): void { self::connectionSetup($options); @@ -638,7 +637,6 @@ public static function addCurrentVersion(array $options, string $version, string * * @param array $options Applications options * @param string $version Migration version to remove - * @throws DbException */ public static function removeCurrentVersion(array $options, string $version): void { diff --git a/src/Mvc/Model/Migration.php b/src/Mvc/Model/Migration.php index ee4ce1c..ea7ca00 100644 --- a/src/Mvc/Model/Migration.php +++ b/src/Mvc/Model/Migration.php @@ -34,6 +34,9 @@ use Phalcon\Migrations\Version\ItemCollection as VersionCollection; use Phalcon\Migrations\Version\ItemInterface; use Phalcon\Text; +use Throwable; + +use function get_called_class; /** * Migrations of DML y DDL over databases @@ -538,13 +541,13 @@ public function morphTable(string $tableName, array $definition): void if (!isset($localFields[$fieldName])) { try { self::$connection->addColumn($tableName, $tableSchema, $column); - } catch (\Throwable $exception) { + } catch (Throwable $exception) { throw new RuntimeException( sprintf( "Failed to add column '%s' in table '%s'. In '%s' migration. DB error: %s", $column->getName(), $tableName, - \get_called_class(), + get_called_class(), $exception->getMessage() ) ); @@ -577,13 +580,13 @@ public function morphTable(string $tableName, array $definition): void if ($changed === true) { try { self::$connection->modifyColumn($tableName, $tableSchema, $column, $column); - } catch (\Throwable $exception) { + } catch (Throwable $exception) { throw new RuntimeException( sprintf( "Failed to modify column '%s' in table '%s'. In '%s' migration. DB error: %s", $column->getName(), $tableName, - \get_called_class(), + get_called_class(), $exception->getMessage() ) ); @@ -598,13 +601,13 @@ public function morphTable(string $tableName, array $definition): void * TODO: Check, why schemaName is empty string. */ self::$connection->dropColumn($tableName, '', $fieldName); - } catch (\Throwable $exception) { + } catch (Throwable $exception) { throw new RuntimeException( sprintf( "Failed to drop column '%s' in table '%s'. In '%s' migration. DB error: %s", $fieldName, $tableName, - \get_called_class(), + get_called_class(), $exception->getMessage() ) ); @@ -614,12 +617,12 @@ public function morphTable(string $tableName, array $definition): void } else { try { self::$connection->createTable($tableName, $tableSchema, $definition); - } catch (\Throwable $exception) { + } catch (Throwable $exception) { throw new RuntimeException( sprintf( "Failed to create table '%s'. In '%s' migration. DB error: %s", $tableName, - \get_called_class(), + get_called_class(), $exception->getMessage() ) ); @@ -659,13 +662,13 @@ public function morphTable(string $tableName, array $definition): void $schemaName, $tableReference ); - } catch (\Throwable $exception) { + } catch (Throwable $exception) { throw new RuntimeException( sprintf( "Failed to add foreign key '%s' in '%s'. In '%s' migration. DB error: %s", $tableReference->getName(), $tableName, - \get_called_class(), + get_called_class(), $exception->getMessage() ) ); @@ -725,13 +728,13 @@ public function morphTable(string $tableName, array $definition): void $schemaName, $tableReference->getName() ); - } catch (\Throwable $exception) { + } catch (Throwable $exception) { throw new RuntimeException( sprintf( "Failed to drop foreign key '%s' in '%s'. In '%s' migration. DB error: %s", $tableReference->getName(), $tableName, - \get_called_class(), + get_called_class(), $exception->getMessage() ) ); @@ -743,13 +746,13 @@ public function morphTable(string $tableName, array $definition): void $schemaName, $tableReference ); - } catch (\Throwable $exception) { + } catch (Throwable $exception) { throw new RuntimeException( sprintf( "Failed to add foreign key '%s' in '%s'. In '%s' migration. DB error: %s", $tableReference->getName(), $tableName, - \get_called_class(), + get_called_class(), $exception->getMessage() ) ); @@ -764,13 +767,13 @@ public function morphTable(string $tableName, array $definition): void * TODO: Check, why schemaName is empty string. */ self::$connection->dropForeignKey($tableName, '', $referenceName); - } catch (\Throwable $exception) { + } catch (Throwable $exception) { throw new RuntimeException( sprintf( "Failed to drop foreign key '%s' in '%s'. In '%s' migration. DB error: %s", $referenceName, $tableName, - \get_called_class(), + get_called_class(), $exception->getMessage() ) ); @@ -996,13 +999,13 @@ private function addPrimaryKey(string $tableName, string $schemaName, IndexInter { try { self::$connection->addPrimaryKey($tableName, $schemaName, $index); - } catch (\Throwable $exception) { + } catch (Throwable $exception) { throw new RuntimeException( sprintf( "Failed to add primary key '%s' in '%s'. In '%s' migration. DB error: %s", $index->getName(), $tableName, - \get_called_class(), + get_called_class(), $exception->getMessage() ) ); @@ -1018,12 +1021,12 @@ private function dropPrimaryKey(string $tableName, string $schemaName): void { try { self::$connection->dropPrimaryKey($tableName, $schemaName); - } catch (\Throwable $exception) { + } catch (Throwable $exception) { throw new RuntimeException( sprintf( "Failed to drop primary key in '%s'. In '%s' migration. DB error: %s", $tableName, - \get_called_class(), + get_called_class(), $exception->getMessage() ) ); @@ -1040,13 +1043,13 @@ private function addIndex(string $tableName, string $schemaName, IndexInterface { try { self::$connection->addIndex($tableName, $schemaName, $indexName); - } catch (\Throwable $exception) { + } catch (Throwable $exception) { throw new RuntimeException( sprintf( "Failed to add index '%s' in '%s'. In '%s' migration. DB error: %s", $indexName->getName(), $tableName, - \get_called_class(), + get_called_class(), $exception->getMessage() ) ); @@ -1063,13 +1066,13 @@ private function dropIndex(string $tableName, string $schemaName, string $indexN { try { self::$connection->dropIndex($tableName, $schemaName, $indexName); - } catch (\Throwable $exception) { + } catch (Throwable $exception) { throw new RuntimeException( sprintf( "Failed to drop index '%s' in '%s'. In '%s' migration. DB error: %s", $indexName, $tableName, - \get_called_class(), + get_called_class(), $exception->getMessage() ) ); diff --git a/src/Mvc/Model/Migration/TableAware/ListTablesDb.php b/src/Mvc/Model/Migration/TableAware/ListTablesDb.php index 703a309..9fb1e16 100644 --- a/src/Mvc/Model/Migration/TableAware/ListTablesDb.php +++ b/src/Mvc/Model/Migration/TableAware/ListTablesDb.php @@ -24,7 +24,7 @@ class ListTablesDb implements ListTablesInterface * Get table names with prefix for running migration * * @param string $tablePrefix - * @param DirectoryIterator $iterator + * @param DirectoryIterator|null $iterator * @return string * @throws DbException */ @@ -34,17 +34,14 @@ public function listTablesForPrefix(string $tablePrefix, DirectoryIterator $iter throw new InvalidArgumentException("Parameters weren't defined in " . __METHOD__); } - $modelMigration = new ModelMigration(); - $connection = $modelMigration->getConnection(); - - $tablesList = $connection->listTables(); + $tablesList = (new ModelMigration())->getConnection()->listTables(); if (empty($tablesList)) { return ''; } - $strlen = strlen($tablePrefix); + $length = strlen($tablePrefix); foreach ($tablesList as $key => $value) { - if (substr($value, 0, $strlen) != $tablePrefix) { + if (substr($value, 0, $length) !== $tablePrefix) { unset($tablesList[$key]); } } diff --git a/src/Mvc/Model/Migration/TableAware/ListTablesInterface.php b/src/Mvc/Model/Migration/TableAware/ListTablesInterface.php index 0399cfb..1cbf180 100644 --- a/src/Mvc/Model/Migration/TableAware/ListTablesInterface.php +++ b/src/Mvc/Model/Migration/TableAware/ListTablesInterface.php @@ -21,7 +21,7 @@ interface ListTablesInterface * Get list table from prefix * * @param string $tablePrefix Table prefix - * @param DirectoryIterator $iterator + * @param DirectoryIterator|null $iterator * @return string */ public function listTablesForPrefix(string $tablePrefix, DirectoryIterator $iterator = null): string; diff --git a/src/Mvc/Model/Migration/TableAware/ListTablesIterator.php b/src/Mvc/Model/Migration/TableAware/ListTablesIterator.php index ad41d8d..3bda2cd 100644 --- a/src/Mvc/Model/Migration/TableAware/ListTablesIterator.php +++ b/src/Mvc/Model/Migration/TableAware/ListTablesIterator.php @@ -22,26 +22,24 @@ class ListTablesIterator implements ListTablesInterface * Get table names with prefix for running migration * * @param string $tablePrefix - * @param DirectoryIterator $iterator + * @param DirectoryIterator|null $iterator * @return string */ public function listTablesForPrefix(string $tablePrefix, DirectoryIterator $iterator = null): string { - if (empty($tablePrefix) || empty($iterator)) { + if (empty($tablePrefix) || $iterator === null) { throw new InvalidArgumentException("Parameters weren't defined in " . __METHOD__); } - $strlen = strlen($tablePrefix); $fileNames = []; + $length = strlen($tablePrefix); foreach ($iterator as $fileInfo) { - if (substr($fileInfo->getFilename(), 0, $strlen) == $tablePrefix) { + if (substr($fileInfo->getFilename(), 0, $length) === $tablePrefix) { $file = explode('.', $fileInfo->getFilename()); $fileNames[] = $file[0]; } } - $fileNames = array_unique($fileNames); - - return implode(',', $fileNames); + return implode(',', array_unique($fileNames)); } }