| Conditions | 13 |
| Paths | 490 |
| Total Lines | 78 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 44 |
| CRAP Score | 13.292 |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php declare(strict_types=1); |
||
| 50 | 8 | protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) |
|
| 51 | { |
||
| 52 | 8 | $directories = $input->getArgument('migration-directories'); |
|
| 53 | 8 | $migrations = $this->getMigrations($directories); |
|
|
1 ignored issue
–
show
|
|||
| 54 | 6 | $db = $this->connect($input->getOption('server'), $input->getArgument('database')); |
|
| 55 | |||
| 56 | 6 | $this->acquireLock($db); |
|
| 57 | |||
| 58 | try { |
||
| 59 | 5 | $databaseMigrationsCollection = $this->getMigrationsCollection($db); |
|
| 60 | 5 | $progress = new Console\Helper\ProgressBar($output, count($migrations)); |
|
| 61 | |||
| 62 | 5 | switch ($output->getVerbosity()) { |
|
| 63 | 5 | case $output::VERBOSITY_VERBOSE: |
|
| 64 | $format = 'verbose'; |
||
| 65 | break; |
||
| 66 | 5 | case $output::VERBOSITY_VERY_VERBOSE: |
|
| 67 | $format = 'very_verbose'; |
||
| 68 | break; |
||
| 69 | 5 | case $output::VERBOSITY_DEBUG: |
|
| 70 | $format = 'debug'; |
||
| 71 | break; |
||
| 72 | default: |
||
| 73 | 5 | $format = 'normal'; |
|
| 74 | } |
||
| 75 | |||
| 76 | 5 | $progress->setFormat($format); |
|
| 77 | 5 | $progress->start(); |
|
| 78 | 5 | $executedMigrations = 0; |
|
| 79 | |||
| 80 | 5 | foreach ($migrations as $id => $migration) { |
|
| 81 | 5 | $progress->advance(); |
|
| 82 | |||
| 83 | 5 | if ($migration instanceof MongoDbMigrations\ContextualMigrationInterface && $input->getOption('contexts') !== []) { |
|
| 84 | 4 | if ($migration->getContexts() === []) { |
|
| 85 | 1 | throw new \InvalidArgumentException('An empty context specification is not allowed'); |
|
| 86 | } |
||
| 87 | |||
| 88 | 4 | if (array_intersect($migration->getContexts(), $input->getOption('contexts')) === []) { |
|
|
1 ignored issue
–
show
|
|||
| 89 | 2 | continue; |
|
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | 5 | if (!$migration instanceof MongoDbMigrations\RunAlwaysMigrationInterface) { |
|
| 94 | 5 | if ($databaseMigrationsCollection->countDocuments(['migration_id' => $id]) > 0) { |
|
| 95 | 1 | continue; |
|
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | 5 | $migration->execute($db); |
|
| 100 | 5 | $executedMigrations++; |
|
| 101 | |||
| 102 | $migrationInfo = [ |
||
| 103 | 5 | 'migration_id' => $id, |
|
| 104 | 5 | 'migration_class' => get_class($migration), |
|
| 105 | 5 | 'last_execution_date' => new MongoDB\BSON\UTCDatetime((new \DateTime())->getTimestamp() * 1000), |
|
| 106 | 5 | 'run_always' => $migration instanceof MongoDbMigrations\RunAlwaysMigrationInterface, |
|
| 107 | ]; |
||
| 108 | |||
| 109 | 5 | if ($migration instanceof MongoDbMigrations\ContextualMigrationInterface) { |
|
| 110 | 2 | $migrationInfo['contexts'] = $migration->getContexts(); |
|
| 111 | } |
||
| 112 | |||
| 113 | 5 | $databaseMigrationsCollection->updateOne( |
|
| 114 | 5 | ['migration_id' => $id], |
|
| 115 | 5 | ['$set' => $migrationInfo], |
|
| 116 | 5 | ['upsert' => true] |
|
| 117 | ); |
||
| 118 | } |
||
| 119 | |||
| 120 | 3 | $progress->finish(); |
|
| 121 | 3 | $output->writeln(''); |
|
| 122 | |||
| 123 | 3 | $output->writeln("<info>✓ Successfully executed {$executedMigrations} migrations</info>"); |
|
| 124 | 2 | } catch (\Exception $e) { |
|
| 125 | 2 | throw new Console\Exception\RuntimeException('Error while executing migrations', $e->getCode(), $e); |
|
| 126 | 3 | } finally { |
|
| 127 | 5 | $this->releaseLock($db); |
|
| 128 | } |
||
| 131 |