Completed
Push — master ( a2dd34...253c8c )
by Song
03:46
created

MakeCommand::getNameInput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Console;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Illuminate\Database\Eloquent\Model;
7
8
class MakeCommand extends GeneratorCommand
9
{
10
    /**
11
     * The console command name.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'admin:make {name} {--model=} {--O|output}';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Make empty admin controller';
23
24
    /**
25
     * @var ResourceGenerator
26
     */
27
    protected $generator;
28
29
    /**
30
     * Execute the console command.
31
     *
32
     * @return void
33
     */
34
    public function handle()
35
    {
36
        if (!$this->modelExists()) {
37
            $this->error('Model does not exists !');
38
39
            return false;
40
        }
41
42
        $modelName = $this->option('model');
43
44
        $this->generator = new ResourceGenerator($modelName);
45
46
        if ($this->option('output')) {
47
            return $this->output($modelName);
0 ignored issues
show
Bug introduced by
It seems like $modelName defined by $this->option('model') on line 42 can also be of type array or null; however, Encore\Admin\Console\MakeCommand::output() 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...
48
        }
49
50
        parent::handle();
51
    }
52
53
    /**
54
     * @param string $modelName
55
     */
56
    protected function output($modelName)
57
    {
58
        $this->alert("laravel-admin controller code for model [{$modelName}]");
59
60
        $this->info($this->generator->generateGrid());
61
        $this->info($this->generator->generateShow());
62
        $this->info($this->generator->generateForm());
63
    }
64
65
    /**
66
     * Determine if the model is exists.
67
     *
68
     * @return bool
69
     */
70
    protected function modelExists()
71
    {
72
        $model = $this->option('model');
73
74
        if (empty($model)) {
75
            return true;
76
        }
77
78
        return class_exists($model) && is_subclass_of($model, Model::class);
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Illuminate\Database\Eloquent\Model::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
79
    }
80
81
    /**
82
     * Replace the class name for the given stub.
83
     *
84
     * @param string $stub
85
     * @param string $name
86
     *
87
     * @return string
88
     */
89
    protected function replaceClass($stub, $name)
90
    {
91
        $stub = parent::replaceClass($stub, $name);
92
93
        return str_replace(
94
            [
95
                'DummyModelNamespace',
96
                'DummyModel',
97
                'DummyGrid',
98
                'DummyShow',
99
                'DummyForm'
100
            ],
101
            [
102
                $this->option('model'),
103
                class_basename($this->option('model')),
0 ignored issues
show
Bug introduced by
It seems like $this->option('model') targeting Illuminate\Console\Command::option() can also be of type array or null; however, class_basename() does only seem to accept string|object, 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...
104
                $this->indentCodes($this->generator->generateGrid()),
105
                $this->indentCodes($this->generator->generateShow()),
106
                $this->indentCodes($this->generator->generateForm())
107
            ],
108
            $stub
109
        );
110
    }
111
112
    /**
113
     * @param string $code
114
     * @return string
115
     */
116
    protected function indentCodes($code)
117
    {
118
        $indent = str_repeat(' ',  8);
119
120
        return rtrim($indent. preg_replace("/\r\n/", "\r\n{$indent}", $code));
121
    }
122
123
    /**
124
     * Get the stub file for the generator.
125
     *
126
     * @return string
127
     */
128
    protected function getStub()
129
    {
130
        if ($this->option('model')) {
131
            return __DIR__.'/stubs/controller.stub';
132
        }
133
134
        return __DIR__.'/stubs/blank.stub';
135
    }
136
137
    /**
138
     * Get the default namespace for the class.
139
     *
140
     * @param string $rootNamespace
141
     *
142
     * @return string
143
     */
144
    protected function getDefaultNamespace($rootNamespace)
145
    {
146
        $directory = config('admin.directory');
147
148
        $namespace = ucfirst(basename($directory));
149
150
        return $rootNamespace."\\$namespace\Controllers";
151
    }
152
153
    /**
154
     * Get the desired class name from the input.
155
     *
156
     * @return string
157
     */
158
    protected function getNameInput()
159
    {
160
        $name = trim($this->argument('name'));
161
162
        $this->type = $this->qualifyClass($name);
163
164
        return $name;
165
    }
166
}
167