Skip to content

Commit

Permalink
Code reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeckerson committed Mar 22, 2021
1 parent 15c3d63 commit 5a2b3aa
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 45 deletions.
10 changes: 4 additions & 6 deletions src/Migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
{
Expand Down
51 changes: 27 additions & 24 deletions src/Mvc/Model/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
)
);
Expand Down Expand Up @@ -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()
)
);
Expand All @@ -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()
)
);
Expand All @@ -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()
)
);
Expand Down Expand Up @@ -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()
)
);
Expand Down Expand Up @@ -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()
)
);
Expand All @@ -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()
)
);
Expand All @@ -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()
)
);
Expand Down Expand Up @@ -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()
)
);
Expand All @@ -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()
)
);
Expand All @@ -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()
)
);
Expand All @@ -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()
)
);
Expand Down
11 changes: 4 additions & 7 deletions src/Mvc/Model/Migration/TableAware/ListTablesDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Mvc/Model/Migration/TableAware/ListTablesInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 5 additions & 7 deletions src/Mvc/Model/Migration/TableAware/ListTablesIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 5a2b3aa

Please sign in to comment.