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

ModuleMakeCommand::handle()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4.0032

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 0
dl 0
loc 24
ccs 16
cts 17
cp 0.9412
crap 4.0032
rs 9.536
c 0
b 0
f 0
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