Skip to content

Commit

Permalink
Minor refactoring and cleanup (#237)
Browse files Browse the repository at this point in the history
Co-authored-by: Sergei Tigrov <rrr-r@ya.ru>
  • Loading branch information
vjik and Tigrov committed Nov 26, 2023
1 parent e8ad9e3 commit 3a29b7d
Show file tree
Hide file tree
Showing 22 changed files with 70 additions and 63 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/rector.yml
Expand Up @@ -14,8 +14,10 @@ name: rector
jobs:
rector:
uses: yiisoft/actions/.github/workflows/rector.yml@master
secrets:
token: ${{ secrets.YIISOFT_GITHUB_TOKEN }}
with:
os: >-
['ubuntu-latest']
php: >-
['8.0']
['8.2']
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -37,7 +37,7 @@ The preferred way to install this extension is through [composer](http://getcomp
Either run

```shell
composer require --prefer-dist yiisoft/db-migration
composer require yiisoft/db-migration
```

or add
Expand Down
6 changes: 3 additions & 3 deletions src/Command/CreateCommand.php
Expand Up @@ -90,9 +90,9 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$this->migrator->setIO($io);
$this->migrationService->setIO($io);
$this->createService->setIO($io);
$this->migrator->setIo($io);
$this->migrationService->setIo($io);
$this->createService->setIo($io);

/** @var string|null $path */
$path = $input->getOption('path');
Expand Down
10 changes: 5 additions & 5 deletions src/Command/DownCommand.php
Expand Up @@ -61,9 +61,9 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$this->migrator->setIO($io);
$this->migrationService->setIO($io);
$this->downRunner->setIO($io);
$this->migrator->setIo($io);
$this->migrationService->setIo($io);
$this->downRunner->setIo($io);

$this->migrationService->before(self::getDefaultName() ?? '');

Expand All @@ -78,9 +78,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::INVALID;
}

/** @psalm-var string[] $paths */
/** @var string[] $paths */
$paths = $input->getOption('path');
/** @psalm-var string[] $namespaces */
/** @var string[] $namespaces */
$namespaces = $input->getOption('namespace');

if (!empty($paths) || !empty($namespaces)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Command/HistoryCommand.php
Expand Up @@ -49,8 +49,8 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$this->migrator->setIO($io);
$this->migrationService->setIO($io);
$this->migrator->setIo($io);
$this->migrationService->setIo($io);

$this->migrationService->before(self::getDefaultName() ?? '');

Expand Down
8 changes: 4 additions & 4 deletions src/Command/NewCommand.php
Expand Up @@ -56,13 +56,13 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$this->migrator->setIO($io);
$this->migrationService->setIO($io);
$this->migrator->setIo($io);
$this->migrationService->setIo($io);

/** @psalm-var string[] $paths */
/** @var string[] $paths */
$paths = $input->getOption('path');

/** @psalm-var string[] $namespaces */
/** @var string[] $namespaces */
$namespaces = $input->getOption('namespace');

if (!empty($paths) || !empty($namespaces)) {
Expand Down
12 changes: 6 additions & 6 deletions src/Command/RedoCommand.php
Expand Up @@ -61,10 +61,10 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$this->migrator->setIO($io);
$this->migrationService->setIO($io);
$this->downRunner->setIO($io);
$this->updateRunner->setIO($io);
$this->migrator->setIo($io);
$this->migrationService->setIo($io);
$this->downRunner->setIo($io);
$this->updateRunner->setIo($io);

$this->migrationService->before(self::getDefaultName() ?? '');

Expand All @@ -79,9 +79,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::INVALID;
}

/** @psalm-var string[] $paths */
/** @var string[] $paths */
$paths = $input->getOption('path');
/** @psalm-var string[] $namespaces */
/** @var string[] $namespaces */
$namespaces = $input->getOption('namespace');

if (!empty($paths) || !empty($namespaces)) {
Expand Down
26 changes: 13 additions & 13 deletions src/Command/UpdateCommand.php
Expand Up @@ -59,14 +59,14 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$this->migrator->setIO($io);
$this->migrationService->setIO($io);
$this->updateRunner->setIO($io);
$this->migrator->setIo($io);
$this->migrationService->setIo($io);
$this->updateRunner->setIo($io);

/** @psalm-var string[] $paths */
/** @var string[] $paths */
$paths = $input->getOption('path');

/** @psalm-var string[] $namespaces */
/** @var string[] $namespaces */
$namespaces = $input->getOption('namespace');

if (!empty($paths) || !empty($namespaces)) {
Expand Down Expand Up @@ -101,15 +101,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::SUCCESS;
}

$n = count($migrations);
$migrationWord = $n === 1 ? 'migration' : 'migrations';
$migrationsCount = count($migrations);
$migrationWord = $migrationsCount === 1 ? 'migration' : 'migrations';

if ($limit !== null && $n > $limit) {
if ($limit !== null && $migrationsCount > $limit) {
$migrations = array_slice($migrations, 0, $limit);

$output->writeln("<fg=yellow>Total $limit out of $n new $migrationWord to be applied:</>\n");
$output->writeln("<fg=yellow>Total $limit out of $migrationsCount new $migrationWord to be applied:</>\n");
} else {
$output->writeln("<fg=yellow>Total $n new $migrationWord to be applied:</>\n");
$output->writeln("<fg=yellow>Total $migrationsCount new $migrationWord to be applied:</>\n");
}

foreach ($migrations as $i => $migration) {
Expand Down Expand Up @@ -137,20 +137,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int

if ($helper->ask($input, $output, $question)) {
$instances = $this->migrationService->makeMigrations($migrations);
$migrationWas = ($n === 1 ? 'migration was' : 'migrations were');
$migrationWas = ($migrationsCount === 1 ? 'migration was' : 'migrations were');

foreach ($instances as $i => $instance) {
try {
$this->updateRunner->run($instance, $i + 1);
} catch (Throwable $e) {
$output->writeln("\n<fg=yellow> >>> Total $i out of $n new $migrationWas applied.</>\n");
$output->writeln("\n<fg=yellow> >>> Total $i out of $migrationsCount new $migrationWas applied.</>\n");
$io->error($i > 0 ? 'Partially updated.' : 'Not updated.');

throw $e;
}
}

$output->writeln("\n<fg=green> >>> Total $n new $migrationWas applied.</>\n");
$output->writeln("\n<fg=green> >>> Total $migrationsCount new $migrationWas applied.</>\n");
$io->success('Updated successfully.');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Informer/ConsoleMigrationInformer.php
Expand Up @@ -33,7 +33,7 @@ public function endCommand(string $message): void
$this->io?->writeln(' ' . $message);
}

public function setIO(?SymfonyStyle $io): void
public function setIo(?SymfonyStyle $io): void
{
$this->io = $io;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Informer/MigrationInformerInterface.php
Expand Up @@ -19,5 +19,5 @@ public function beginCommand(string $message): void;

public function endCommand(string $message): void;

public function setIO(?SymfonyStyle $io): void;
public function setIo(?SymfonyStyle $io): void;
}
2 changes: 1 addition & 1 deletion src/Informer/NullMigrationInformer.php
Expand Up @@ -31,7 +31,7 @@ public function endCommand(string $message): void
// do nothing
}

public function setIO(?SymfonyStyle $io): void
public function setIo(?SymfonyStyle $io): void
{
// do nothing
}
Expand Down
5 changes: 3 additions & 2 deletions src/MigrationBuilder.php
Expand Up @@ -197,14 +197,15 @@ public function delete(string $table, array|string $condition = '', array $param
* @throws Exception
* @throws InvalidConfigException
* @throws NotSupportedException
*
* @psalm-param array<string, string|ColumnInterface> $columns
*/
public function createTable(string $table, array $columns, string|null $options = null): void
{
$time = $this->beginCommand("create table $table");

$this->db->createCommand()->createTable($table, $columns, $options)->execute();

/** @psalm-var array<string, string> $columns */
foreach ($columns as $column => $type) {
if ($type instanceof ColumnInterface) {
$comment = $type->getComment();
Expand Down Expand Up @@ -661,7 +662,7 @@ protected function endCommand(float $time): void

private function hasIndex(string $table, string $column): bool
{
/** @psalm-var Constraint[] $indexes */
/** @var Constraint[] $indexes */
$indexes = $this->db->getSchema()->getTableIndexes($table);

foreach ($indexes as $index) {
Expand Down
8 changes: 5 additions & 3 deletions src/Migrator.php
Expand Up @@ -23,9 +23,9 @@ public function __construct(
) {
}

public function setIO(?SymfonyStyle $io): void
public function setIo(?SymfonyStyle $io): void
{
$this->informer->setIO($io);
$this->informer->setIo($io);
}

public function up(MigrationInterface $migration): void
Expand Down Expand Up @@ -73,7 +73,9 @@ public function getMigrationNameLimit(): ?int
return $this->migrationNameLimit = $limit;
}

/** @psalm-return array<class-string, int|string> */
/**
* @psalm-return array<class-string, int|string>
*/
public function getHistory(?int $limit = null): array
{
$this->checkMigrationHistoryTable();
Expand Down
4 changes: 2 additions & 2 deletions src/Runner/DownRunner.php
Expand Up @@ -21,15 +21,15 @@ public function __construct(private Migrator $migrator)
{
}

public function setIO(?SymfonyStyle $io): void
public function setIo(?SymfonyStyle $io): void
{
$this->io = $io;
}

public function run(RevertibleMigrationInterface $migration, int|null $number = null): void
{
if ($this->io === null) {
throw new RuntimeException('You need to set output decorator via `setIO()`.');
throw new RuntimeException('You need to set output decorator via `setIo()`.');
}

$num = $number !== null ? $number . '. ' : '';
Expand Down
4 changes: 2 additions & 2 deletions src/Runner/UpdateRunner.php
Expand Up @@ -21,15 +21,15 @@ public function __construct(private Migrator $migrator)
{
}

public function setIO(?SymfonyStyle $io): void
public function setIo(?SymfonyStyle $io): void
{
$this->io = $io;
}

public function run(MigrationInterface $migration, int|null $number = null): void
{
if ($this->io === null) {
throw new RuntimeException('You need to set output decorator via `setIO()`.');
throw new RuntimeException('You need to set output decorator via `setIo()`.');
}

$num = $number !== null ? $number . '. ' : '';
Expand Down
2 changes: 1 addition & 1 deletion src/Service/Generate/CreateService.php
Expand Up @@ -131,7 +131,7 @@ public function setTemplates(array $value = []): void
$this->templates = $value;
}

public function setIO(?SymfonyStyle $io): void
public function setIo(?SymfonyStyle $io): void
{
$this->io = $io;
}
Expand Down
22 changes: 12 additions & 10 deletions src/Service/MigrationService.php
Expand Up @@ -47,9 +47,9 @@ final class MigrationService
{
private string $createNamespace = '';
private string $createPath = '';
/** @psalm-var string[] */
/** @var string[] */
private array $updateNamespaces = [];
/** @psalm-var string[] */
/** @var string[] */
private array $updatePaths = [];
private string $version = '1.0';
private ?SymfonyStyle $io = null;
Expand All @@ -62,7 +62,7 @@ public function __construct(
) {
}

public function setIO(?SymfonyStyle $io): void
public function setIo(?SymfonyStyle $io): void
{
$this->io = $io;
}
Expand Down Expand Up @@ -227,9 +227,6 @@ public function databaseConnection(): void
/**
* Creates a new migration instance.
*
* @psalm-suppress MoreSpecificReturnType
* @psalm-suppress LessSpecificReturnStatement
*
* @param string $class The migration class name.
*
* @return object The migration instance.
Expand Down Expand Up @@ -441,10 +438,12 @@ private function getPathFromNamespace(string $path): string
{
$namespacesPath = [];

/** @psalm-suppress UnresolvableInclude */
/**
* @psalm-suppress UnresolvableInclude
* @psalm-var array<string, list<string>> $map
*/
$map = require $this->getVendorDir() . '/composer/autoload_psr4.php';

/** @psalm-var array<string, list<string>> $map */
foreach ($map as $namespace => $directories) {
foreach ($directories as $directory) {
$namespacesPath[str_replace('\\', '/', trim($namespace, '\\'))] = $directory;
Expand All @@ -465,10 +464,13 @@ private function getNamespacesFromPath(string $path): array
{
$namespaces = [];
$path = realpath($this->aliases->get($path)) . DIRECTORY_SEPARATOR;
/** @psalm-suppress UnresolvableInclude */

/**
* @psalm-suppress UnresolvableInclude
* @psalm-var array<string, list<string>> $map
*/
$map = require $this->getVendorDir() . '/composer/autoload_psr4.php';

/** @psalm-var array<string, list<string>> $map */
foreach ($map as $namespace => $directories) {
foreach ($directories as $directory) {
$directory = realpath($directory) . DIRECTORY_SEPARATOR;
Expand Down
2 changes: 1 addition & 1 deletion tests/Common/Runner/AbstractDownRunnerTest.php
Expand Up @@ -20,7 +20,7 @@ public function testWithoutIO(): void
$runner = new DownRunner($this->container->get(Migrator::class));

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('You need to set output decorator via `setIO()`.');
$this->expectExceptionMessage('You need to set output decorator via `setIo()`.');

$runner->run(new StubRevertibleMigration());
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Common/Runner/AbstractUpRunnerTest.php
Expand Up @@ -20,7 +20,7 @@ public function testWithoutIO(): void
$runner = new UpdateRunner($this->container->get(Migrator::class));

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('You need to set output decorator via `setIO()`.');
$this->expectExceptionMessage('You need to set output decorator via `setIo()`.');

$runner->run(new StubMigration());
}
Expand Down
Expand Up @@ -25,7 +25,7 @@ public function testWithoutIO(): void
);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('You need to set output decorator via `setIO()`.');
$this->expectExceptionMessage('You need to set output decorator via `setIo()`.');

$service->run();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Migration/NullMigrationInformerTest.php
Expand Up @@ -15,7 +15,7 @@ public function testSetIO(): void
$io = $this->createMock(SymfonyStyle::class);
$nullInformer = new NullMigrationInformer();

$nullInformer->setIO($io);
$nullInformer->setIo($io);

$this->expectNotToPerformAssertions();
}
Expand Down

0 comments on commit 3a29b7d

Please sign in to comment.