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 ModelFactory extends FileAppender { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * The name and signature of the console command. |
||
| 10 | * |
||
| 11 | * @var string |
||
| 12 | */ |
||
| 13 | protected $name = 'jig:generate:factory'; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * The console command description. |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $description = 'Append a new factory'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Path for view |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected string $view = 'factory'; |
||
|
|
|||
| 28 | |||
| 29 | /** |
||
| 30 | * Execute the console command. |
||
| 31 | * |
||
| 32 | * @return void |
||
| 33 | */ |
||
| 34 | public function handle() |
||
| 35 | { |
||
| 36 | //TODO check if exists |
||
| 37 | //TODO make global for all generator |
||
| 38 | //TODO also with prefix |
||
| 39 | if(!empty($template = $this->option('template'))) { |
||
| 40 | $this->view = 'templates.'.$template.'.factory'; |
||
| 41 | } |
||
| 42 | |||
| 43 | if ($this->appendIfNotAlreadyAppended(base_path('database/factories/ModelFactory.php'), $this->buildClass())){ |
||
| 44 | $this->info('Appending '.$this->modelBaseName.' model to ModelFactory finished'); |
||
| 45 | } |
||
| 46 | |||
| 47 | if ($this->option('seed')) { |
||
| 48 | $this->info('Seeding testing data'); |
||
| 49 | factory($this->modelFullName , 50)->create(); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | protected function buildClass(): string |
||
| 54 | { |
||
| 55 | |||
| 56 | return view('jig::'.$this->view, [ |
||
| 57 | 'modelFullName' => $this->modelFullName, |
||
| 58 | |||
| 59 | 'columns' => $this->readColumnsFromTable($this->tableName) |
||
| 60 | // we skip primary key |
||
| 61 | ->filter(function($col){ |
||
| 62 | return $col['name'] != 'id'; |
||
| 63 | }) |
||
| 64 | ->map(function($col) { |
||
| 65 | if($col['name'] == 'deleted_at') { |
||
| 66 | $type = 'null'; |
||
| 67 | } else if($col['name'] == 'remember_token') { |
||
| 68 | $type = 'null'; |
||
| 69 | } else { |
||
| 70 | if ($col['type'] == 'date') { |
||
| 71 | $type = '$faker->date()'; |
||
| 72 | } elseif ($col['type'] == 'time') { |
||
| 73 | $type = '$faker->time()'; |
||
| 74 | } elseif ($col['type'] == 'datetime') { |
||
| 75 | $type = '$faker->dateTime'; |
||
| 76 | } elseif ($col['type'] == 'text') { |
||
| 77 | $type = '$faker->text()'; |
||
| 78 | } elseif ($col['type'] == 'boolean') { |
||
| 79 | $type = '$faker->boolean()'; |
||
| 80 | } elseif ($col['type'] == 'integer' || $col['type'] == 'numeric' || $col['type'] == 'decimal') { |
||
| 81 | $type = '$faker->randomNumber(5)'; |
||
| 82 | } elseif ($col['type'] == 'float') { |
||
| 83 | $type = '$faker->randomFloat'; |
||
| 84 | } elseif ($col['name'] == 'title') { |
||
| 85 | $type = '$faker->sentence'; |
||
| 86 | } elseif ($col['name'] == 'email') { |
||
| 87 | $type = '$faker->email'; |
||
| 88 | } elseif ($col['name'] == 'name' || $col['name'] == 'first_name') { |
||
| 89 | $type = '$faker->firstName'; |
||
| 90 | } elseif ($col['name'] == 'surname' || $col['name'] == 'last_name') { |
||
| 91 | $type = '$faker->lastName'; |
||
| 92 | } elseif ($col['name'] == 'slug') { |
||
| 93 | $type = '$faker->unique()->slug'; |
||
| 94 | } elseif ($col['name'] == 'password') { |
||
| 95 | $type = 'bcrypt($faker->password)'; |
||
| 96 | } else { |
||
| 97 | $type = '$faker->sentence'; |
||
| 98 | } |
||
| 99 | } |
||
| 100 | return [ |
||
| 101 | 'name' => $col['name'], |
||
| 102 | 'faker' => $type, |
||
| 103 | ]; |
||
| 104 | }), |
||
| 105 | 'translatable' => $this->readColumnsFromTable($this->tableName)->filter(function($column) { |
||
| 106 | return $column['type'] == "json"; |
||
| 107 | })->pluck('name'), |
||
| 108 | ])->render(); |
||
| 109 | } |
||
| 110 | |||
| 111 | protected function getOptions() { |
||
| 112 | return [ |
||
| 113 | ['model-name', 'm', InputOption::VALUE_OPTIONAL, 'Generates a code for the given model'], |
||
| 114 | ['template', 't', InputOption::VALUE_OPTIONAL, 'Specify custom template'], |
||
| 115 | ['seed', 's', InputOption::VALUE_OPTIONAL, 'Seeds the table with fake data'], |
||
| 116 | ['model-with-full-namespace', 'fnm', InputOption::VALUE_OPTIONAL, 'Specify model with full namespace'], |
||
| 117 | ]; |
||
| 118 | } |
||
| 119 | |||
| 120 | } |
||
| 121 |