Completed
Push — master ( 18af08...7d327c )
by Fumio
03:56
created

AddonMakeCommand::handle()   B

Complexity

Conditions 8
Paths 25

Size

Total Lines 57
Code Lines 37

Duplication

Lines 4
Ratio 7.02 %

Code Coverage

Tests 33
CRAP Score 8.1458

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 4
loc 57
ccs 33
cts 38
cp 0.8684
rs 7.2648
cc 8
eloc 37
nc 25
nop 3
crap 8.1458

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 MakeCommandTrait;
20
21
    /**
22
     * The console command signature.
23
     *
24
     * @var string
25
     */
26
    protected $signature = 'make:addon
27
        {name : The name of the addon.}
28
        {skeleton? : Skeleton of addon.}
29
        {--namespace= : PHP namespace of addon. Slash OK.}
30
        {--no-namespace : No PHP namespace.}
31
        {--language= : Languages, comma separated.}
32
        {--yes : No confirm.}
33
    ';
34
35
    /**
36
     * The console command description.
37
     *
38
     * @var string
39
     */
40
    protected $description = 'Create a new addon directory';
41
42
    /**
43
     * @var array
44
     */
45
    protected $skeletons = [
46
        1 => 'minimum',
47
        2 => 'simple',
48
        3 => 'library',
49
        4 => 'api',
50
        5 => 'ui',
51
        6 => 'debug',
52
        7 => 'generator',
53
        8 => 'laravel5',
54
        9 => 'sample:ui',
55
        10 => 'sample:auth',
56
    ];
57
58
    /**
59
     * @var string
60
     */
61
    protected $default_skeleton = 'sample:ui';
62
63
    /**
64
     * Execute the console command.
65
     *
66
     * @param \Jumilla\Addomnipot\Laravel\Addons\AddonGenerator $generator
67
     *
68
     * @return mixed
69
     */
70 3
    public function handle(Filesystem $filesystem, AddonEnvironment $env, AddonGenerator $generator)
71
    {
72 3
        $addon_name = preg_replace('#(/+)#', '-', $this->argument('name'));
73
74 3
        $output_path = $env->path($addon_name);
0 ignored issues
show
Bug introduced by
It seems like $addon_name defined by preg_replace('#(/+)#', '...this->argument('name')) on line 72 can also be of type array<integer,string>; however, Jumilla\Addomnipot\Laravel\Environment::path() 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...
75
76 3
        if ($filesystem->exists($output_path)) {
77
            throw new UnexpectedValueException("addon directory '{$addon_name}' is already exists.");
78
        }
79
80 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...
81
82 3
        if ($this->option('no-namespace')) {
83 1
            $namespace = '';
84 1
        } else {
85 2
            if ($this->option('namespace')) {
86 1
                $namespace = str_replace('/', '\\', $this->option('namespace'));
87 1
                $namespace = studly_case(preg_replace('/[^\w_\\\\]/', '', $namespace));
0 ignored issues
show
Bug introduced by
It seems like preg_replace('/[^\\w_\\\\]/', '', $namespace) targeting preg_replace() can also be of type array<integer,string>; however, studly_case() 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...
88 1
            } else {
89 1
                $namespace = 'App\\'.studly_case(preg_replace('/[^\w_]/', '', $addon_name));
0 ignored issues
show
Bug introduced by
It seems like preg_replace('/[^\\w_]/', '', $addon_name) targeting preg_replace() can also be of type array<integer,string>; however, studly_case() 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...
90
            }
91
92 2
            $namespace = preg_replace('/^(\d)/', '_$1', $namespace);
93
        }
94 3
        $languages = $this->option('language') ? explode($this->option('language')) : [];
95
96
        $properties = [
97 3
            'addon_name' => preg_replace('/[^\w_]/', '', $addon_name),
98 3
            'addon_class' => preg_replace(
99 3
                ['/[^\w_]/', '/^(\d)/'],
100 3
                ['', '_$1'],
101 3
                studly_case($addon_name)
0 ignored issues
show
Bug introduced by
It seems like $addon_name defined by preg_replace('#(/+)#', '...this->argument('name')) on line 72 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...
102 3
            ),
103 3
            'namespace' => $namespace,
104 3
            'languages' => array_unique(array_merge(['en', $this->laravel['config']->get('app.locale')], $languages)),
105 3
        ];
106
107
        // confirm
108 3
        $this->line('Addon name: '.$properties['addon_name']);
109 3
        $this->line('PHP namespace: '.$properties['namespace']);
110 3
        $this->line('Skeleton: '.$skeleton);
111 3
        $this->line('Languages: '.implode(', ', $properties['languages']));
112
113 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...
114
            $this->comment('canceled');
115
            return;
116
        }
117
118
        try {
119 3
            $generator->generateAddon($output_path, str_replace(':', '-', $skeleton), $properties);
120 3
            $this->info('Addon Generated.');
121 3
        } catch (Exception $ex) {
122
            $filesystem->deleteDirectory($output_path);
123
124
            throw $ex;
125
        }
126 3
    }
127
}
128