Completed
Pull Request — master (#1163)
by
unknown
09:19
created

ModuleMakeCommand   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 94
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 29 7
A getArguments() 0 6 1
A getOptions() 0 10 1
A getModuleType() 0 16 5
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Console\Command;
6
use Nwidart\Modules\Contracts\ActivatorInterface;
7
use Nwidart\Modules\Generators\DatabaseModuleGenerator;
8
use Nwidart\Modules\Generators\ModuleGenerator;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputOption;
11
12
class ModuleMakeCommand extends Command
13
{
14
    /**
15
     * The console command name.
16
     *
17
     * @var string
18
     */
19
    protected $name = 'module:make';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Create a new module.';
27
28
    /**
29
     * Execute the console command.
30 130
     */
31
    public function handle() : int
32 130
    {
33 130
        $names = $this->argument('name');
34
35 130
        if (empty($names) || !is_array($names)) {
36 130
            $this->error('The names of modules are required');
37 130
            return 1;
38 130
        }
39 130
40 130
        foreach ($names as $name) {
41 130
            $generator = config('modules.database_management') ? new DatabaseModuleGenerator($name) : new ModuleGenerator($name);
42 130
            $code = with($generator)
43 130
                ->setFilesystem($this->laravel['files'])
44 130
                ->setModule($this->laravel['modules'])
45 130
                ->setConfig($this->laravel['config'])
46
                ->setActivator($this->laravel[ActivatorInterface::class])
47 130
                ->setConsole($this)
48 1
                ->setForce($this->option('force'))
49
                ->setType($this->getModuleType())
50
                ->setActive(!$this->option('disabled'))
51
                ->generate();
52 130
53
            if ($code === E_ERROR) {
54
                $success = false;
55
            }
56
        }
57
58
        return $success ? 0 : E_ERROR;
0 ignored issues
show
Bug introduced by
The variable $success 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...
59
    }
60 130
61
    /**
62
     * Get the console command arguments.
63 130
     *
64
     * @return array
65
     */
66
    protected function getArguments()
67 130
    {
68
        return [
69
            ['name', InputArgument::IS_ARRAY, 'The names of modules will be created.'],
70 130
        ];
71
    }
72
73
    protected function getOptions()
74
    {
75
        return [
76
            ['plain', 'p', InputOption::VALUE_NONE, 'Generate a plain module (without some resources).'],
77
            ['api', null, InputOption::VALUE_NONE, 'Generate an api module.'],
78
            ['web', null, InputOption::VALUE_NONE, 'Generate a web module.'],
79
            ['disabled', 'd', InputOption::VALUE_NONE, 'Do not enable the module at creation.'],
80
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when the module already exists.'],
81
        ];
82
    }
83
84
    /**
85
    * Get module type .
86
    *
87
    * @return string
88
    */
89
    private function getModuleType()
90
    {
91
        $isPlain = $this->option('plain');
92
        $isApi = $this->option('api');
93
94
        if ($isPlain && $isApi) {
95
            return 'web';
96
        }
97
        if ($isPlain) {
98
            return 'plain';
99
        } elseif ($isApi) {
100
            return 'api';
101
        } else {
102
            return 'web';
103
        }
104
    }
105
}
106