Completed
Push — symfony3-wololo ( 50a832...8a87df )
by Kamil
18:56
created

AbstractInstallCommand::askHidden()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
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\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Question\Question;
20
use Symfony\Component\Validator\ConstraintViolationList;
21
22
abstract class AbstractInstallCommand extends ContainerAwareCommand
23
{
24
    const WEB_ASSETS_DIRECTORY = 'web/assets/';
25
    const WEB_BUNDLES_DIRECTORY = 'web/bundles/';
26
    const WEB_MEDIA_DIRECTORY = 'web/media/';
27
    const WEB_MEDIA_IMAGE_DIRECTORY = 'web/media/image/';
28
29
    /**
30
     * @var CommandExecutor
31
     */
32
    protected $commandExecutor;
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    protected function initialize(InputInterface $input, OutputInterface $output)
38
    {
39
        $application = $this->getApplication();
40
        $application->setCatchExceptions(false);
41
42
        $this->commandExecutor = new CommandExecutor($input, $output, $application);
0 ignored issues
show
Bug introduced by
It seems like $application defined by $this->getApplication() on line 39 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...
43
    }
44
45
    /**
46
     * @param $id
47
     *
48
     * @return object
49
     */
50
    protected function get($id)
51
    {
52
        return $this->getContainer()->get($id);
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    protected function getEnvironment()
59
    {
60
        return $this->get('kernel')->getEnvironment();
61
    }
62
63
    /**
64
     * @return bool
65
     */
66
    protected function isDebug()
67
    {
68
        return $this->get('kernel')->isDebug();
69
    }
70
71
    /**
72
     * @param array $headers
73
     * @param array $rows
74
     * @param OutputInterface $output
75
     */
76
    protected function renderTable(array $headers, array $rows, OutputInterface $output)
77
    {
78
        $table = $this->getHelper('table');
79
80
        $table
0 ignored issues
show
Bug introduced by
The method setHeaders() does not seem to exist on object<Symfony\Component...Helper\HelperInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
            ->setHeaders($headers)
82
            ->setRows($rows)
83
            ->render($output);
84
    }
85
86
    /**
87
     * @param OutputInterface $output
88
     * @param int $length
89
     *
90
     * @return ProgressBar
91
     */
92
    protected function createProgressBar(OutputInterface $output, $length = 10)
93
    {
94
        $progress = new ProgressBar($output);
95
        $progress->setBarCharacter('<info>|</info>');
96
        $progress->setEmptyBarCharacter(' ');
97
        $progress->setProgressCharacter('|');
98
99
        $progress->start($length);
100
101
        return $progress;
102
    }
103
104
    /**
105
     * @param array $commands
106
     * @param InputInterface $input
107
     * @param OutputInterface $output
108
     * @param bool $displayProgress
109
     */
110
    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...
111
    {
112
        if ($displayProgress) {
113
            $progress = $this->createProgressBar($output, count($commands));
114
        }
115
116
        foreach ($commands as $key => $value) {
117
            if (is_string($key)) {
118
                $command = $key;
119
                $parameters = $value;
120
            } else {
121
                $command = $value;
122
                $parameters = [];
123
            }
124
125
            $this->commandExecutor->runCommand($command, $parameters);
126
127
            // PDO does not always close the connection after Doctrine commands.
128
            // See https://github.com/symfony/symfony/issues/11750.
129
            $this->get('doctrine')->getManager()->getConnection()->close();
130
131
            if ($displayProgress) {
132
                $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...
133
            }
134
        }
135
136
        if ($displayProgress) {
137
            $progress->finish();
138
        }
139
    }
140
141
    /**
142
     * @param string $directory
143
     * @param OutputInterface $output
144
     */
145
    protected function ensureDirectoryExistsAndIsWritable($directory, OutputInterface $output)
146
    {
147
        $checker = $this->get('sylius.installer.checker.command_directory');
148
        $checker->setCommandName($this->getName());
149
150
        $checker->ensureDirectoryExists($directory, $output);
151
        $checker->ensureDirectoryIsWritable($directory, $output);
152
    }
153
}
154