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 Policy extends ClassGenerator { |
||
| 8 | |||
| 9 | use FileManipulations; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * The name and signature of the console command. |
||
| 13 | * |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | protected $name = 'jig:generate:policy'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * The console command description. |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $description = 'Generate an Repository class'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Path for view |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected string $view = 'policy'; |
||
|
|
|||
| 31 | |||
| 32 | /** |
||
| 33 | * Controller has also export method |
||
| 34 | * |
||
| 35 | * @return mixed |
||
| 36 | */ |
||
| 37 | protected bool $export = false; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Controller has also bulk options method |
||
| 41 | * |
||
| 42 | * @return mixed |
||
| 43 | */ |
||
| 44 | protected bool $withoutBulk = true; |
||
| 45 | |||
| 46 | public function handle() |
||
| 47 | { |
||
| 48 | $force = $this->option('force'); |
||
| 49 | |||
| 50 | if($this->option('with-export')){ |
||
| 51 | $this->export = true; |
||
| 52 | } |
||
| 53 | |||
| 54 | if($this->option('without-bulk')){ |
||
| 55 | $this->withoutBulk = true; |
||
| 56 | } |
||
| 57 | |||
| 58 | if ($this->generateClass($force)){ |
||
| 59 | |||
| 60 | $this->info('Generating '.$this->classFullName.' finished'); |
||
| 61 | } |
||
| 62 | |||
| 63 | } |
||
| 64 | |||
| 65 | protected function buildClass(): string { |
||
| 66 | |||
| 67 | //Set belongsTo Relations |
||
| 68 | $this->setBelongsToRelations(); |
||
| 69 | |||
| 70 | return view('jig::'.$this->view, [ |
||
| 71 | 'policyBaseName' => $this->classBaseName, |
||
| 72 | 'policyNamespace' => $this->classNamespace, |
||
| 73 | 'modelBaseName' => $this->modelBaseName, |
||
| 74 | 'modelFullName' => $this->modelFullName, |
||
| 75 | 'modelPlural' => $this->modelPlural, |
||
| 76 | 'modelTitle' => $this->titleSingular, |
||
| 77 | 'titlePlural' => $this->titlePlural, |
||
| 78 | 'modelVariableName' => $this->modelVariableName, |
||
| 79 | 'modelVariableNamePlural' => Str::plural($this->modelVariableName), |
||
| 80 | 'modelRouteAndViewName' => $this->modelRouteAndViewName, |
||
| 81 | 'modelViewsDirectory' => $this->modelViewsDirectory, |
||
| 82 | 'modelDotNotation' => $this->modelDotNotation, |
||
| 83 | 'modelWithNamespaceFromDefault' => $this->modelWithNamespaceFromDefault, |
||
| 84 | 'export' => $this->export, |
||
| 85 | 'withoutBulk' => $this->withoutBulk, |
||
| 86 | 'exportBaseName' => $this->exportBaseName, |
||
| 87 | 'resource' => $this->resource, |
||
| 88 | // validation in store/update |
||
| 89 | 'columns' => $this->getVisibleColumns($this->tableName, $this->modelVariableName), |
||
| 90 | 'relations' => $this->relations, |
||
| 91 | 'hasSoftDelete' => $this->readColumnsFromTable($this->tableName)->filter(function($column) { |
||
| 92 | return $column['name'] == "deleted_at"; |
||
| 93 | })->count() > 0, |
||
| 94 | ])->render(); |
||
| 95 | } |
||
| 96 | |||
| 97 | protected function getOptions() { |
||
| 98 | return [ |
||
| 99 | ['model-name', 'm', InputOption::VALUE_OPTIONAL, 'Generates a code for the given model'], |
||
| 100 | ['template', 't', InputOption::VALUE_OPTIONAL, 'Specify custom template'], |
||
| 101 | ['belongs-to-many', 'btm', InputOption::VALUE_OPTIONAL, 'Specify belongs to many relations'], |
||
| 102 | ['force', 'f', InputOption::VALUE_NONE, 'Force will delete files before regenerating controller'], |
||
| 103 | ['model-with-full-namespace', 'fnm', InputOption::VALUE_OPTIONAL, 'Specify model with full namespace'], |
||
| 104 | ['with-export', 'e', InputOption::VALUE_NONE, 'Generate an option to Export as Excel'], |
||
| 105 | ['without-bulk', 'wb', InputOption::VALUE_NONE, 'Generate without bulk options'], |
||
| 106 | ]; |
||
| 107 | } |
||
| 108 | |||
| 109 | public function generateClassNameFromTable($tableName): string |
||
| 110 | { |
||
| 111 | return Str::studly(Str::singular($tableName))."Policy"; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get the default namespace for the class. |
||
| 116 | * |
||
| 117 | * @param string $rootNamespace |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | protected function getDefaultNamespace(string $rootNamespace) : string |
||
| 121 | { |
||
| 122 | return $rootNamespace.'\Policies'; |
||
| 123 | } |
||
| 124 | } |
||
| 125 |