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 ApiRoutes extends FileAppender { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * The name and signature of the console command. |
||
| 10 | * |
||
| 11 | * @var string |
||
| 12 | */ |
||
| 13 | protected $name = 'jig:generate:api:routes'; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * The console command description. |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $description = 'Append api routes to the api.php routes file'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Path for view |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected string $view = 'api-routes'; |
||
|
|
|||
| 28 | |||
| 29 | /** |
||
| 30 | * Routes have also export route |
||
| 31 | * |
||
| 32 | * @return mixed |
||
| 33 | */ |
||
| 34 | protected bool $export = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Routes have also bulk options route |
||
| 38 | * |
||
| 39 | * @return mixed |
||
| 40 | */ |
||
| 41 | protected bool $withoutBulk = false; |
||
| 42 | |||
| 43 | public function handle() |
||
| 44 | { |
||
| 45 | if($this->option('with-export')){ |
||
| 46 | $this->export = true; |
||
| 47 | } |
||
| 48 | |||
| 49 | if($this->option('without-bulk')){ |
||
| 50 | $this->withoutBulk = true; |
||
| 51 | } |
||
| 52 | |||
| 53 | //TODO check if exists |
||
| 54 | //TODO make global for all generator |
||
| 55 | //TODO also with prefix |
||
| 56 | if(!empty($template = $this->option('template'))) { |
||
| 57 | $this->view = 'templates.'.$template.'.routes'; |
||
| 58 | } |
||
| 59 | |||
| 60 | if ($this->appendIfNotAlreadyAppended(base_path('routes/api.php'), PHP_EOL.PHP_EOL.$this->buildClass())){ |
||
| 61 | $this->info('Appending API routes finished'); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | protected function buildClass(bool $api=false): string |
||
| 66 | { |
||
| 67 | return view('jig::'.$this->view, [ |
||
| 68 | 'controllerClassName' => class_basename($this->controllerWithNamespaceFromDefault), |
||
| 69 | 'controllerPartiallyFullName' => $this->controllerWithNamespaceFromDefault, |
||
| 70 | 'modelRouteName' => Str::plural($this->modelRouteAndViewName), |
||
| 71 | 'modelVariableName' => $this->modelVariableName, |
||
| 72 | 'modelViewsDirectory' => $this->modelViewsDirectory, |
||
| 73 | 'resource' => $this->resource, |
||
| 74 | 'export' => $this->export, |
||
| 75 | 'withoutBulk' => true, |
||
| 76 | ])->render(); |
||
| 77 | } |
||
| 78 | |||
| 79 | protected function getOptions() { |
||
| 80 | return [ |
||
| 81 | ['model-name', 'm', InputOption::VALUE_OPTIONAL, 'Generates a controller for the given model'], |
||
| 82 | ['controller-name', 'c', InputOption::VALUE_OPTIONAL, 'Specify custom controller name'], |
||
| 83 | ['template', 't', InputOption::VALUE_OPTIONAL, 'Specify custom template'], |
||
| 84 | ['with-export', 'e', InputOption::VALUE_NONE, 'Generate an option to Export as Excel'], |
||
| 85 | ['without-bulk', 'wb', InputOption::VALUE_NONE, 'Generate without bulk options'], |
||
| 86 | ]; |
||
| 87 | } |
||
| 88 | |||
| 89 | } |
||
| 90 |