Passed
Pull Request — master (#881)
by Asmir
02:07
created

DumpSchemaCommand::execute()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 61
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 5.0035

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 37
nc 9
nop 2
dl 0
loc 61
ccs 36
cts 38
cp 0.9474
crap 5.0035
rs 9.0168
c 2
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\Tools\Console\Exception\InvalidOptionUsage;
8
use Doctrine\Migrations\Tools\Console\Exception\SchemaDumpRequiresNoMigrations;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use function addslashes;
13
use function assert;
14
use function class_exists;
15
use function is_string;
16
use function key;
17
use function sprintf;
18
use function strpos;
19
20
/**
21
 * The DumpSchemaCommand class is responsible for dumping your current database schema to a migration class. This is
22
 * intended to be used in conjuction with the RollupCommand.
23
 *
24
 * @see Doctrine\Migrations\Tools\Console\Command\RollupCommand
25
 */
26
class DumpSchemaCommand extends DoctrineCommand
27
{
28
    /** @var string */
29
    protected static $defaultName = 'migrations:dump-schema';
30
31 2
    protected function configure() : void
32
    {
33 2
        parent::configure();
34
35
        $this
36 2
            ->setAliases(['dump-schema'])
37 2
            ->setDescription('Dump the schema for your database to a migration.')
38 2
            ->setHelp(<<<EOT
39 2
The <info>%command.name%</info> command dumps the schema for your database to a migration:
40
41
    <info>%command.full_name%</info>
42
43
After dumping your schema to a migration, you can rollup your migrations using the <info>migrations:rollup</info> command.
44
EOT
45
            )
46 2
            ->addOption(
47 2
                'editor-cmd',
48 2
                null,
49 2
                InputOption::VALUE_OPTIONAL,
50 2
                'Open file with this command upon creation.'
51
            )
52 2
            ->addOption(
53 2
                'formatted',
54 2
                null,
55 2
                InputOption::VALUE_NONE,
56 2
                'Format the generated SQL.'
57
            )
58 2
            ->addOption(
59 2
                'namespace',
60 2
                null,
61 2
                InputOption::VALUE_REQUIRED,
62 2
                'Namespace to use for the generated migrations (defaults to the first namespace definition).'
63
            )
64 2
            ->addOption(
65 2
                'filter-tables',
66 2
                null,
67 2
                InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY,
68 2
                'Filter the tables to dump via Regex.'
69
            )
70 2
            ->addOption(
71 2
                'line-length',
72 2
                null,
73 2
                InputOption::VALUE_OPTIONAL,
74 2
                'Max line length of unformatted lines.',
75 2
                120
76
            );
77 2
    }
78
79
    /**
80
     * @throws SchemaDumpRequiresNoMigrations
81
     */
82 2
    public function execute(
83
        InputInterface $input,
84
        OutputInterface $output
85
    ) : ?int {
86 2
        $formatted  = $input->getOption('formatted');
87 2
        $lineLength = (int) $input->getOption('line-length');
88
89 2
        $schemaDumper = $this->getDependencyFactory()->getSchemaDumper();
90
91 2
        if ($formatted) {
92 1
            if (! class_exists('SqlFormatter')) {
93
                throw InvalidOptionUsage::new(
94
                    'The "--formatted" option can only be used if the sql formatter is installed. Please run "composer require jdorn/sql-formatter".'
95
                );
96
            }
97
        }
98
99 2
        $configuration = $this->getDependencyFactory()->getConfiguration();
100
101 2
        $namespace = $input->getOption('namespace');
102 2
        if ($namespace === null) {
103 2
            $dirs      = $configuration->getMigrationDirectories();
104 2
            $namespace = key($dirs);
105
        }
106 2
        assert(is_string($namespace));
107
108 2
        $this->checkNoPreviousDumpExistsForNamespace($namespace);
109
110 1
        $fqcn = $this->getDependencyFactory()->getClassNameGenerator()->generateClassName($namespace);
111
112 1
        $path = $schemaDumper->dump(
113 1
            $fqcn,
114 1
            $input->getOption('filter-tables'),
0 ignored issues
show
Bug introduced by
It seems like $input->getOption('filter-tables') can also be of type boolean and null and string; however, parameter $excludedTablesRegexes of Doctrine\Migrations\SchemaDumper::dump() does only seem to accept array, 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

114
            /** @scrutinizer ignore-type */ $input->getOption('filter-tables'),
Loading history...
115 1
            $formatted,
116 1
            $lineLength
117
        );
118
119 1
        $editorCommand = $input->getOption('editor-cmd');
120
121 1
        if ($editorCommand !== null) {
122 1
            assert(is_string($editorCommand));
123 1
            $this->procOpen($editorCommand, $path);
124
        }
125
126 1
        $output->writeln([
127 1
            sprintf('Dumped your schema to a new migration class at "<info>%s</info>"', $path),
128 1
            '',
129 1
            sprintf(
130 1
                'To run just this migration for testing purposes, you can use <info>migrations:execute --up \'%s\'</info>',
131 1
                addslashes($fqcn)
132
            ),
133 1
            '',
134 1
            sprintf(
135 1
                'To revert the migration you can use <info>migrations:execute --down \'%s\'</info>',
136 1
                addslashes($fqcn)
137
            ),
138 1
            '',
139 1
            'To use this as a rollup migration you can use the <info>migrations:rollup</info> command.',
140
        ]);
141
142 1
        return 0;
143
    }
144
145 2
    private function checkNoPreviousDumpExistsForNamespace(string $namespace) : void
146
    {
147 2
        $migrations = $this->getDependencyFactory()->getMigrationRepository()->getMigrations();
148 2
        foreach ($migrations->getItems() as $migration) {
149 1
            if (strpos((string) $migration->getVersion(), $namespace) !== false) {
150 1
                throw SchemaDumpRequiresNoMigrations::new($namespace);
151
            }
152
        }
153 1
    }
154
}
155