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

GenerateCommand::execute()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 4.0275

Importance

Changes 0
Metric Value
cc 4
eloc 24
c 0
b 0
f 0
nc 6
nop 2
dl 0
loc 39
ccs 22
cts 25
cp 0.88
crap 4.0275
rs 9.536
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console\Command;
6
7
use Exception;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use function assert;
12
use function is_string;
13
use function key;
14
use function sprintf;
15
16
/**
17
 * The GenerateCommand class is responsible for generating a blank migration class for you to modify to your needs.
18
 */
19
final class GenerateCommand extends DoctrineCommand
20
{
21
    /** @var string */
22
    protected static $defaultName = 'migrations:generate';
23
24 1
    protected function configure() : void
25
    {
26
        $this
27 1
            ->setAliases(['generate'])
28 1
            ->setDescription('Generate a blank migration class.')
29 1
            ->addOption(
30 1
                'namespace',
31 1
                null,
32 1
                InputOption::VALUE_REQUIRED,
33 1
                'The namespace to use for the migration (must be in the list of configured namespaces)'
34
            )
35 1
            ->setHelp(<<<EOT
36 1
The <info>%command.name%</info> command generates a blank migration class:
37
38
    <info>%command.full_name%</info>
39
40
EOT
41
            );
42
43 1
        parent::configure();
44 1
    }
45
46 1
    protected function execute(InputInterface $input, OutputInterface $output) : int
47
    {
48 1
        $configuration = $this->getDependencyFactory()->getConfiguration();
49
50 1
        $migrationGenerator = $this->getDependencyFactory()->getMigrationGenerator();
51
52 1
        $namespace = $input->getOption('namespace');
53 1
        if ($namespace === '') {
54
            $namespace = null;
55
        }
56
57 1
        $dirs = $configuration->getMigrationDirectories();
58 1
        if ($namespace === null) {
59 1
            $namespace = key($dirs);
60
        } elseif (! isset($dirs[$namespace])) {
61
            throw new Exception(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

61
            throw new Exception(sprintf('Path not defined for the namespace %s', /** @scrutinizer ignore-type */ $namespace));
Loading history...
62
        }
63
64 1
        assert(is_string($namespace));
65
66 1
        $fqcn = $this->getDependencyFactory()->getClassNameGenerator()->generateClassName($namespace);
67
68 1
        $path = $migrationGenerator->generateMigration($fqcn);
69
70 1
        $output->writeln([
71 1
            sprintf('Generated new migration class to "<info>%s</info>"', $path),
72 1
            '',
73 1
            sprintf(
74 1
                'To run just this migration for testing purposes, you can use <info>migrations:execute --up \'%s\'</info>',
75 1
                $fqcn
76
            ),
77 1
            '',
78 1
            sprintf(
79 1
                'To revert the migration you can use <info>migrations:execute --down \'%s\'</info>',
80 1
                $fqcn
81
            ),
82
        ]);
83
84 1
        return 0;
85
    }
86
}
87