Completed
Push — master ( cfaf77...b5ed7c )
by Kamil
47:11 queued 29:42
created

AbstractInstallCommand::proceedAskRequest()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 3
nop 5
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\CoreBundle\Command;
13
14
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
15
use Symfony\Component\Console\Helper\ProgressBar;
16
use Symfony\Component\Console\Helper\QuestionHelper;
17
use Symfony\Component\Console\Helper\Table;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\Console\Question\Question;
21
use Symfony\Component\Validator\ConstraintViolationList;
22
23
abstract class AbstractInstallCommand extends ContainerAwareCommand
24
{
25
    const WEB_ASSETS_DIRECTORY = 'web/assets/';
26
    const WEB_BUNDLES_DIRECTORY = 'web/bundles/';
27
    const WEB_MEDIA_DIRECTORY = 'web/media/';
28
    const WEB_MEDIA_IMAGE_DIRECTORY = 'web/media/image/';
29
30
    /**
31
     * @var CommandExecutor
32
     */
33
    protected $commandExecutor;
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    protected function initialize(InputInterface $input, OutputInterface $output)
39
    {
40
        $application = $this->getApplication();
41
        $application->setCatchExceptions(false);
42
43
        $this->commandExecutor = new CommandExecutor($input, $output, $application);
0 ignored issues
show
Bug introduced by
It seems like $application defined by $this->getApplication() on line 40 can be null; however, Sylius\Bundle\CoreBundle...Executor::__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...
44
    }
45
46
    /**
47
     * @param $id
48
     *
49
     * @return object
50
     */
51
    protected function get($id)
52
    {
53
        return $this->getContainer()->get($id);
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    protected function getEnvironment()
60
    {
61
        return $this->get('kernel')->getEnvironment();
62
    }
63
64
    /**
65
     * @return bool
66
     */
67
    protected function isDebug()
68
    {
69
        return $this->get('kernel')->isDebug();
70
    }
71
72
    /**
73
     * @param array $headers
74
     * @param array $rows
75
     * @param OutputInterface $output
76
     */
77
    protected function renderTable(array $headers, array $rows, OutputInterface $output)
78
    {
79
        $table = new Table($output);
80
81
        $table
82
            ->setHeaders($headers)
83
            ->setRows($rows)
84
            ->render();
85
    }
86
87
    /**
88
     * @param OutputInterface $output
89
     * @param int $length
90
     *
91
     * @return ProgressBar
92
     */
93
    protected function createProgressBar(OutputInterface $output, $length = 10)
94
    {
95
        $progress = new ProgressBar($output);
96
        $progress->setBarCharacter('<info>|</info>');
97
        $progress->setEmptyBarCharacter(' ');
98
        $progress->setProgressCharacter('|');
99
100
        $progress->start($length);
101
102
        return $progress;
103
    }
104
105
    /**
106
     * @param array $commands
107
     * @param InputInterface $input
108
     * @param OutputInterface $output
109
     * @param bool $displayProgress
110
     */
111
    protected function runCommands(array $commands, InputInterface $input, OutputInterface $output, $displayProgress = true)
0 ignored issues
show
Unused Code introduced by
The parameter $input is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
    {
113
        if ($displayProgress) {
114
            $progress = $this->createProgressBar($output, count($commands));
115
        }
116
117
        foreach ($commands as $key => $value) {
118
            if (is_string($key)) {
119
                $command = $key;
120
                $parameters = $value;
121
            } else {
122
                $command = $value;
123
                $parameters = [];
124
            }
125
126
            $this->commandExecutor->runCommand($command, $parameters);
127
128
            // PDO does not always close the connection after Doctrine commands.
129
            // See https://github.com/symfony/symfony/issues/11750.
130
            $this->get('doctrine')->getManager()->getConnection()->close();
131
132
            if ($displayProgress) {
133
                $progress->advance();
0 ignored issues
show
Bug introduced by
The variable $progress does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
134
            }
135
        }
136
137
        if ($displayProgress) {
138
            $progress->finish();
139
        }
140
    }
141
142
    /**
143
     * @param string $directory
144
     * @param OutputInterface $output
145
     */
146
    protected function ensureDirectoryExistsAndIsWritable($directory, OutputInterface $output)
147
    {
148
        $checker = $this->get('sylius.installer.checker.command_directory');
149
        $checker->setCommandName($this->getName());
150
151
        $checker->ensureDirectoryExists($directory, $output);
152
        $checker->ensureDirectoryIsWritable($directory, $output);
153
    }
154
}
155