Failed Conditions
Pull Request — master (#632)
by Michael
02:44
created

StatusCommand::execute()   C

Complexity

Conditions 10
Paths 70

Size

Total Lines 48
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 12.6013

Importance

Changes 0
Metric Value
cc 10
eloc 26
nc 70
nop 2
dl 0
loc 48
ccs 19
cts 27
cp 0.7037
crap 12.6013
rs 5.3454
c 0
b 0
f 0

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console\Command;
6
7
use Doctrine\Migrations\Configuration\Configuration;
8
use Doctrine\Migrations\Tools\Console\Helper\MigrationStatusInfosHelper;
9
use Doctrine\Migrations\Version;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use function count;
14
use function in_array;
15
use function max;
16
use function sprintf;
17
use function str_repeat;
18
use function strlen;
19
20
class StatusCommand extends AbstractCommand
21
{
22 11
    protected function configure() : void
23
    {
24
        $this
25 11
            ->setName('migrations:status')
26 11
            ->setDescription('View the status of a set of migrations.')
27 11
            ->addOption(
28 11
                'show-versions',
29 11
                null,
30 11
                InputOption::VALUE_NONE,
31 11
                'This will display a list of all available migrations and their status'
32
            )
33 11
            ->setHelp(<<<EOT
34 11
The <info>%command.name%</info> command outputs the status of a set of migrations:
35
36
    <info>%command.full_name%</info>
37
38
You can output a list of all available migrations and their status with <comment>--show-versions</comment>:
39
40
    <info>%command.full_name% --show-versions</info>
41
EOT
42
        );
43
44 11
        parent::configure();
45 11
    }
46
47 9
    public function execute(InputInterface $input, OutputInterface $output) : void
48
    {
49 9
        $configuration = $this->getMigrationConfiguration($input, $output);
50
51 9
        $infos = new MigrationStatusInfosHelper($configuration);
52
53 9
        $output->writeln("\n <info>==</info> Configuration\n");
54
55 9
        foreach ($infos->getMigrationsInfos() as $name => $value) {
56 9
            $string = (string) $value;
57
58 9
            if ($name === 'New Migrations') {
59 9
                $string = $value > 0 ? '<question>' . $value . '</question>' : '0';
60
            }
61
62 9
            if ($name === 'Executed Unavailable Migrations') {
63 9
                $string = $value > 0 ? '<error>' . $value . '</error>' : '0';
64
            }
65
66 9
            $this->writeStatusInfosLineAligned($output, $name, $string);
67
        }
68
69 9
        if (! $input->getOption('show-versions')) {
70 8
            return;
71
        }
72
73 1
        $migrations = $configuration->getMigrations();
74
75 1
        if ($migrations) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $migrations of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
76 1
            $output->writeln("\n <info>==</info> Available Migration Versions\n");
77
78 1
            $this->showVersions($migrations, $configuration, $output);
79
        }
80
81 1
        if (! count($infos->getExecutedUnavailableMigrations())) {
82 1
            return;
83
        }
84
85
        $output->writeln(
86
            "\n <info>==</info> Previously Executed Unavailable Migration Versions\n"
87
        );
88
89
        foreach ($infos->getExecutedUnavailableMigrations() as $executedUnavailableMigration) {
90
            $output->writeln(
91
                sprintf(
92
                    '    <comment>>></comment> %s (<comment>%s</comment>)',
93
                    $configuration->getDateTime($executedUnavailableMigration),
94
                    $executedUnavailableMigration
95
                )
96
            );
97
        }
98
    }
99
100 9
    private function writeStatusInfosLineAligned(OutputInterface $output, string $title, ?string $value) : void
101
    {
102 9
        $output->writeln(sprintf(
103 9
            '    <comment>>></comment> %s: %s%s',
104 9
            $title,
105 9
            str_repeat(' ', 50 - strlen($title)),
106 9
            $value
107
        ));
108 9
    }
109
110
    /**
111
     * @param Version[] $migrations
112
     */
113 1
    private function showVersions(array $migrations, Configuration $configuration, OutputInterface $output) : void
114
    {
115 1
        $migratedVersions = $configuration->getMigratedVersions();
116
117 1
        foreach ($migrations as $version) {
118 1
            $isMigrated = in_array($version->getVersion(), $migratedVersions, true);
119 1
            $status     = $isMigrated ? '<info>migrated</info>' : '<error>not migrated</error>';
120
121 1
            $migrationDescription = $version->getMigration()->getDescription()
122
                ? str_repeat(' ', 5) . $version->getMigration()->getDescription()
123 1
                : '';
124
125 1
            $formattedVersion = $configuration->getDateTime($version->getVersion());
126
127 1
            $output->writeln(sprintf(
128 1
                '    <comment>>></comment> %s (<comment>%s</comment>)%s%s%s',
129 1
                $formattedVersion,
130 1
                $version->getVersion(),
131 1
                str_repeat(' ', max(1, 49 - strlen($formattedVersion) - strlen($version->getVersion()))),
132 1
                $status,
133 1
                $migrationDescription
134
            ));
135
        }
136 1
    }
137
}
138