Completed
Pull Request — master (#1023)
by Asmir
02:45 queued 16s
created

DoctrineCommand::initialize()   C

Complexity

Conditions 13
Paths 36

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 13.0976

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 6.6166
c 0
b 0
f 0
ccs 11
cts 12
cp 0.9167
cc 13
nc 36
nop 2
crap 13.0976

How to fix   Complexity   

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\Configuration\Connection\ConfigurationFile;
8
use Doctrine\Migrations\Configuration\Connection\ConnectionRegistryConnection;
9
use Doctrine\Migrations\Configuration\EntityManager\ManagerRegistryEntityManager;
10
use Doctrine\Migrations\Configuration\Migration\ConfigurationFileWithFallback;
11
use Doctrine\Migrations\DependencyFactory;
12
use Doctrine\Migrations\Tools\Console\ConsoleLogger;
13
use Doctrine\Migrations\Tools\Console\Exception\DependenciesNotSatisfied;
14
use Doctrine\Migrations\Tools\Console\Exception\InvalidOptionUsage;
15
use Psr\Log\LoggerInterface;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\Console\Style\StyleInterface;
21
use Symfony\Component\Console\Style\SymfonyStyle;
22
use function is_string;
23
24
/**
25
 * The DoctrineCommand class provides base functionality for the other migrations commands to extend from.
26
 */
27
abstract class DoctrineCommand extends Command
28
{
29
    /** @var DependencyFactory|null */
30
    private $dependencyFactory;
31
32 71
    /** @var StyleInterface */
33
    protected $io;
34 71
35 71
    public function __construct(?DependencyFactory $dependencyFactory = null, ?string $name = null)
36 71
    {
37
        parent::__construct($name);
38 71
        $this->dependencyFactory = $dependencyFactory;
39
    }
40 71
41 71
    protected function configure() : void
42 71
    {
43 71
        $this->addOption(
44 71
            'configuration',
45
            null,
46
            InputOption::VALUE_REQUIRED,
47 71
            'The path to a migrations configuration file. <comment>[default: any of migrations.{php,xml,json,yml,yaml}]</comment>'
48
        );
49
50
        $this->addOption(
51 71
            'em',
52 71
            null,
53 71
            InputOption::VALUE_REQUIRED,
54 71
            'The name of the connection to use (available only with ManagerRegistryEntityManager loader).'
55 71
        );
56 71
57
        $this->addOption(
58 71
            'conn',
59
            null,
60 71
            InputOption::VALUE_REQUIRED,
61
            'The name of the entity manager to use (available only with ConnectionRegistryConnection loader).'
62 71
        );
63
64 71
        if ($this->dependencyFactory !== null) {
65 71
            return;
66
        }
67
68
        $this->addOption(
69
            'db-configuration',
70
            null,
71
            InputOption::VALUE_REQUIRED,
72
            'The path to a database connection configuration file.',
73 71
            'migrations-db.php'
74 1
        );
75 1
    }
76
77
    protected function initialize(InputInterface $input, OutputInterface $output) : void
78 71
    {
79 26
        $this->io = new SymfonyStyle($input, $output);
80
81
        $configurationParameter = $input->getOption('configuration');
82 45
        if ($this->dependencyFactory === null) {
83 45
            $configurationLoader     = new ConfigurationFileWithFallback(
84 45
                is_string($configurationParameter)
85 45
                    ? $configurationParameter
86
                    : null
87 65
            );
88
            $connectionLoader        = new ConfigurationFile((string) $input->getOption('db-configuration'));
89 65
            $this->dependencyFactory = DependencyFactory::fromConnection($configurationLoader, $connectionLoader);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Doctrine\Migrations\Dep...der, $connectionLoader) of type object<self> is incompatible with the declared type object<Doctrine\Migratio...DependencyFactory>|null of property $dependencyFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
90
        } elseif (is_string($configurationParameter)) {
91
            $configurationLoader = new ConfigurationFileWithFallback($configurationParameter);
92
            $this->dependencyFactory->setConfigurationLoader($configurationLoader);
93 65
        }
94
95
        if ($input->getOption('em')!== null && $input->getOption('conn')!==null) {
96 32
            throw new InvalidOptionUsage('You can specify only one of the --em and --conn options.');
97
        }
98 32
99
        $connectionLoader = $this->dependencyFactory->getConnectionLoader();
100
        if ($connectionLoader instanceof ConnectionRegistryConnection && $input->getOption('conn')!==null) {
101
            $connectionName = $input->getOption('conn');
102
            $connectionLoader->setConnectionName((string) $connectionName);
103
        } elseif ($input->getOption('conn')!==null) {
104
            throw new InvalidOptionUsage('You can use the --conn option only if the connection loader is of type ConnectionRegistryConnection.');
105
        }
106
107
        $connectionLoader = $this->dependencyFactory->getEntityManagerLoader();
108
        if ($connectionLoader instanceof ManagerRegistryEntityManager && $input->getOption('em')!==null) {
109
            $emName = $input->getOption('em');
110
            $connectionLoader->setManagerName((string) $emName);
111
        } elseif ($input->getOption('em')!==null) {
112
            throw new InvalidOptionUsage('You can use the --em option only if the entity loader is of type ManagerRegistryEntityManager.');
113
        }
114
115
        if ($this->dependencyFactory->isFrozen()) {
116
            return;
117
        }
118
119
        $logger = new ConsoleLogger($output);
120
        $this->dependencyFactory->setService(LoggerInterface::class, $logger);
0 ignored issues
show
Documentation introduced by
$logger is of type object<Doctrine\Migratio...\Console\ConsoleLogger>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121
        $this->dependencyFactory->freeze();
122
    }
123
124
    protected function getDependencyFactory() : DependencyFactory
125
    {
126
        if ($this->dependencyFactory === null) {
127
            throw DependenciesNotSatisfied::new();
128
        }
129
130
        return $this->dependencyFactory;
131
    }
132
133
    protected function canExecute(string $question, InputInterface $input) : bool
134
    {
135
        return ! $input->isInteractive() || $this->io->confirm($question);
136
    }
137
}
138