Failed Conditions
Branch feature/streamline-console-com... (d385c8)
by Bas
11:19
created

ModelMakeCommand   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 74
dl 0
loc 187
rs 9.84
c 0
b 0
f 0
wmc 32

7 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 21 6
A createMigration() 0 22 5
A resolveStubPath() 0 9 3
A handleAllOption() 0 9 2
A getStub() 0 22 6
A getOptions() 0 25 2
B handleOptions() 0 20 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Console;
6
7
use Illuminate\Foundation\Console\ModelMakeCommand as IlluminateModelMakeCommand;
8
use Illuminate\Support\Str;
9
use LaravelFreelancerNL\Aranguent\Console\Concerns\ArangoCommands;
10
use Symfony\Component\Console\Input\InputOption;
11
12
class ModelMakeCommand extends IlluminateModelMakeCommand
13
{
14
    use ArangoCommands;
15
16
    /**
17
     * The console command name.
18
     *
19
     * @var string
20
     */
21
    protected $name = 'make:model';
22
23
    /**
24
     * Execute the console command.
25
     *
26
     * @return void|false
27
     */
28
    public function handle()
29
    {
30
        /** @phpstan-ignore-next-line  */
31
        if ($this->hasOption('arangodb') &&  $this->option('arangodb')) {
32
            $this->useArangoDB = true;
33
        }
34
35
        if ($this->useFallback()) {
36
            /** @phpstan-ignore-next-line */
37
            return parent::handle();
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::handle() targeting Illuminate\Foundation\Co...elMakeCommand::handle() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug Best Practice introduced by
The expression return parent::handle() returns the type void which is incompatible with the documented return type false.
Loading history...
38
        }
39
40
        /** @phpstan-ignore-next-line */
41
        if (parent::handle() === false && ! $this->option('force')) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::handle() targeting Illuminate\Foundation\Co...elMakeCommand::handle() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
introduced by
The condition parent::handle() === false is always false.
Loading history...
42
            return false;
43
        }
44
45
        $this->handleAllOption();
46
47
48
        $this->handleOptions();
49
    }
50
51
52
    /**
53
     * Create a migration file for the model.
54
     *
55
     * @return void
56
     */
57
    protected function createMigration()
58
    {
59
        if ($this->useFallback()) {
60
            parent::createMigration();
61
            return;
62
        }
63
64
        $table = Str::snake(Str::pluralStudly(class_basename($this->argument('name'))));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') can also be of type array; however, parameter $class of class_basename() does only seem to accept object|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
        $table = Str::snake(Str::pluralStudly(class_basename(/** @scrutinizer ignore-type */ $this->argument('name'))));
Loading history...
65
66
        if ($this->option('pivot')) {
67
            $table = Str::singular($table);
68
        }
69
70
        $createCommand = '--create';
71
        if ($this->option('edge-pivot') || $this->option('edge-morph-pivot')) {
72
            $createCommand = '--edge';
73
        }
74
75
        $this->call('make:migration', [
76
            'name' => "create_{$table}_table",
77
            $createCommand => $table,
78
            '--fullpath' => true,
79
        ]);
80
    }
81
82
    /**
83
     * @return void
84
     */
85
    public function handleAllOption(): void
86
    {
87
        if ($this->option('all')) {
88
            $this->input->setOption('factory', true);
89
            $this->input->setOption('seed', true);
90
            $this->input->setOption('migration', true);
91
            $this->input->setOption('controller', true);
92
            $this->input->setOption('policy', true);
93
            $this->input->setOption('resource', true);
94
        }
95
    }
96
97
    /**
98
     * @return void
99
     */
100
    public function handleOptions(): void
101
    {
102
        if ($this->option('factory')) {
103
            $this->createFactory();
104
        }
105
106
        if ($this->option('migration')) {
107
            $this->createMigration();
108
        }
109
110
        if ($this->option('seed')) {
111
            $this->createSeeder();
112
        }
113
114
        if ($this->option('controller') || $this->option('resource') || $this->option('api')) {
115
            $this->createController();
116
        }
117
118
        if ($this->option('policy')) {
119
            $this->createPolicy();
120
        }
121
    }
122
123
    /**
124
     * @return array<array-key, string|null|int>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, string|null|int> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, string|null|int>.
Loading history...
125
     */
126
    protected function getOptions()
127
    {
128
        $options = parent::getOptions();
129
        $options[] = [
130
            'edge-pivot',
131
            null,
132
            InputOption::VALUE_NONE, 'The generated model uses a custom intermediate edge-collection model for ArangoDB'
133
        ];
134
        $options[] = [
135
            'edge-morph-pivot',
136
            null,
137
            InputOption::VALUE_NONE,
138
            'The generated model uses a custom polymorphic intermediate edge-collection model for ArangoDB'
139
        ];
140
141
        if (!$this->arangodbIsDefaultConnection()) {
142
            $options[] = [
143
                'arangodb',
144
                null,
145
                InputOption::VALUE_NONE,
146
                'Use ArangoDB instead of the default connection.'
147
            ];
148
        }
149
150
        return $options;
151
    }
152
153
154
    /**
155
     * Get the stub file for the generator.
156
     *
157
     * @return string
158
     */
159
    protected function getStub()
160
    {
161
        if ($this->useFallback()) {
162
            return parent::getStub();
163
        }
164
        if ($this->option('pivot')) {
165
            return $this->resolveStubPath('/stubs/model.pivot.stub');
166
        }
167
168
        if ($this->option('morph-pivot')) {
169
            return $this->resolveStubPath('/stubs/model.morph-pivot.stub');
170
        }
171
172
        if ($this->option('edge-pivot')) {
173
            return $this->resolveStubPath('/stubs/model.edge-pivot.stub');
174
        }
175
176
        if ($this->option('edge-morph-pivot')) {
177
            return $this->resolveStubPath('/stubs/model.edge-morph-pivot.stub');
178
        }
179
180
        return $this->resolveStubPath('/stubs/model.stub');
181
    }
182
183
184
    /**
185
     * Resolve the fully-qualified path to the stub.
186
     *
187
     * @param  string  $stub
188
     * @return string
189
     */
190
    protected function resolveStubPath($stub)
191
    {
192
        if ($this->useFallback()) {
193
            return parent::resolveStubPath($stub);
194
        }
195
196
        return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
197
            ? $customPath
198
            : __DIR__ . $stub;
199
    }
200
}
201