Failed Conditions
Pull Request — master (#944)
by Asmir
02:33
created

DiffCommand::execute()   B

Complexity

Conditions 9
Paths 15

Size

Total Lines 66
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 9.7971

Importance

Changes 0
Metric Value
cc 9
eloc 42
nc 15
nop 2
dl 0
loc 66
ccs 33
cts 42
cp 0.7856
crap 9.7971
rs 7.6924
c 0
b 0
f 0

How to fix   Long Method   

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\Generator\Exception\NoChangesDetected;
8
use Doctrine\Migrations\Tools\Console\Exception\InvalidOptionUsage;
9
use OutOfBoundsException;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use function addslashes;
14
use function assert;
15
use function class_exists;
16
use function filter_var;
17
use function is_string;
18
use function key;
19
use function sprintf;
20
use const FILTER_VALIDATE_BOOLEAN;
21
22
/**
23
 * The DiffCommand class is responsible for generating a migration by comparing your current database schema to
24
 * your mapping information.
25
 */
26
final class DiffCommand extends DoctrineCommand
27
{
28
    /** @var string */
29
    protected static $defaultName = 'migrations:diff';
30
31 1
    protected function configure() : void
32
    {
33 1
        parent::configure();
34
35
        $this
36 1
            ->setAliases(['diff'])
37 1
            ->setDescription('Generate a migration by comparing your current database to your mapping information.')
38 1
            ->setHelp(<<<EOT
39 1
The <info>%command.name%</info> command generates a migration by comparing your current database to your mapping information:
40
41
    <info>%command.full_name%</info>
42
43
EOT
44
            )
45 1
            ->addOption(
46 1
                'namespace',
47 1
                null,
48 1
                InputOption::VALUE_REQUIRED,
49 1
                'The namespace to use for the migration (must be in the list of configured namespaces)'
50
            )
51 1
            ->addOption(
52 1
                'filter-expression',
53 1
                null,
54 1
                InputOption::VALUE_REQUIRED,
55 1
                'Tables which are filtered by Regular Expression.'
56
            )
57 1
            ->addOption(
58 1
                'formatted',
59 1
                null,
60 1
                InputOption::VALUE_NONE,
61 1
                'Format the generated SQL.'
62
            )
63 1
            ->addOption(
64 1
                'line-length',
65 1
                null,
66 1
                InputOption::VALUE_REQUIRED,
67 1
                'Max line length of unformatted lines.',
68 1
                120
69
            )
70 1
            ->addOption(
71 1
                'check-database-platform',
72 1
                null,
73 1
                InputOption::VALUE_OPTIONAL,
74 1
                'Check Database Platform to the generated code.',
75 1
                false
76
            )
77 1
            ->addOption(
78 1
                'allow-empty-diff',
79 1
                null,
80 1
                InputOption::VALUE_NONE,
81 1
                'Do not throw an exception when no changes are detected.'
82
            );
83 1
    }
84
85
    /**
86
     * @throws InvalidOptionUsage
87
     */
88 1
    public function execute(
89
        InputInterface $input,
90
        OutputInterface $output
91
    ) : ?int {
92 1
        $filterExpression = (string) $input->getOption('filter-expression') ?: null;
93 1
        $formatted        = filter_var($input->getOption('formatted'), FILTER_VALIDATE_BOOLEAN);
94 1
        $lineLength       = (int) $input->getOption('line-length');
95 1
        $allowEmptyDiff   = $input->getOption('allow-empty-diff');
96 1
        $checkDbPlatform  = filter_var($input->getOption('check-database-platform'), FILTER_VALIDATE_BOOLEAN);
97 1
        $namespace        = $input->getOption('namespace') ?: null;
98 1
        if ($formatted) {
99 1
            if (! class_exists('SqlFormatter')) {
100
                throw InvalidOptionUsage::new(
101
                    'The "--formatted" option can only be used if the sql formatter is installed. Please run "composer require jdorn/sql-formatter".'
102
                );
103
            }
104
        }
105
106 1
        $configuration = $this->getDependencyFactory()->getConfiguration();
107
108 1
        $dirs = $configuration->getMigrationDirectories();
109 1
        if ($namespace === null) {
110
            $namespace = key($dirs);
111 1
        } elseif (! isset($dirs[$namespace])) {
112
            throw new OutOfBoundsException(sprintf('Path not defined for the namespace %s', $namespace));
113
        }
114
115 1
        assert(is_string($namespace));
116
117 1
        $fqcn = $this->getDependencyFactory()->getClassNameGenerator()->generateClassName($namespace);
118
119 1
        $diffGenerator = $this->getDependencyFactory()->getDiffGenerator();
120
121
        try {
122 1
            $path = $diffGenerator->generate(
123 1
                $fqcn,
124 1
                $filterExpression,
125 1
                $formatted,
126 1
                $lineLength,
127 1
                $checkDbPlatform
128
            );
129
        } catch (NoChangesDetected $exception) {
130
            if ($allowEmptyDiff) {
131
                $output->writeln($exception->getMessage());
132
133
                return 0;
134
            }
135
136
            throw $exception;
137
        }
138
139 1
        $output->writeln([
140 1
            sprintf('Generated new migration class to "<info>%s</info>"', $path),
141 1
            '',
142 1
            sprintf(
143 1
                'To run just this migration for testing purposes, you can use <info>migrations:execute --up \'%s\'</info>',
144 1
                addslashes($fqcn)
145
            ),
146 1
            '',
147 1
            sprintf(
148 1
                'To revert the migration you can use <info>migrations:execute --down \'%s\'</info>',
149 1
                addslashes($fqcn)
150
            ),
151
        ]);
152
153 1
        return 0;
154
    }
155
}
156