Passed
Push — master ( 229462...d74785 )
by Andreas
02:20 queued 14s
created

DumpSchemaCommand::execute()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 64
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 6.0045

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 39
c 2
b 0
f 0
nc 10
nop 2
dl 0
loc 64
ccs 38
cts 40
cp 0.95
crap 6.0045
rs 8.6737

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 count;
16
use function is_string;
17
use function key;
18
use function sprintf;
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 2
        $versions     = $this->getDependencyFactory()->getMigrationRepository()->getMigrations();
91
92 2
        if (count($versions) > 0) {
93 1
            throw SchemaDumpRequiresNoMigrations::new();
94
        }
95
96 1
        if ($formatted) {
97 1
            if (! class_exists('SqlFormatter')) {
98
                throw InvalidOptionUsage::new(
99
                    'The "--formatted" option can only be used if the sql formatter is installed. Please run "composer require jdorn/sql-formatter".'
100
                );
101
            }
102
        }
103
104 1
        $configuration = $this->getDependencyFactory()->getConfiguration();
105
106 1
        $namespace = $input->getOption('namespace');
107 1
        if ($namespace === null) {
108 1
            $dirs      = $configuration->getMigrationDirectories();
109 1
            $namespace = key($dirs);
110
        }
111 1
        assert(is_string($namespace));
112
113 1
        $fqcn = $this->getDependencyFactory()->getClassNameGenerator()->generateClassName($namespace);
114
115 1
        $path = $schemaDumper->dump(
116 1
            $fqcn,
117 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

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