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; |
||
7 | class ViewForm extends ViewGenerator { |
||
8 | |||
9 | /** |
||
10 | * The name and signature of the console command. |
||
11 | * |
||
12 | * @var string |
||
13 | */ |
||
14 | protected $name = 'jig:generate:form'; |
||
15 | |||
16 | /** |
||
17 | * The console command description. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $description = 'Generate create and edit view templates'; |
||
22 | |||
23 | /** |
||
24 | * Path for create view |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected string $create = 'create'; |
||
|
|||
29 | |||
30 | /** |
||
31 | * Path for edit view |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected string $show = 'show'; |
||
36 | |||
37 | /** |
||
38 | * Path for form view |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected string $form = 'form'; |
||
43 | |||
44 | /** |
||
45 | * Execute the console command. |
||
46 | * |
||
47 | * @return void |
||
48 | */ |
||
49 | public function handle() |
||
50 | { |
||
51 | $force = $this->option('force'); |
||
52 | |||
53 | //TODO check if exists |
||
54 | //TODO make global for all generator |
||
55 | //TODO also with prefix |
||
56 | /*if(!empty($template = $this->option('template'))) { |
||
57 | $this->create = 'templates.'.$template.'.create'; |
||
58 | $this->edit = 'templates.'.$template.'.edit'; |
||
59 | $this->form = 'templates.'.$template.'.form'; |
||
60 | $this->formRight = 'templates.'.$template.'form-right'; |
||
61 | $this->formJs = 'templates.'.$template.'.form-js'; |
||
62 | }*/ |
||
63 | |||
64 | if(!empty($belongsToMany = $this->option('belongs-to-many'))) { |
||
65 | $this->setBelongToManyRelation($belongsToMany); |
||
66 | } |
||
67 | /*Make Create Form*/ |
||
68 | $viewPath = resource_path('js/Pages/'.$this->modelPlural.'/CreateForm.vue'); |
||
69 | if ($this->alreadyExists($viewPath) && !$force) { |
||
70 | $this->error('File '.$viewPath.' already exists!'); |
||
71 | } else { |
||
72 | if ($this->alreadyExists($viewPath) && $force) { |
||
73 | $this->warn('File '.$viewPath.' already exists! File will be deleted.'); |
||
74 | $this->files->delete($viewPath); |
||
75 | } |
||
76 | $this->makeDirectory($viewPath); |
||
77 | $this->files->put($viewPath, $this->buildForm("create-form")); |
||
78 | $this->info('Generating '.$viewPath.' finished'); |
||
79 | } |
||
80 | /*Make Create Page*/ |
||
81 | $viewPath = resource_path('js/Pages/'.$this->modelPlural.'/Create.vue'); |
||
82 | if ($this->alreadyExists($viewPath) && !$force) { |
||
83 | $this->error('File '.$viewPath.' already exists!'); |
||
84 | } else { |
||
85 | if ($this->alreadyExists($viewPath) && $force) { |
||
86 | $this->warn('File '.$viewPath.' already exists! File will be deleted.'); |
||
87 | $this->files->delete($viewPath); |
||
88 | } |
||
89 | $this->makeDirectory($viewPath); |
||
90 | $this->files->put($viewPath, $this->buildForm("create")); |
||
91 | $this->info('Generating '.$viewPath.' finished'); |
||
92 | } |
||
93 | //Make edit form |
||
94 | $viewPath = resource_path('js/Pages/'.$this->modelPlural.'/EditForm.vue'); |
||
95 | if ($this->alreadyExists($viewPath) && !$force) { |
||
96 | $this->error('File '.$viewPath.' already exists!'); |
||
97 | } else { |
||
98 | if ($this->alreadyExists($viewPath) && $force) { |
||
99 | $this->warn('File '.$viewPath.' already exists! File will be deleted.'); |
||
100 | $this->files->delete($viewPath); |
||
101 | } |
||
102 | $this->makeDirectory($viewPath); |
||
103 | $this->files->put($viewPath, $this->buildForm("edit-form")); |
||
104 | $this->info('Generating '.$viewPath.' finished'); |
||
105 | } |
||
106 | |||
107 | //Make edit Page |
||
108 | $viewPath = resource_path('js/Pages/'.$this->modelPlural.'/Edit.vue'); |
||
109 | if ($this->alreadyExists($viewPath) && !$force) { |
||
110 | $this->error('File '.$viewPath.' already exists!'); |
||
111 | } else { |
||
112 | if ($this->alreadyExists($viewPath) && $force) { |
||
113 | $this->warn('File '.$viewPath.' already exists! File will be deleted.'); |
||
114 | $this->files->delete($viewPath); |
||
115 | } |
||
116 | $this->makeDirectory($viewPath); |
||
117 | $this->files->put($viewPath, $this->buildForm("edit")); |
||
118 | $this->info('Generating '.$viewPath.' finished'); |
||
119 | } |
||
120 | |||
121 | // Make Show Form |
||
122 | $viewPath = resource_path('js/Pages/'.$this->modelPlural.'/ShowForm.vue'); |
||
123 | if ($this->alreadyExists($viewPath) && !$force) { |
||
124 | $this->error('File '.$viewPath.' already exists!'); |
||
125 | } else { |
||
126 | if ($this->alreadyExists($viewPath) && $force) { |
||
127 | $this->warn('File '.$viewPath.' already exists! File will be deleted.'); |
||
128 | $this->files->delete($viewPath); |
||
129 | } |
||
130 | $this->makeDirectory($viewPath); |
||
131 | $this->files->put($viewPath, $this->buildForm("show-form")); |
||
132 | $this->info('Generating '.$viewPath.' finished'); |
||
133 | } |
||
134 | |||
135 | // Make Show Page |
||
136 | $viewPath = resource_path('js/Pages/'.$this->modelPlural.'/Show.vue'); |
||
137 | if ($this->alreadyExists($viewPath) && !$force) { |
||
138 | $this->error('File '.$viewPath.' already exists!'); |
||
139 | } else { |
||
140 | if ($this->alreadyExists($viewPath) && $force) { |
||
141 | $this->warn('File '.$viewPath.' already exists! File will be deleted.'); |
||
142 | $this->files->delete($viewPath); |
||
143 | } |
||
144 | $this->makeDirectory($viewPath); |
||
145 | $this->files->put($viewPath, $this->buildForm("show")); |
||
146 | $this->info('Generating '.$viewPath.' finished'); |
||
147 | } |
||
148 | |||
149 | } |
||
150 | |||
151 | protected function isUsedTwoColumnsLayout() : bool { |
||
152 | return false; |
||
153 | } |
||
154 | |||
155 | protected function buildForm(?string $type=null): string |
||
156 | { |
||
157 | $belongsTos = $this->setBelongsToRelations(); |
||
158 | $relatables = $this->getVisibleColumns($this->tableName, $this->modelVariableName)->filter(function($column) use($belongsTos) { |
||
159 | return in_array($column['name'],$belongsTos->pluck('foreign_key')->toArray()); |
||
160 | })->keyBy('name'); |
||
161 | $columns = $this->getVisibleColumns($this->tableName, $this->modelVariableName)->reject(function($column) use($belongsTos) { |
||
162 | return in_array($column['name'],$belongsTos->pluck('foreign_key')->toArray()) || $column["name"] ==='slug'; |
||
163 | })->map(function($column) { |
||
164 | $column["label"] = str_replace("_"," ",Str::title($column['name'])); |
||
165 | return $column; |
||
166 | })->keyBy('name'); |
||
167 | return view('jig::'.$type, [ |
||
168 | 'modelBaseName' => $this->modelBaseName, |
||
169 | 'modelRouteAndViewName' => $this->modelRouteAndViewName, |
||
170 | 'modelPlural' => $this->modelPlural, |
||
171 | 'modelTitle' => $this->titleSingular, |
||
172 | 'modelDotNotation' => $this->modelDotNotation, |
||
173 | 'modelLangFormat' => $this->modelLangFormat, |
||
174 | 'columns' => $columns, |
||
175 | "relatable" => $relatables, |
||
176 | 'hasTranslatable' => $this->readColumnsFromTable($this->tableName)->filter(function($column) { |
||
177 | return $column['type'] == "json"; |
||
178 | })->count() > 0, |
||
179 | 'translatableTextarea' => ['perex', 'text', 'body'], |
||
180 | 'relations' => $this->relations, |
||
181 | ])->render(); |
||
182 | } |
||
183 | |||
184 | protected function buildShow(): string |
||
185 | { |
||
186 | |||
187 | $belongsTos = $this->setBelongsToRelations(); |
||
188 | $relatables = $this->getVisibleColumns($this->tableName, $this->modelVariableName)->filter(function($column) use($belongsTos) { |
||
189 | return in_array($column['name'],$belongsTos->pluck('foreign_key')->toArray()); |
||
190 | })->keyBy('name'); |
||
191 | $columns = $this->readColumnsFromTable($this->tableName)->reject(function($column) use($belongsTos) { |
||
192 | return in_array($column['name'],$belongsTos->pluck('foreign_key')->toArray()); |
||
193 | })->map(function($column) { |
||
194 | $column["label"] = str_replace("_"," ",Str::title($column['name'])); |
||
195 | return $column; |
||
196 | })->keyBy('name'); |
||
197 | return view('jig::'.$this->show, [ |
||
198 | 'modelBaseName' => $this->modelBaseName, |
||
199 | 'modelRouteAndViewName' => $this->modelRouteAndViewName, |
||
200 | 'modelVariableName' => $this->modelVariableName, |
||
201 | 'modelPlural' => $this->modelPlural, |
||
202 | 'modelTitleSingular' => $this->titleSingular, |
||
203 | 'modelViewsDirectory' => $this->modelViewsDirectory, |
||
204 | 'modelDotNotation' => $this->modelDotNotation, |
||
205 | 'modelJSName' => $this->modelJSName, |
||
206 | 'modelLangFormat' => $this->modelLangFormat, |
||
207 | 'resource' => $this->resource, |
||
208 | 'isUsedTwoColumnsLayout' => $this->isUsedTwoColumnsLayout(), |
||
209 | |||
210 | 'modelTitle' => $this->readColumnsFromTable($this->tableName)->filter(function($column){ |
||
211 | return in_array($column['name'], ['title', 'name', 'first_name', 'email']); |
||
212 | })->first(null, ['name'=>'id'])['name'], |
||
213 | 'columns' => $columns, |
||
214 | "relatables" => $relatables, |
||
215 | 'relations' => $this->relations, |
||
216 | 'hasTranslatable' => $this->readColumnsFromTable($this->tableName)->filter(function($column) { |
||
217 | return $column['type'] == "json"; |
||
218 | })->count() > 0, |
||
219 | ])->render(); |
||
220 | } |
||
221 | |||
222 | protected function getOptions() { |
||
223 | return [ |
||
224 | ['model-name', 'm', InputOption::VALUE_OPTIONAL, 'Generates a code for the given model'], |
||
225 | ['belongs-to-many', 'btm', InputOption::VALUE_OPTIONAL, 'Specify belongs to many relations'], |
||
226 | ['template', 't', InputOption::VALUE_OPTIONAL, 'Specify custom template'], |
||
227 | ['force', 'f', InputOption::VALUE_NONE, 'Force will delete files before regenerating form'], |
||
228 | ]; |
||
229 | } |
||
230 | |||
232 |