Completed
Pull Request — master (#322)
by Mathieu
03:43
created

InstallCommand   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 136
rs 10
c 0
b 0
f 0
ccs 7
cts 7
cp 1
wmc 14
lcom 1
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A installFromFile() 0 22 3
B install() 0 29 6
A getArguments() 0 7 1
A getOptions() 0 10 1
A handle() 0 15 2
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Console\Command;
6
use Nwidart\Modules\Json;
7
use Nwidart\Modules\Process\Installer;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputOption;
10
11
class InstallCommand extends Command
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'module:install';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Install the specified module by given package name (vendor/name).';
26
27
    /**
28
     * Create a new command instance.
29
     */
30 56
    public function __construct()
31
    {
32 56
        parent::__construct();
33 56
    }
34
35
    /**
36
     * Execute the console command.
37
     */
38
    public function handle()
39
    {
40
        if (is_null($this->argument('name'))) {
41
            $this->installFromFile();
42
43
            return;
44
        }
45
46
        $this->install(
47
            $this->argument('name'),
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') targeting Illuminate\Console\Command::argument() can also be of type array; however, Nwidart\Modules\Commands\InstallCommand::install() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
48
            $this->argument('version'),
0 ignored issues
show
Bug introduced by
It seems like $this->argument('version') targeting Illuminate\Console\Command::argument() can also be of type array; however, Nwidart\Modules\Commands\InstallCommand::install() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
49
            $this->option('type'),
0 ignored issues
show
Bug introduced by
It seems like $this->option('type') targeting Illuminate\Console\Command::option() can also be of type array; however, Nwidart\Modules\Commands\InstallCommand::install() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
50
            $this->option('tree')
0 ignored issues
show
Documentation introduced by
$this->option('tree') is of type string|array, 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...
51
        );
52
    }
53
54
    /**
55
     * Install modules from modules.json file.
56
     */
57
    protected function installFromFile()
58
    {
59
        if (!file_exists($path = base_path('modules.json'))) {
60
            $this->error("File 'modules.json' does not exist in your project root.");
61
62
            return;
63
        }
64
65
        $modules = Json::make($path);
66
67
        $dependencies = $modules->get('require', []);
68
69
        foreach ($dependencies as $module) {
70
            $module = collect($module);
71
72
            $this->install(
73
                $module->get('name'),
74
                $module->get('version'),
75
                $module->get('type')
76
            );
77
        }
78
    }
79
80
    /**
81
     * Install the specified module.
82
     *
83
     * @param string $name
84
     * @param string $version
85
     * @param string $type
86
     * @param bool   $tree
87
     */
88
    protected function install($name, $version = 'dev-master', $type = 'composer', $tree = false)
89
    {
90
        $installer = new Installer(
91
            $name,
92
            $version,
93
            $type ?: $this->option('type'),
0 ignored issues
show
Bug introduced by
It seems like $type ?: $this->option('type') can also be of type array; however, Nwidart\Modules\Process\Installer::__construct() does only seem to accept string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
94
            $tree ?: $this->option('tree')
95
        );
96
97
        $installer->setRepository($this->laravel['modules']);
98
99
        $installer->setConsole($this);
100
101
        if ($timeout = $this->option('timeout')) {
102
            $installer->setTimeout($timeout);
0 ignored issues
show
Documentation introduced by
$timeout is of type string|array, but the function expects a integer.

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...
103
        }
104
105
        if ($path = $this->option('path')) {
106
            $installer->setPath($path);
0 ignored issues
show
Bug introduced by
It seems like $path defined by $this->option('path') on line 105 can also be of type array; however, Nwidart\Modules\Process\Installer::setPath() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
107
        }
108
109
        $installer->run();
110
111
        if (!$this->option('no-update')) {
112
            $this->call('module:update', [
113
                'module' => $installer->getModuleName(),
114
            ]);
115
        }
116
    }
117
118
    /**
119
     * Get the console command arguments.
120
     *
121
     * @return array
122
     */
123 56
    protected function getArguments()
124
    {
125
        return array(
126 56
            array('name', InputArgument::OPTIONAL, 'The name of module will be installed.'),
127
            array('version', InputArgument::OPTIONAL, 'The version of module will be installed.'),
128
        );
129
    }
130
131
    /**
132
     * Get the console command options.
133
     *
134
     * @return array
135
     */
136 56
    protected function getOptions()
137
    {
138
        return array(
139 56
            array('timeout', null, InputOption::VALUE_OPTIONAL, 'The process timeout.', null),
140
            array('path', null, InputOption::VALUE_OPTIONAL, 'The installation path.', null),
141
            array('type', null, InputOption::VALUE_OPTIONAL, 'The type of installation.', null),
142
            array('tree', null, InputOption::VALUE_NONE, 'Install the module as a git subtree', null),
143
            array('no-update', null, InputOption::VALUE_NONE, 'Disables the automatic update of the dependencies.', null),
144
        );
145
    }
146
}
147