DoctrineCommand::initialize()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.7873

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 13
cts 19
cp 0.6842
rs 9.1928
c 0
b 0
f 0
cc 5
nc 6
nop 2
crap 5.7873
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\Migration\ConfigurationFileWithFallback;
9
use Doctrine\Migrations\DependencyFactory;
10
use Doctrine\Migrations\Tools\Console\ConsoleLogger;
11
use Doctrine\Migrations\Tools\Console\Exception\DependenciesNotSatisfied;
12
use Psr\Log\LoggerInterface;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Style\StyleInterface;
18
use Symfony\Component\Console\Style\SymfonyStyle;
19
use function is_string;
20
21
/**
22
 * The DoctrineCommand class provides base functionality for the other migrations commands to extend from.
23
 */
24
abstract class DoctrineCommand extends Command
25
{
26
    /** @var DependencyFactory|null */
27
    private $dependencyFactory;
28
29
    /** @var StyleInterface */
30
    protected $io;
31
32 71
    public function __construct(?DependencyFactory $dependencyFactory = null, ?string $name = null)
33
    {
34 71
        parent::__construct($name);
35 71
        $this->dependencyFactory = $dependencyFactory;
36 71
    }
37
38 71
    protected function configure() : void
39
    {
40 71
        $this->addOption(
41 71
            'configuration',
42 71
            null,
43 71
            InputOption::VALUE_REQUIRED,
44 71
            'The path to a migrations configuration file. <comment>[default: any of migrations.{php,xml,json,yml,yaml}]</comment>'
45
        );
46
47 71
        if ($this->dependencyFactory !== null) {
48
            return;
49
        }
50
51 71
        $this->addOption(
52 71
            'db-configuration',
53 71
            null,
54 71
            InputOption::VALUE_REQUIRED,
55 71
            'The path to a database connection configuration file.',
56 71
            'migrations-db.php'
57
        );
58 71
    }
59
60 71
    protected function initialize(InputInterface $input, OutputInterface $output) : void
61
    {
62 71
        $this->io = new SymfonyStyle($input, $output);
63
64 71
        $configurationParameter = $input->getOption('configuration');
65 71
        if ($this->dependencyFactory === null) {
66
            $configurationLoader     = new ConfigurationFileWithFallback(
67
                is_string($configurationParameter)
68
                    ? $configurationParameter
69
                    : null
70
            );
71
            $connectionLoader        = new ConfigurationFile((string) $input->getOption('db-configuration'));
72
            $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...
73 71
        } elseif (is_string($configurationParameter)) {
74 1
            $configurationLoader = new ConfigurationFileWithFallback($configurationParameter);
75 1
            $this->dependencyFactory->setConfigurationLoader($configurationLoader);
76
        }
77
78 71
        if ($this->dependencyFactory->isFrozen()) {
79 26
            return;
80
        }
81
82 45
        $logger = new ConsoleLogger($output);
83 45
        $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...
84 45
        $this->dependencyFactory->freeze();
85 45
    }
86
87 65
    protected function getDependencyFactory() : DependencyFactory
88
    {
89 65
        if ($this->dependencyFactory === null) {
90
            throw DependenciesNotSatisfied::new();
91
        }
92
93 65
        return $this->dependencyFactory;
94
    }
95
96 32
    protected function canExecute(string $question, InputInterface $input) : bool
97
    {
98 32
        return ! $input->isInteractive() || $this->io->confirm($question);
99
    }
100
}
101