Failed Conditions
Pull Request — master (#917)
by Asmir
02:36
created

UpToDateCommand::execute()   C

Complexity

Conditions 13
Paths 16

Size

Total Lines 56
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 13.0376

Importance

Changes 0
Metric Value
cc 13
eloc 33
c 0
b 0
f 0
nc 16
nop 2
dl 0
loc 56
ccs 31
cts 33
cp 0.9394
crap 13.0376
rs 6.6166

How to fix   Long Method    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\Exception\MetadataStorageError;
8
use Doctrine\Migrations\Metadata\AvailableMigration;
9
use Doctrine\Migrations\Metadata\AvailableMigrationsList;
10
use Doctrine\Migrations\Metadata\ExecutedMigration;
11
use Doctrine\Migrations\Metadata\ExecutedMigrationsSet;
12
use Doctrine\Migrations\Version\Version;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use function array_map;
17
use function array_merge;
18
use function array_unique;
19
use function count;
20
use function sprintf;
21
use function uasort;
22
23
/**
24
 * The UpToDateCommand class outputs if your database is up to date or if there are new migrations
25
 * that need to be executed.
26
 */
27
class UpToDateCommand extends DoctrineCommand
28
{
29
    /** @var string */
30
    protected static $defaultName = 'migrations:up-to-date';
31
32 8
    protected function configure() : void
33
    {
34
        $this
35 8
            ->setAliases(['up-to-date'])
36 8
            ->setDescription('Tells you if your schema is up-to-date.')
37 8
            ->addOption('fail-on-unregistered', 'u', InputOption::VALUE_NONE, 'Whether to fail when there are unregistered extra migrations found')
38 8
            ->addOption('extended', 'e', InputOption::VALUE_NONE, 'Show extended information on versions.')
39 8
            ->setHelp(<<<EOT
40 8
The <info>%command.name%</info> command tells you if your schema is up-to-date:
41
42
    <info>%command.full_name%</info>
43
EOT
44
            );
45
46 8
        parent::configure();
47 8
    }
48
49 8
    public function execute(InputInterface $input, OutputInterface $output) : ?int
50
    {
51 8
        $statusCalculator = $this->getDependencyFactory()->getMigrationStatusCalculator();
52
53
        try {
54 8
            $executedUnavailableMigrations = $statusCalculator->getExecutedUnavailableMigrations();
55 1
        } catch (MetadataStorageError $metadataStorageError) {
56 1
            $output->writeln(sprintf(
57 1
                '<error>%s</error>',
58 1
                $metadataStorageError->getMessage()
59
            ));
60
61 1
            return 3;
62
        }
63
64 7
        $newMigrations = $statusCalculator->getNewMigrations();
65
66 7
        $newMigrationsCount                 = count($newMigrations);
67 7
        $executedUnavailableMigrationsCount =  count($executedUnavailableMigrations);
68
69 7
        if ($newMigrationsCount === 0 && $executedUnavailableMigrationsCount ===0) {
70 2
            $output->writeln('<comment>Up-to-date! No migrations to execute.</comment>');
71
72 2
            return 0;
73
        }
74
75 5
        if ($newMigrationsCount > 0) {
76 3
            $output->writeln(sprintf(
77 3
                '<error>Out-of-date! %u migration%s available to execute.</error>',
78 3
                $newMigrationsCount,
79 3
                $newMigrationsCount > 1 ? 's are' : ' is'
80
            ));
81
82 3
            if ($input->getOption('extended')) {
83 1
                $versions = $this->getSortedVersions($newMigrations, $executedUnavailableMigrations);
84 1
                $this->getDependencyFactory()->getMigrationStatusInfosHelper()->listVersions($versions, $output);
85
            }
86
87 3
            return 1;
88
        }
89
90 2
        if ($executedUnavailableMigrationsCount > 0) {
91 2
            $output->writeln(sprintf(
92 2
                '<error>You have %1$u previously executed migration%3$s in the database that %2$s registered migration%3$s.</error>',
93 2
                $executedUnavailableMigrationsCount,
94 2
                $executedUnavailableMigrationsCount > 1 ? 'are not' : 'is not a',
95 2
                $executedUnavailableMigrationsCount > 1 ? 's' : ''
96
            ));
97
98 2
            if ($input->getOption('extended')) {
99
                $versions = $this->getSortedVersions($newMigrations, $executedUnavailableMigrations);
100
                $this->getDependencyFactory()->getMigrationStatusInfosHelper()->listVersions($versions, $output);
101
            }
102
        }
103
104 2
        return $executedUnavailableMigrationsCount > 0 && $input->getOption('fail-on-unregistered') === true ? 2 : 0;
105
    }
106
107
    /**
108
     * @return Version[]
109
     */
110 1
    private function getSortedVersions(AvailableMigrationsList $newMigrations, ExecutedMigrationsSet $executedUnavailableMigrations) : array
111
    {
112
        $executedUnavailableVersion = array_map(static function (ExecutedMigration $executedMigration) : Version {
113 1
            return $executedMigration->getVersion();
114 1
        }, $executedUnavailableMigrations->getItems());
115
116
        $newVersions = array_map(static function (AvailableMigration $availableMigration) : Version {
117 1
            return $availableMigration->getVersion();
118 1
        }, $newMigrations->getItems());
119
120 1
        $versions = array_unique(array_merge($executedUnavailableVersion, $newVersions));
121
122 1
        $comparator = $this->getDependencyFactory()->getVersionComparator();
123
        uasort($versions, static function (Version $a, Version $b) use ($comparator) : int {
124 1
            return $comparator->compare($a, $b);
125 1
        });
126
127 1
        return $versions;
128
    }
129
}
130