Completed
Pull Request — master (#1088)
by
unknown
02:55
created

ModuleMakeCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
dl 0
loc 65
ccs 20
cts 21
cp 0.9524
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getArguments() 0 6 1
A getOptions() 0 8 1
A handle() 0 24 4
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Nwidart\Modules\Console\Command;
6
use Nwidart\Modules\Generators\ModuleGenerator;
7
use Symfony\Component\Console\Input\InputOption;
8
use Nwidart\Modules\Contracts\ActivatorInterface;
9
use Symfony\Component\Console\Input\InputArgument;
10
11
class ModuleMakeCommand extends Command
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'module:make';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Create a new module.';
26
27
    /**
28
     * Execute the console command.
29
     */
30 4
    public function handle(): int
31
    {
32 4
        $names = $this->argument('name');
33 4
        $success = true;
34
35 4
        foreach ($names as $name) {
0 ignored issues
show
Bug introduced by
The expression $names of type string|array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
36 4
            $code = with(new ModuleGenerator($name))
37 4
                ->setFilesystem($this->laravel['files'])
38 4
                ->setModule($this->laravel['modules'])
39 4
                ->setConfig($this->laravel['config'])
40 4
                ->setActivator($this->laravel[ActivatorInterface::class])
41 4
                ->setConsole($this)
42 4
                ->setForce($this->option('force'))
43 4
                ->setPlain($this->option('plain'))
44 4
                ->setActive(!$this->option('disabled'))
45 4
                ->generate();
46
47 4
            if ($code === E_ERROR) {
48
                $success = false;
49
            }
50
        }
51
52 4
        return $success ? 0 : E_ERROR;
53
    }
54
55
    /**
56
     * Get the console command arguments.
57
     *
58
     * @return array
59
     */
60 4
    protected function getArguments()
61
    {
62
        return [
63 4
            ['name', InputArgument::IS_ARRAY, 'The names of modules will be created.'],
64
        ];
65
    }
66
67 4
    protected function getOptions()
68
    {
69
        return [
70 4
            ['plain', 'p', InputOption::VALUE_NONE, 'Generate a plain module (without some resources).'],
71
            ['disabled', 'd', InputOption::VALUE_NONE, 'Do not enable the module at creation.'],
72
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when the module already exists.'],
73
        ];
74
    }
75
}
76