Completed
Push — master ( 2044f4...a35418 )
by Loïc
13s queued 11s
created

InstallDatabaseCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
nc 1
cc 1
nop 0
1
<?php
2
3
namespace App\Command\Installer;
4
5
use App\Command\Helper\CommandsRunner;
6
use App\Installer\Provider\DatabaseSetupCommandsProviderInterface;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Style\SymfonyStyle;
11
12
class InstallDatabaseCommand extends Command
13
{
14
    /** @var DatabaseSetupCommandsProviderInterface */
15
    private $databaseSetupCommandsProvider;
16
17
    /** @var CommandsRunner */
18
    private $commandsRunner;
19
20
    /** @var string */
21
    private $environment;
22
23
    public function __construct(
24
        DatabaseSetupCommandsProviderInterface $databaseSetupCommandsProvider,
25
        CommandsRunner $commandsRunner,
26
        string $environment
27
    ) {
28
        $this->databaseSetupCommandsProvider = $databaseSetupCommandsProvider;
29
        $this->commandsRunner = $commandsRunner;
30
        $this->environment = $environment;
31
32
        parent::__construct();
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    protected function configure()
39
    {
40
        $this
41
            ->setName('app:install:database')
42
            ->setDescription('Install AppName database.')
43
            ->setHelp(<<<EOT
44
The <info>%command.name%</info> command creates AppName database.
45
EOT
46
            )
47
        ;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     *
53
     * @throws \Exception
54
     */
55
    protected function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        $outputStyle = new SymfonyStyle($input, $output);
58
        $outputStyle->writeln(sprintf(
59
            'Creating AppName database for environment <info>%s</info>.',
60
            $this->environment
61
        ));
62
        $commands = $this
63
            ->databaseSetupCommandsProvider
64
            ->getCommands($input, $output, $this->getHelper('question'))
0 ignored issues
show
Compatibility introduced by
$this->getHelper('question') of type object<Symfony\Component...Helper\HelperInterface> is not a sub-type of object<Symfony\Component...\Helper\QuestionHelper>. It seems like you assume a concrete implementation of the interface Symfony\Component\Console\Helper\HelperInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
65
        ;
66
        $this->commandsRunner->run($commands, $input, $output, $this->getApplication());
0 ignored issues
show
Bug introduced by
It seems like $this->getApplication() can be null; however, run() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
67
        $outputStyle->newLine();
68
        $commandExecutor = new CommandExecutor($input, $output, $this->getApplication());
0 ignored issues
show
Bug introduced by
It seems like $this->getApplication() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
69
        $commandExecutor->runCommand('app:install:sample-data', [], $output);
70
    }
71
}
72