ModelMakeCommand::getStub()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.0208

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 6
nop 0
dl 0
loc 22
ccs 11
cts 12
cp 0.9167
crap 6.0208
rs 9.2222
c 0
b 0
f 0
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 7
    public function handle()
29
    {
30
        /** @phpstan-ignore-next-line  */
31 7
        if ($this->hasOption('arangodb') &&  $this->option('arangodb')) {
32
            $this->useArangoDB = true;
33
        }
34
35 7
        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 7
        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 7
        $this->handleAllOption();
46
47
48 7
        $this->handleOptions();
49
    }
50
51
52
    /**
53
     * Create a migration file for the model.
54
     *
55
     * @return void
56
     */
57 2
    protected function createMigration()
58
    {
59 2
        if ($this->useFallback()) {
60
            parent::createMigration();
61
            return;
62
        }
63
64 2
        $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 2
        if ($this->option('pivot')) {
67
            $table = Str::singular($table);
68
        }
69
70 2
        $createCommand = '--create';
71 2
        if ($this->option('edge-pivot') || $this->option('edge-morph-pivot')) {
72 1
            $createCommand = '--edge';
73
        }
74
75 2
        $this->call('make:migration', [
76 2
            'name' => "create_{$table}_table",
77 2
            $createCommand => $table,
78 2
            '--fullpath' => true,
79 2
        ]);
80
    }
81
82
    /**
83
     * @return void
84
     */
85 7
    public function handleAllOption(): void
86
    {
87 7
        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 7
    public function handleOptions(): void
101
    {
102 7
        if ($this->option('factory')) {
103
            $this->createFactory();
104
        }
105
106 7
        if ($this->option('migration')) {
107 2
            $this->createMigration();
108
        }
109
110 7
        if ($this->option('seed')) {
111
            $this->createSeeder();
112
        }
113
114 7
        if ($this->option('controller') || $this->option('resource') || $this->option('api')) {
115
            $this->createController();
116
        }
117
118 7
        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 498
    protected function getOptions()
127
    {
128 498
        $options = parent::getOptions();
129 498
        $options[] = [
130 498
            'edge-pivot',
131 498
            null,
132 498
            InputOption::VALUE_NONE, 'The generated model uses a custom intermediate edge-collection model for ArangoDB',
133 498
        ];
134 498
        $options[] = [
135 498
            'edge-morph-pivot',
136 498
            null,
137 498
            InputOption::VALUE_NONE,
138 498
            'The generated model uses a custom polymorphic intermediate edge-collection model for ArangoDB',
139 498
        ];
140
141 498
        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 498
        return $options;
151
    }
152
153
154
    /**
155
     * Get the stub file for the generator.
156
     *
157
     * @return string
158
     */
159 7
    protected function getStub()
160
    {
161 7
        if ($this->useFallback()) {
162
            return parent::getStub();
163
        }
164 7
        if ($this->option('pivot')) {
165 1
            return $this->resolveStubPath('/stubs/model.pivot.stub');
166
        }
167
168 6
        if ($this->option('morph-pivot')) {
169 1
            return $this->resolveStubPath('/stubs/model.morph-pivot.stub');
170
        }
171
172 5
        if ($this->option('edge-pivot')) {
173 2
            return $this->resolveStubPath('/stubs/model.edge-pivot.stub');
174
        }
175
176 3
        if ($this->option('edge-morph-pivot')) {
177 1
            return $this->resolveStubPath('/stubs/model.edge-morph-pivot.stub');
178
        }
179
180 2
        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 7
    protected function resolveStubPath($stub)
191
    {
192 7
        if ($this->useFallback()) {
193
            return parent::resolveStubPath($stub);
194
        }
195
196 7
        return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
197
            ? $customPath
198 7
            : __DIR__ . $stub;
199
    }
200
}
201