Failed Conditions
Push — master ( 85e30d...9d20a3 )
by Andreas
02:08
created

DumpSchemaCommand::execute()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 53
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 5.0061

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 31
c 2
b 0
f 0
nc 6
nop 2
dl 0
loc 53
ccs 30
cts 32
cp 0.9375
crap 5.0061
rs 9.1128

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 class_exists;
13
use function count;
14
use function sprintf;
15
16
/**
17
 * The DumpSchemaCommand class is responsible for dumping your current database schema to a migration class. This is
18
 * intended to be used in conjuction with the RollupCommand.
19
 *
20
 * @see Doctrine\Migrations\Tools\Console\Command\RollupCommand
21
 */
22
class DumpSchemaCommand extends AbstractCommand
23
{
24
    /** @var string */
25
    protected static $defaultName = 'migrations:dump-schema';
26
27 10
    protected function configure() : void
28
    {
29 10
        parent::configure();
30
31
        $this
32 10
            ->setAliases(['dump-schema'])
33 10
            ->setDescription('Dump the schema for your database to a migration.')
34 10
            ->setHelp(<<<EOT
35 10
The <info>%command.name%</info> command dumps the schema for your database to a migration:
36
37
    <info>%command.full_name%</info>
38
39
After dumping your schema to a migration, you can rollup your migrations using the <info>migrations:rollup</info> command.
40
EOT
41
            )
42 10
            ->addOption(
43 10
                'editor-cmd',
44 10
                null,
45 10
                InputOption::VALUE_OPTIONAL,
46 10
                'Open file with this command upon creation.'
47
            )
48 10
            ->addOption(
49 10
                'formatted',
50 10
                null,
51 10
                InputOption::VALUE_NONE,
52 10
                'Format the generated SQL.'
53
            )
54 10
            ->addOption(
55 10
                'line-length',
56 10
                null,
57 10
                InputOption::VALUE_OPTIONAL,
58 10
                'Max line length of unformatted lines.',
59 10
                120
60
            );
61 10
    }
62
63
    /**
64
     * @throws SchemaDumpRequiresNoMigrations
65
     */
66 3
    public function execute(
67
        InputInterface $input,
68
        OutputInterface $output
69
    ) : ?int {
70 3
        $formatted  = (bool) $input->getOption('formatted');
71 3
        $lineLength = (int) $input->getOption('line-length');
72
73 3
        $schemaDumper = $this->dependencyFactory->getSchemaDumper();
74 3
        $versions     = $this->migrationRepository->getVersions();
75
76 3
        if (count($versions) > 0) {
77 2
            throw SchemaDumpRequiresNoMigrations::new();
78
        }
79
80 1
        if ($formatted) {
81 1
            if (! class_exists('SqlFormatter')) {
82
                throw InvalidOptionUsage::new(
83
                    'The "--formatted" option can only be used if the sql formatter is installed. Please run "composer require jdorn/sql-formatter".'
84
                );
85
            }
86
        }
87
88 1
        $versionNumber = $this->configuration->generateVersionNumber();
89
90 1
        $path = $schemaDumper->dump(
91 1
            $versionNumber,
92 1
            $formatted,
93 1
            $lineLength
94
        );
95
96 1
        $editorCommand = $input->getOption('editor-cmd');
97
98 1
        if ($editorCommand !== null) {
99 1
            $this->procOpen($editorCommand, $path);
100
        }
101
102 1
        $output->writeln([
103 1
            sprintf('Dumped your schema to a new migration class at "<info>%s</info>"', $path),
104 1
            '',
105 1
            sprintf(
106 1
                'To run just this migration for testing purposes, you can use <info>migrations:execute --up %s</info>',
107 1
                $versionNumber
108
            ),
109 1
            '',
110 1
            sprintf(
111 1
                'To revert the migration you can use <info>migrations:execute --down %s</info>',
112 1
                $versionNumber
113
            ),
114 1
            '',
115 1
            'To use this as a rollup migration you can use the <info>migrations:rollup</info> command.',
116
        ]);
117
118 1
        return 0;
119
    }
120
}
121