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; |
||
| 9 | class Repository extends ClassGenerator { |
||
| 10 | |||
| 11 | use FileManipulations; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * The name and signature of the console command. |
||
| 15 | * |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | protected $name = 'jig:generate:repository'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The console command description. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $description = 'Generate an Repository class'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Path for view |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected string $view = 'repository'; |
||
|
|
|||
| 33 | |||
| 34 | /** |
||
| 35 | * Controller has also export method |
||
| 36 | * |
||
| 37 | * @return mixed |
||
| 38 | */ |
||
| 39 | protected bool $export = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Controller has also bulk options method |
||
| 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 | if(!empty($belongsToMany = $this->option('belongs-to-many'))) { |
||
| 61 | $this->setBelongToManyRelation($belongsToMany); |
||
| 62 | } |
||
| 63 | |||
| 64 | if ($this->generateClass($force)){ |
||
| 65 | |||
| 66 | $this->info('Generating '.$this->classFullName.' finished'); |
||
| 67 | } |
||
| 68 | |||
| 69 | } |
||
| 70 | |||
| 71 | protected function buildClass(): string { |
||
| 72 | |||
| 73 | //Set belongsTo Relations |
||
| 74 | $this->setBelongsToRelations(); |
||
| 75 | |||
| 76 | return view('jig::'.$this->view, [ |
||
| 77 | 'repoBaseName' => $this->classBaseName, |
||
| 78 | 'repoNamespace' => $this->classNamespace, |
||
| 79 | 'modelBaseName' => $this->modelBaseName, |
||
| 80 | 'modelFullName' => $this->modelFullName, |
||
| 81 | 'modelPlural' => $this->modelPlural, |
||
| 82 | 'modelTitle' => $this->titleSingular, |
||
| 83 | 'modelVariableName' => $this->modelVariableName, |
||
| 84 | 'modelVariableNamePlural' => Str::plural($this->modelVariableName), |
||
| 85 | 'modelRouteAndViewName' => $this->modelRouteAndViewName, |
||
| 86 | 'modelViewsDirectory' => $this->modelViewsDirectory, |
||
| 87 | 'modelDotNotation' => $this->modelDotNotation, |
||
| 88 | 'modelWithNamespaceFromDefault' => $this->modelWithNamespaceFromDefault, |
||
| 89 | 'export' => $this->export, |
||
| 90 | 'withoutBulk' => $this->withoutBulk, |
||
| 91 | 'exportBaseName' => $this->exportBaseName, |
||
| 92 | 'resource' => $this->resource, |
||
| 93 | // index |
||
| 94 | 'columnsToQuery' => $this->readColumnsFromTable($this->tableName)->filter(function($column) { |
||
| 95 | return !($column['type'] == 'text' || $column['name'] == "password" || $column['name'] == "remember_token" || $column['name'] == "deleted_at"||Str::contains($column['name'],"_id")); |
||
| 96 | })->pluck('name')->toArray(), |
||
| 97 | 'columnsToSearchIn' => $this->readColumnsFromTable($this->tableName)->filter(function($column) { |
||
| 98 | return ($column['type'] == 'json' || $column['type'] == 'text' || $column['type'] == 'string' || $column['name'] == "id") && !($column['name'] == "password" || $column['name'] == "remember_token"); |
||
| 99 | })->pluck('name')->toArray(), |
||
| 100 | // 'filters' => $this->readColumnsFromTable($tableName)->filter(function($column) { |
||
| 101 | // return $column['type'] == 'boolean' || $column['type'] == 'date'; |
||
| 102 | // }), |
||
| 103 | // validation in store/update |
||
| 104 | 'columns' => $this->getVisibleColumns($this->tableName, $this->modelVariableName), |
||
| 105 | 'relations' => $this->relations, |
||
| 106 | 'hasSoftDelete' => $this->readColumnsFromTable($this->tableName)->filter(function($column) { |
||
| 107 | return $column['name'] == "deleted_at"; |
||
| 108 | })->count() > 0, |
||
| 109 | ])->render(); |
||
| 110 | } |
||
| 111 | |||
| 112 | protected function getOptions() { |
||
| 113 | return [ |
||
| 114 | ['model-name', 'm', InputOption::VALUE_OPTIONAL, 'Generates a code for the given model'], |
||
| 115 | ['template', 't', InputOption::VALUE_OPTIONAL, 'Specify custom template'], |
||
| 116 | ['belongs-to-many', 'btm', InputOption::VALUE_OPTIONAL, 'Specify belongs to many relations'], |
||
| 117 | ['force', 'f', InputOption::VALUE_NONE, 'Force will delete files before regenerating controller'], |
||
| 118 | ['model-with-full-namespace', 'fnm', InputOption::VALUE_OPTIONAL, 'Specify model with full namespace'], |
||
| 119 | ['with-export', 'e', InputOption::VALUE_NONE, 'Generate an option to Export as Excel'], |
||
| 120 | ['without-bulk', 'wb', InputOption::VALUE_NONE, 'Generate without bulk options'], |
||
| 121 | ]; |
||
| 122 | } |
||
| 123 | |||
| 124 | public function generateClassNameFromTable($tableName) { |
||
| 125 | return Str::studly(Str::plural($tableName)); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Get the default namespace for the class. |
||
| 130 | * |
||
| 131 | * @param string $rootNamespace |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | protected function getDefaultNamespace(string $rootNamespace): string |
||
| 135 | { |
||
| 136 | return $rootNamespace.'\Repositories'; |
||
| 137 | } |
||
| 138 | } |
||
| 139 |