Completed
Pull Request — master (#66)
by
unknown
01:44
created

MorphTableCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 51
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 14 1
A parseFields() 0 7 1
A schema() 0 6 2
A keys() 0 5 1
1
<?php namespace Wn\Generators\Commands;
2
3
4
class MorphTableCommand extends BaseCommand {
5
6
    protected $signature = 'wn:morph-table
7
        {model : Name of the persistant model or table}
8
        {morphable : Name of the morphable identifier}
9
        {--add= : specifies additional columns like timestamps, softDeletes, rememberToken and nullableTimestamps.}
10
        {--file= : name of the migration file (to use only for testing purpose).}
11
        {--force= : override the existing files}
12
    ';
13
14
    protected $description = 'Generates creation migration for a morphable pivot table';
15
16
    protected $fields;
17
18
    public function handle()
19
    {
20
        $this->parseFields();
21
22
        $this->call('wn:migration', [
23
            'table' => snake_case(str_plural($this->argument('morphable'))),
0 ignored issues
show
Bug introduced by
It seems like $this->argument('morphable') targeting Illuminate\Console\Command::argument() can also be of type array or null; however, str_plural() 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...
24
            '--schema' => $this->schema(),
25
            '--keys' => $this->keys(),
26
            '--file' => $this->option('file'),
27
            '--parsed' => false,
28
            '--force' => $this->option('force'),
29
            '--add' => $this->option('add')
30
        ]);
31
    }
32
33
    protected function parseFields()
34
    {
35
        $this->fields = array_map(function($arg, $app) {
36
            return snake_case(str_singular($this->argument($arg))) . "_" . $app;
0 ignored issues
show
Bug introduced by
It seems like $this->argument($arg) targeting Illuminate\Console\Command::argument() can also be of type array or null; however, str_singular() 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...
37
        }, ['model', 'morphable', 'morphable'], ['id', 'id', 'type']);
38
39
    }
40
41
    protected function schema()
42
    {
43
        return implode(' ', array_map(function($field) {
44
            return $field . ':' . (substr($field, -3) == '_id' ? 'integer:unsigned' : 'string.50') . ':index';
45
        }, $this->fields));
46
    }
47
48
    protected function keys()
49
    {
50
        // return implode(' ', $this->fields);
51
        return snake_case(str_singular($this->argument('model'))) . "_id";
0 ignored issues
show
Bug introduced by
It seems like $this->argument('model') targeting Illuminate\Console\Command::argument() can also be of type array or null; however, str_singular() 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...
52
    }
53
54
}
55