Completed
Pull Request — master (#949)
by Grégoire
02:40
created

DiffCommand::execute()   B

Complexity

Conditions 9
Paths 60

Size

Total Lines 74
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 10.1071

Importance

Changes 0
Metric Value
cc 9
eloc 46
c 0
b 0
f 0
nc 60
nop 2
dl 0
loc 74
ccs 35
cts 46
cp 0.7609
crap 10.1071
rs 7.6226

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');
93 1
        if ($filterExpression === '') {
94
            $filterExpression = null;
95
        }
96
97 1
        $formatted       = filter_var($input->getOption('formatted'), FILTER_VALIDATE_BOOLEAN);
98 1
        $lineLength      = (int) $input->getOption('line-length');
99 1
        $allowEmptyDiff  = $input->getOption('allow-empty-diff');
100 1
        $checkDbPlatform = filter_var($input->getOption('check-database-platform'), FILTER_VALIDATE_BOOLEAN);
101 1
        $namespace       = $input->getOption('namespace');
102 1
        if ($namespace === '') {
103
            $namespace = null;
104
        }
105
106 1
        if ($formatted) {
107 1
            if (! class_exists('SqlFormatter')) {
108
                throw InvalidOptionUsage::new(
109
                    'The "--formatted" option can only be used if the sql formatter is installed. Please run "composer require jdorn/sql-formatter".'
110
                );
111
            }
112
        }
113
114 1
        $configuration = $this->getDependencyFactory()->getConfiguration();
115
116 1
        $dirs = $configuration->getMigrationDirectories();
117 1
        if ($namespace === null) {
118
            $namespace = key($dirs);
119 1
        } elseif (! isset($dirs[$namespace])) {
120
            throw new OutOfBoundsException(sprintf('Path not defined for the namespace %s', $namespace));
0 ignored issues
show
Bug introduced by
It seems like $namespace can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

120
            throw new OutOfBoundsException(sprintf('Path not defined for the namespace %s', /** @scrutinizer ignore-type */ $namespace));
Loading history...
121
        }
122
123 1
        assert(is_string($namespace));
124
125 1
        $fqcn = $this->getDependencyFactory()->getClassNameGenerator()->generateClassName($namespace);
126
127 1
        $diffGenerator = $this->getDependencyFactory()->getDiffGenerator();
128
129
        try {
130 1
            $path = $diffGenerator->generate(
131 1
                $fqcn,
132 1
                $filterExpression,
133 1
                $formatted,
134 1
                $lineLength,
135 1
                $checkDbPlatform
136
            );
137
        } catch (NoChangesDetected $exception) {
138
            if ($allowEmptyDiff) {
139
                $output->writeln($exception->getMessage());
140
141
                return 0;
142
            }
143
144
            throw $exception;
145
        }
146
147 1
        $output->writeln([
148 1
            sprintf('Generated new migration class to "<info>%s</info>"', $path),
149 1
            '',
150 1
            sprintf(
151 1
                'To run just this migration for testing purposes, you can use <info>migrations:execute --up \'%s\'</info>',
152 1
                addslashes($fqcn)
153
            ),
154 1
            '',
155 1
            sprintf(
156 1
                'To revert the migration you can use <info>migrations:execute --down \'%s\'</info>',
157 1
                addslashes($fqcn)
158
            ),
159
        ]);
160
161 1
        return 0;
162
    }
163
}
164