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; |
||
6 | class Export extends ClassGenerator { |
||
7 | |||
8 | /** |
||
9 | * The name and signature of the console command. |
||
10 | * |
||
11 | * @var string |
||
12 | */ |
||
13 | protected $name = 'jig:generate:export'; |
||
14 | |||
15 | /** |
||
16 | * The console command description. |
||
17 | * |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $description = 'Generate an export class'; |
||
21 | |||
22 | /** |
||
23 | * Path for view |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected string $view = 'export'; |
||
|
|||
28 | |||
29 | /** |
||
30 | * Execute the console command. |
||
31 | * |
||
32 | * @return void |
||
33 | */ |
||
34 | public function handle() |
||
35 | { |
||
36 | $force = $this->option('force'); |
||
37 | |||
38 | if(!empty($template = $this->option('template'))) { |
||
39 | $this->view = 'templates.'.$template.'.export'; |
||
40 | } |
||
41 | |||
42 | if ($this->generateClass($force)){ |
||
43 | $this->info('Generating '.$this->classFullName.' finished'); |
||
44 | } |
||
45 | |||
46 | } |
||
47 | |||
48 | protected function buildClass(): string { |
||
49 | return view('jig::'.$this->view, [ |
||
50 | 'exportNamespace' => $this->classNamespace, |
||
51 | 'modelFullName' => $this->modelFullName, |
||
52 | 'classBaseName' => $this->exportBaseName, |
||
53 | 'modelBaseName' => $this->modelBaseName, |
||
54 | 'modelVariableName' => $this->modelVariableName, |
||
55 | 'modelLangFormat' => $this->modelLangFormat, |
||
56 | 'columnsToExport' => $this->readColumnsFromTable($this->tableName)->filter(function($column) { |
||
57 | return !($column['name'] == "password" || $column['name'] == "remember_token" || $column['name'] == "updated_at" || $column['name'] == "created_at" || $column['name'] == "deleted_at"); |
||
58 | })->pluck('name')->toArray(), |
||
59 | ])->render(); |
||
60 | } |
||
61 | |||
62 | protected function getOptions() { |
||
63 | return [ |
||
64 | ['force', 'f', InputOption::VALUE_NONE, 'Force will delete files before regenerating request'], |
||
65 | ['model-with-full-namespace', 'fnm', InputOption::VALUE_OPTIONAL, 'Specify model with full namespace'], |
||
66 | ['template', 't', InputOption::VALUE_OPTIONAL, 'Specify custom template'], |
||
67 | ]; |
||
68 | } |
||
69 | |||
70 | public function generateClassNameFromTable($tableName) { |
||
71 | return $this->exportBaseName; |
||
72 | } |
||
73 | /** |
||
74 | * Get the default namespace for the class. |
||
75 | * |
||
76 | * @param string $rootNamespace |
||
77 | * @return string |
||
78 | */ |
||
79 | protected function getDefaultNamespace(string $rootNamespace) : string |
||
80 | { |
||
81 | return $rootNamespace.'\Exports'; |
||
82 | } |
||
83 | } |
||
84 |