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 UpdateRequest extends ClassGenerator { |
||
| 8 | |||
| 9 | /** |
||
| 10 | * The name and signature of the console command. |
||
| 11 | * |
||
| 12 | * @var string |
||
| 13 | */ |
||
| 14 | protected $name = 'jig:generate:request:update'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * The console command description. |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $description = 'Generate an Update request class'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Path for view |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected string $view = 'update-request'; |
||
|
|
|||
| 29 | |||
| 30 | /** |
||
| 31 | * Execute the console command. |
||
| 32 | * |
||
| 33 | * @return mixed |
||
| 34 | */ |
||
| 35 | public function handle() |
||
| 36 | { |
||
| 37 | $force = $this->option('force'); |
||
| 38 | |||
| 39 | //TODO check if exists |
||
| 40 | //TODO make global for all generator |
||
| 41 | //TODO also with prefix |
||
| 42 | if(!empty($template = $this->option('template'))) { |
||
| 43 | $this->view = 'templates.'.$template.'.update-request'; |
||
| 44 | } |
||
| 45 | |||
| 46 | if(!empty($belongsToMany = $this->option('belongs-to-many'))) { |
||
| 47 | $this->setBelongToManyRelation($belongsToMany); |
||
| 48 | } |
||
| 49 | |||
| 50 | if ($this->generateClass($force)){ |
||
| 51 | $this->info('Generating '.$this->classFullName.' finished'); |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | protected function buildClass() : string { |
||
| 56 | |||
| 57 | $this->setBelongsToRelations(); |
||
| 58 | return view('jig::'.$this->view, [ |
||
| 59 | 'modelBaseName' => $this->modelBaseName, |
||
| 60 | 'modelDotNotation' => $this->modelDotNotation, |
||
| 61 | 'modelWithNamespaceFromDefault' => $this->modelWithNamespaceFromDefault, |
||
| 62 | 'modelVariableName' => $this->modelVariableName, |
||
| 63 | 'modelFullName' => $this->modelFullName, |
||
| 64 | 'tableName' => $this->tableName, |
||
| 65 | 'containsPublishedAtColumn' => in_array("published_at", array_column($this->readColumnsFromTable($this->tableName)->toArray(), 'name')), |
||
| 66 | |||
| 67 | // validation in store/update |
||
| 68 | 'columns' => $this->getVisibleColumns($this->tableName, $this->modelVariableName), |
||
| 69 | 'translatable' => $this->readColumnsFromTable($this->tableName)->filter(function($column) { |
||
| 70 | return $column['type'] == "json"; |
||
| 71 | })->pluck('name'), |
||
| 72 | 'relatable' => $this->readColumnsFromTable($this->tableName)->filter(function($column) { |
||
| 73 | return in_array($column['name'],$this->relations["belongsTo"]->pluck('foreign_key')->toArray()); |
||
| 74 | })->keyBy('name'), |
||
| 75 | 'hasSoftDelete' => $this->readColumnsFromTable($this->tableName)->filter(function($column) { |
||
| 76 | return $column['name'] == "deleted_at"; |
||
| 77 | })->count() > 0, |
||
| 78 | 'relations' => $this->relations, |
||
| 79 | ])->render(); |
||
| 80 | } |
||
| 81 | |||
| 82 | protected function getOptions() { |
||
| 83 | return [ |
||
| 84 | ['model-name', 'm', InputOption::VALUE_OPTIONAL, 'Generates a code for the given model'], |
||
| 85 | ['template', 't', InputOption::VALUE_OPTIONAL, 'Specify custom template'], |
||
| 86 | ['belongs-to-many', 'btm', InputOption::VALUE_OPTIONAL, 'Specify belongs to many relations'], |
||
| 87 | ['force', 'f', InputOption::VALUE_NONE, 'Force will delete files before regenerating request'], |
||
| 88 | ]; |
||
| 89 | } |
||
| 90 | |||
| 91 | public function generateClassNameFromTable($tableName): string |
||
| 92 | { |
||
| 93 | return 'Update'.$this->modelBaseName; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Get the default namespace for the class. |
||
| 98 | * |
||
| 99 | * @param string $rootNamespace |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | protected function getDefaultNamespace(string $rootNamespace) : string |
||
| 103 | { |
||
| 104 | return $rootNamespace.'\Http\Requests\\'.$this->modelWithNamespaceFromDefault; |
||
| 105 | } |
||
| 106 | |||
| 107 | } |
||
| 108 |