Completed
Push — master ( 250f6a...af675f )
by Sam
07:31
created

Model   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 124
Duplicated Lines 31.45 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 39
loc 124
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Savannabits\JetstreamInertiaGenerator\Generators;
2
3
use Illuminate\Support\Str;
4
use Symfony\Component\Console\Input\InputOption;
5
6
class Model extends ClassGenerator {
7
8
    /**
9
     * The name and signature of the console command.
10
     *
11
     * @var string
12
     */
13
    protected $name = 'jig:generate:model';
14
15
    /**
16
     * The console command description.
17
     *
18
     * @var string
19
     */
20
    protected $description = 'Generate a model class';
21
22
    /**
23
     * Path for view
24
     *
25
     * @var string
26
     */
27
    protected string $view = 'model';
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
28
29
    /**
30
     * Execute the console command.
31
     *
32
     * @return void
33
     */
34
    public function handle()
35
    {
36
        $force = $this->option('force');
37
        //TODO check if exists
38
        //TODO make global for all generator
39
        //TODO also with prefix
40
        if(!empty($template = $this->option('template'))) {
41
            $this->view = 'templates.'.$template.'.model';
42
        }
43
44
        if(!empty($belongsToMany = $this->option('belongs-to-many'))) {
45
            $this->setBelongToManyRelation($belongsToMany);
46
        }
47
48
        if ($this->generateClass($force)){
49
            $this->info('Generating '.$this->classFullName.' finished');
50
        }
51
52
        /*Generate a Policy Skeleton for the model*/
53
        /*$this->call('make:policy', [
54
            "name" => $this->modelBaseName."Policy",
55
            '--model' => $this->modelBaseName,
56
        ]);
57
        $this->info('Generating '.$this->modelBaseName."Policy".' finished');*/
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    protected function buildClass(): string
64
    {
65
        //Set belongsTo Relations
66
67
        $this->setBelongsToRelations();
68
69
        return view('jig::'.$this->view, [
70
            'modelBaseName' => $this->classBaseName,
71
            'modelNameSpace' => $this->classNamespace,
72
            // if table name differs from the snake case plural form of the classname, then we need to specify the table name
73
            'tableName' => ($this->tableName !== Str::snake(Str::plural($this->classBaseName))) ? $this->tableName : null,
74
75
            'dates' => $this->readColumnsFromTable($this->tableName)->filter(function($column) {
76
                return $column['type'] == "date";
77
            })->pluck('name'),
78
            'datetimes' => $this->readColumnsFromTable($this->tableName)->filter(function($column) {
79
                return $column['type'] == "datetime";
80
            })->pluck('name'),
81
82
            'booleans' => $this->readColumnsFromTable($this->tableName)->filter(function($column) {
83
                return $column['type'] == "boolean" || $column['type'] == "bool";
84
            })->pluck('name'),
85
            'fillable' => $this->readColumnsFromTable($this->tableName)->filter(function($column) {
86
                return !in_array($column['name'], ['id', 'created_at', 'updated_at', 'deleted_at','password', 'remember_token','slug']);
87
            })->pluck('name'),
88
            'searchable' => $this->readColumnsFromTable($this->tableName)->filter(function($column) {
89
                return !in_array($column['name'], ['created_at', 'updated_at', 'deleted_at','password', 'remember_token','slug'])
90
                    && !in_array($column["type"],["json"]);
91
            })->pluck('name'),
92
            'hidden' => $this->readColumnsFromTable($this->tableName)->filter(function($column) {
93
                return in_array($column['name'], ['password', 'remember_token']);
94
            })->pluck('name'),
95
            'translatable' => $this->readColumnsFromTable($this->tableName)->filter(function($column) {
96
                return $column['type'] == "json";
97
            })->pluck('name'),
98
            'timestamps' => $this->readColumnsFromTable($this->tableName)->filter(function($column) {
99
                return in_array($column['name'], ['created_at', 'updated_at']);
100
            })->count() > 0,
101
            'hasSoftDelete' => $this->readColumnsFromTable($this->tableName)->filter(function($column) {
102
                return $column['name'] == "deleted_at";
103
            })->count() > 0,
104
            'routeBaseName' => Str::kebab(Str::plural($this->classBaseName)),
105
            'resource' => $this->resource,
106
            'relations' => $this->relations,
107
        ])->render();
108
109
    }
110
111
    protected function getOptions() {
112
        return [
113
            ['template', 't', InputOption::VALUE_OPTIONAL, 'Specify custom template'],
114
            ['belongs-to-many', 'btm', InputOption::VALUE_OPTIONAL, 'Specify belongs to many relations'],
115
            ['force', 'f', InputOption::VALUE_NONE, 'Force will delete files before regenerating model'],
116
        ];
117
    }
118
119
    public function generateClassNameFromTable($tableName) {
120
        return Str::studly(Str::singular($tableName));
121
    }
122
123
    /**
124
     * Get the default namespace for the class.
125
     *
126
     * @param string $rootNamespace
127
     * @return string
128
     */
129
    protected function getDefaultNamespace(string $rootNamespace): string
130
    {
131
        return $rootNamespace.'\Models';
132
    }
133
}
134