ChooseInstallationFolder::execute()   C
last analyzed

Complexity

Conditions 8
Paths 2

Size

Total Lines 49
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 49
rs 6.1403
cc 8
eloc 30
nc 2
nop 0
1
<?php
2
3
namespace N98\Magento\Command\Installer\SubCommand;
4
5
use N98\Magento\Command\SubCommand\AbstractSubCommand;
6
7
class ChooseInstallationFolder extends AbstractSubCommand
8
{
9
    /**
10
     * @return bool
11
     */
12
    public function execute()
0 ignored issues
show
Coding Style introduced by
execute uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
13
    {
14
        $input = $this->input;
15
        $validateInstallationFolder = function ($folderName) use ($input) {
16
            $folderName = rtrim(trim($folderName, ' '), '/');
17
            if (substr($folderName, 0, 1) == '.') {
18
                $cwd = \getcwd();
19
                if (empty($cwd) && isset($_SERVER['PWD'])) {
20
                    $cwd = $_SERVER['PWD'];
21
                }
22
                $folderName = $cwd . substr($folderName, 1);
23
            }
24
25
            if (empty($folderName)) {
26
                throw new \InvalidArgumentException('Installation folder cannot be empty');
27
            }
28
29
            if (!is_dir($folderName)) {
30
                if (!@mkdir($folderName, 0777, true)) {
31
                    throw new \InvalidArgumentException('Cannot create folder.');
32
                }
33
34
                return $folderName;
35
            }
36
37
            return $folderName;
38
        };
39
40
        if (($installationFolder = $input->getOption('installationFolder')) == null) {
41
            $defaultFolder = './magento';
42
            $question[] = "<question>Enter installation folder:</question> [<comment>" . $defaultFolder . "</comment>]";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$question was never initialized. Although not strictly required by PHP, it is generally a good practice to add $question = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
43
44
            $installationFolder = $this->getCommand()->getHelper('dialog')->askAndValidate(
45
                $this->output,
46
                $question,
47
                $validateInstallationFolder,
48
                false,
49
                $defaultFolder
50
            );
51
        } else {
52
            // @Todo improve validation and bring it to 1 single function
53
            $installationFolder = $validateInstallationFolder($installationFolder);
54
        }
55
56
        $this->config->setString('installationFolder', realpath($installationFolder));
0 ignored issues
show
Documentation introduced by
realpath($installationFolder) is of type string, but the function expects a boolean.

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...
57
        \chdir($this->config->getString('installationFolder'));
58
59
        return true;
60
    }
61
}
62