Completed
Push — master ( 880a30...6b3c1e )
by Fumio
02:31
created

AddonMakeCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 90.32%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
c 2
b 0
f 1
lcom 1
cbo 5
dl 0
loc 98
ccs 28
cts 31
cp 0.9032
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C handle() 0 45 7
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
    ';
33
34
    /**
35
     * The console command description.
36
     *
37
     * @var string
38
     */
39
    protected $description = 'Create a new addon directory';
40
41
    /**
42
     * @var array
43
     */
44
    protected $skeletons = [
45
        1 => 'minimum',
46
        2 => 'simple',
47
        3 => 'library',
48
        4 => 'api',
49
        5 => 'ui',
50
        6 => 'debug',
51
        7 => 'generator',
52
        8 => 'laravel5',
53
        9 => 'sample:ui',
54
        10 => 'sample:auth',
55
    ];
56
57
    /**
58
     * @var string
59
     */
60
    protected $default_skeleton = 'sample:ui';
61
62
    /**
63
     * Execute the console command.
64
     *
65
     * @param \Jumilla\Addomnipot\Laravel\Addons\AddonGenerator $generator
66
     *
67
     * @return mixed
68
     */
69 3
    public function handle(Filesystem $filesystem, AddonEnvironment $env, AddonGenerator $generator)
70
    {
71 3
        $addon_name = preg_replace('#(/+)#', '-', $this->argument('name'));
72
73 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 71 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...
74
75 3
        if ($filesystem->exists($output_path)) {
76
            throw new UnexpectedValueException("addon directory '{$addon_name}' is already exists.");
77
        }
78
79 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...
80 3
        if ($this->option('no-namespace')) {
81 1
            $namespace = '';
82 1
        } else {
83 2
            if ($this->option('namespace')) {
84 1
                $namespace = str_replace('/', '\\', $this->option('namespace'));
85 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...
86 1
            } else {
87 1
                $namespace = 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...
88
            }
89
90 2
            $namespace = preg_replace('/^(\d)/', '_$1', $namespace);
91
        }
92 3
        $languages = $this->option('language') ? explode($this->option('language')) : [];
93
94
        $properties = [
95 3
            'addon_name' => preg_replace('/[^\w_]/', '', $addon_name),
96 3
            'addon_class' => preg_replace(
97 3
                ['/[^\w_]/', '/^(\d)/'],
98 3
                ['', '_$1'],
99 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 71 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...
100 3
            ),
101 3
            'namespace' => $this->option('no-namespace') ? '' : $namespace,
102 3
            'languages' => array_unique(array_merge(['en', $this->laravel['config']->get('app.locale')], $languages)),
103 3
        ];
104
105
        try {
106 3
            $generator->generateAddon($output_path, str_replace(':', '-', $skeleton), $properties);
107 3
            $this->info('Addon Generated.');
108 3
        } catch (Exception $ex) {
109
            $filesystem->deleteDirectory($output_path);
110
111
            throw $ex;
112
        }
113 3
    }
114
}
115