Completed
Push — master ( 78b3b4...1185ef )
by Fumio
02:21
created

AddonMakeCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 125
Duplicated Lines 3.2 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 85%

Importance

Changes 0
Metric Value
dl 4
loc 125
ccs 34
cts 40
cp 0.85
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 6

1 Method

Rating   Name   Duplication   Size   Complexity  
C handle() 4 68 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Jumilla\Addomnipot\Laravel\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
use Jumilla\Addomnipot\Laravel\Environment as AddonEnvironment;
8
use Jumilla\Addomnipot\Laravel\Generator as AddonGenerator;
9
use UnexpectedValueException;
10
use Exception;
11
12
/**
13
 * Modules console commands.
14
 *
15
 * @author Fumio Furukawa <[email protected]>
16
 */
17
class AddonMakeCommand extends Command
18
{
19
    use Functions;
20
    use MakeCommandTrait;
21
22
    /**
23
     * The console command signature.
24
     *
25
     * @var string
26
     */
27
    protected $signature = 'make:addon
28
        {name : The name of the addon.}
29
        {skeleton? : Skeleton of addon.}
30
        {--space= : Addons space.}
31
        {--namespace= : PHP namespace of addon. Slash OK.}
32
        {--no-namespace : No PHP namespace.}
33
        {--language= : Languages, comma separated.}
34
        {--yes : No confirm.}
35
    ';
36
37
    /**
38
     * The console command description.
39
     *
40
     * @var string
41
     */
42
    protected $description = 'Create a new addon directory';
43
44
    /**
45
     * @var array
46
     */
47
    protected $skeletons = [
48
        1 => 'minimum',
49
        2 => 'simple',
50
        3 => 'asset',
51
        4 => 'library',
52
        5 => 'api',
53
        6 => 'ui',
54
        11 => 'ui-sample',
55
        12 => 'debug',
56
        13 => 'generator',
57
        14 => 'laravel5',
58
        15 => 'laravel5-auth',
59
    ];
60
61
    /**
62
     * @var string
63
     */
64
    protected $default_skeleton = 'ui-sample';
65
66
    /**
67
     * Execute the console command.
68
     *
69
     * @param \Jumilla\Addomnipot\Laravel\Addons\AddonGenerator $generator
70
     *
71
     * @return mixed
72
     */
73 3
    public function handle(Filesystem $filesystem, AddonEnvironment $env, AddonGenerator $generator)
74
    {
75 3
        $addon_name = preg_replace('#(/+)#', '-', $this->argument('name'));
76
77
        // Check addon-directory
78 3
        if ($env->exists($addon_name)) {
0 ignored issues
show
Bug introduced by
It seems like $addon_name defined by preg_replace('#(/+)#', '...this->argument('name')) on line 75 can also be of type array<integer,string>; however, Jumilla\Addomnipot\Laravel\Environment::exists() 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...
79
            throw new UnexpectedValueException("addon directory '{$addon_name}' is already exists.");
80
        }
81
82 3
        $output_path = $env->spacePath($this->option('space'), $addon_name);
0 ignored issues
show
Bug introduced by
It seems like $this->option('space') targeting Illuminate\Console\Command::option() can also be of type array; however, Jumilla\Addomnipot\Larav...nvironment::spacePath() 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...
Bug introduced by
It seems like $addon_name defined by preg_replace('#(/+)#', '...this->argument('name')) on line 75 can also be of type array<integer,string>; however, Jumilla\Addomnipot\Larav...nvironment::spacePath() 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...
83
84
        // Adjust addon_name
85 3
        $addon_name = preg_replace('/[^\w_\-]/', '', $addon_name);
86
87 3
        $addon_class = preg_replace(
88 3
            ['/[^\w_]/', '/^(\d)/'],
89 3
            ['', '_$1'],
90 3
            studly_case($addon_name)
0 ignored issues
show
Bug introduced by
It seems like $addon_name defined by preg_replace('/[^\\w_\\-]/', '', $addon_name) on line 85 can also be of type array<integer,string>; however, studly_case() 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...
91 3
        );
92
93
        // namespace
94 3
        if ($this->option('no-namespace')) {
95 1
            $namespace = '';
96 1
        } else {
97 2
            if ($this->option('namespace')) {
98 1
                $namespace = str_replace('/', '\\', $this->option('namespace'));
99 1
            } else {
100 1
                $namespace = 'App\\'.$addon_class;
101
            }
102
103 2
            if (! $this->validPhpNamespace($namespace)) {
104
                throw new UnexpectedValueException("PHP namespace '{$namespace}' is invalid.");
105
            }
106
        }
107
108
        // languages
109 3
        $languages = $this->option('language') ? explode($this->option('language')) : [];
110
111
        // Show select prompt if not specified
112 3
        $skeleton = $this->chooseSkeleton($this->argument('skeleton'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('skeleton') targeting Illuminate\Console\Command::argument() can also be of type array; however, Jumilla\Addomnipot\Larav...Trait::chooseSkeleton() 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...
113
114
        $properties = [
115 3
            'addon_name' => $addon_name,
116 3
            'addon_class' => $addon_class,
117 3
            'namespace' => $namespace,
118 3
            'languages' => array_unique(array_merge(['en', $this->laravel['config']->get('app.locale')], $languages)),
119 3
        ];
120
121
        // confirm
122 3
        $this->line('Addon name: '.$properties['addon_name']);
123 3
        $this->line('PHP namespace: '.$properties['namespace']);
124 3
        $this->line('Skeleton: '.$skeleton);
125 3
        $this->line('Languages: '.implode(', ', $properties['languages']));
126
127 3 View Code Duplication
        if (!$this->option('yes') && !$this->confirm('generate ready? [Y/n]', true)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
            $this->comment('canceled');
129
            return;
130
        }
131
132
        try {
133 3
            $generator->generateAddon($output_path, str_replace(':', '-', $skeleton), $properties);
134 3
            $this->info('Addon Generated.');
135 3
        } catch (Exception $ex) {
136
            $filesystem->deleteDirectory($output_path);
137
138
            throw $ex;
139
        }
140 3
    }
141
}
142