| Conditions | 13 |
| Paths | 84 |
| Total Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 27 |
| CRAP Score | 13.3627 |
| Changes | 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); |
||
| 85 | 7 | protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) |
|
| 86 | { |
||
| 87 | 7 | $id = $input->getOption('id'); |
|
| 88 | 7 | $directories = $input->getArgument('migration-directories'); |
|
| 89 | 7 | $migrations = $this->getMigrations($directories); |
|
|
1 ignored issue
–
show
|
|||
| 90 | 6 | $db = $this->connect($input->getOption('server'), $input->getArgument('database')); |
|
|
1 ignored issue
–
show
|
|||
| 91 | |||
| 92 | 6 | $add = $input->getOption('add'); |
|
| 93 | 6 | $delete = $input->getOption('delete'); |
|
| 94 | 6 | $all = $input->getOption('all'); |
|
| 95 | |||
| 96 | // Assume add is the default behavior |
||
| 97 | 6 | if ($add === false && $delete === false) { |
|
| 98 | 5 | $add = true; |
|
| 99 | } |
||
| 100 | |||
| 101 | 6 | if ($add === true && $delete === true) { |
|
| 102 | throw new Console\Exception\RuntimeException("--add and --delete is not allowed to be set both"); |
||
| 103 | } |
||
| 104 | |||
| 105 | 6 | if (!$all && $id === null) { |
|
| 106 | 1 | throw new Console\Exception\RuntimeException("Specify --all or a single migration id"); |
|
| 107 | } |
||
| 108 | |||
| 109 | 5 | $this->acquireLock($db); |
|
| 110 | |||
| 111 | try { |
||
| 112 | 4 | if ($all) { |
|
| 113 | 3 | $id = null; |
|
| 114 | } |
||
| 115 | |||
| 116 | 4 | if ($id !== null) { |
|
| 117 | $migrations = array_filter($migrations, function (MongoDbMigrations\MigrationInterface $migration) use ($id): bool { |
||
| 118 | 1 | return $migration->getId() === $id; |
|
| 119 | 1 | }); |
|
| 120 | |||
| 121 | 1 | if (count($migrations) === 0) { |
|
| 122 | throw new Console\Exception\RuntimeException("No migration for id '{$id}' found"); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | 4 | if ($add) { |
|
| 127 | 3 | $this->addMigrations($migrations, $db); |
|
| 128 | 2 | } elseif ($delete) { |
|
| 129 | 4 | $this->deleteMigrations($migrations, $db); |
|
| 130 | } |
||
| 131 | } catch (\Exception $e) { |
||
| 132 | throw new Console\Exception\RuntimeException('Error while executing migrations', $e->getCode(), $e); |
||
| 133 | 4 | } finally { |
|
| 134 | 4 | $this->releaseLock($db); |
|
| 135 | } |
||
| 136 | 4 | } |
|
| 137 | |||
| 185 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.