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 ViewIndex extends ViewGenerator { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * The name and signature of the console command. |
||
| 10 | * |
||
| 11 | * @var string |
||
| 12 | */ |
||
| 13 | protected $name = 'jig:generate:index'; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * The console command description. |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $description = 'Generate an index view template'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Path for view |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected string $view = 'index'; |
||
|
|
|||
| 28 | |||
| 29 | /** |
||
| 30 | * Path for js view |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | /** |
||
| 35 | * Index view has also export button |
||
| 36 | * |
||
| 37 | * @return mixed |
||
| 38 | */ |
||
| 39 | protected bool $export = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Index view has also bulk options |
||
| 43 | * |
||
| 44 | * @return mixed |
||
| 45 | */ |
||
| 46 | protected bool $withoutBulk = false; |
||
| 47 | |||
| 48 | public function handle() |
||
| 49 | { |
||
| 50 | $force = $this->option('force'); |
||
| 51 | |||
| 52 | if($this->option('with-export')){ |
||
| 53 | $this->export = true; |
||
| 54 | } |
||
| 55 | |||
| 56 | if($this->option('without-bulk')){ |
||
| 57 | $this->withoutBulk = true; |
||
| 58 | } |
||
| 59 | |||
| 60 | $viewPath = resource_path('js/Pages/'.$this->modelPlural.'/Index.vue'); |
||
| 61 | |||
| 62 | if ($this->alreadyExists($viewPath) && !$force) { |
||
| 63 | $this->error('File '.$viewPath.' already exists!'); |
||
| 64 | } else { |
||
| 65 | if ($this->alreadyExists($viewPath) && $force) { |
||
| 66 | $this->warn('File '.$viewPath.' already exists! File will be deleted.'); |
||
| 67 | $this->files->delete($viewPath); |
||
| 68 | } |
||
| 69 | |||
| 70 | $this->makeDirectory($viewPath); |
||
| 71 | |||
| 72 | $this->files->put($viewPath, $this->buildView()); |
||
| 73 | |||
| 74 | $this->info('Generating '.$viewPath.' finished'); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | protected function buildView(): string |
||
| 79 | { |
||
| 80 | |||
| 81 | return view('jig::'.$this->view, [ |
||
| 82 | 'modelBaseName' => $this->modelBaseName, |
||
| 83 | 'modelRouteAndViewName' => str_plural($this->modelRouteAndViewName), |
||
| 84 | 'modelPlural' => $this->modelPlural, |
||
| 85 | 'modelViewsDirectory' => $this->modelViewsDirectory, |
||
| 86 | 'modelJSName' => $this->modelJSName, |
||
| 87 | 'modelVariableName' => $this->modelVariableName, |
||
| 88 | 'modelDotNotation' => $this->modelDotNotation, |
||
| 89 | 'modelLangFormat' => $this->modelLangFormat, |
||
| 90 | 'modelTitle' => $this->titleSingular, |
||
| 91 | 'modelTitlePlural' => $this->titlePlural, |
||
| 92 | 'resource' => $this->resource, |
||
| 93 | 'export' => $this->export, |
||
| 94 | 'containsPublishedAtColumn' => in_array("published_at", array_column($this->readColumnsFromTable($this->tableName)->toArray(), 'name')), |
||
| 95 | 'withoutBulk' => $this->withoutBulk, |
||
| 96 | 'columnsToQuery' => $this->readColumnsFromTable($this->tableName)->filter(function($column) { |
||
| 97 | return !($column['type'] == 'text' || $column['name'] == "password" || $column['name'] == "remember_token" || $column['name'] == "slug" || $column['name'] == "created_at" || $column['name'] == "deleted_at"||Str::contains($column['name'],"_id")); |
||
| 98 | })->pluck('name')->toArray(), |
||
| 99 | 'columns' => $this->readColumnsFromTable($this->tableName)->reject(function($column) { |
||
| 100 | return ($column['type'] == 'text' |
||
| 101 | || in_array($column['name'], ["password", "remember_token", "slug", "created_at", "updated_at", "deleted_at"]) |
||
| 102 | || ($column['type'] == 'json' && in_array($column['name'], ["perex", "text", "body"])) |
||
| 103 | ); |
||
| 104 | })->map(function($col){ |
||
| 105 | |||
| 106 | $filters = collect([]); |
||
| 107 | $col['switch'] = false; |
||
| 108 | |||
| 109 | if ($col['type'] == 'date' || $col['type'] == 'time' || $col['type'] == 'datetime') { |
||
| 110 | $filters->push($col['type']); |
||
| 111 | } |
||
| 112 | |||
| 113 | if ($col['type'] == 'boolean' && ($col['name'] == 'enabled' || $col['name'] == 'activated' || $col['name'] == 'is_published')) { |
||
| 114 | $col['switch'] = true; |
||
| 115 | } |
||
| 116 | |||
| 117 | $col['filters'] = $filters->isNotEmpty() ? ' | '.implode(' | ', $filters->toArray()) : ''; |
||
| 118 | |||
| 119 | return $col; |
||
| 120 | }), |
||
| 121 | // 'filters' => $this->readColumnsFromTable($tableName)->filter(function($column) { |
||
| 122 | // return $column['type'] == 'boolean' || $column['type'] == 'date'; |
||
| 123 | // }), |
||
| 124 | ])->render(); |
||
| 125 | } |
||
| 126 | |||
| 127 | protected function getOptions() { |
||
| 128 | return [ |
||
| 129 | ['model-name', 'm', InputOption::VALUE_OPTIONAL, 'Generates a code for the given model'], |
||
| 130 | ['template', 't', InputOption::VALUE_OPTIONAL, 'Specify custom template'], |
||
| 131 | ['force', 'f', InputOption::VALUE_NONE, 'Force will delete files before regenerating index'], |
||
| 132 | ['with-export', 'e', InputOption::VALUE_NONE, 'Generate an option to Export as Excel'], |
||
| 133 | ['without-bulk', 'wb', InputOption::VALUE_NONE, 'Generate without bulk options'], |
||
| 134 | ]; |
||
| 135 | } |
||
| 136 | |||
| 137 | } |
||
| 138 |