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 Permissions extends ClassGenerator { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * The name and signature of the console command. |
||
| 10 | * |
||
| 11 | * @var string |
||
| 12 | */ |
||
| 13 | protected $name = 'jig:generate:permissions'; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * The console command description. |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $description = 'Generate permissions migration'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Permissions has also bulk options |
||
| 24 | * |
||
| 25 | * @return mixed |
||
| 26 | */ |
||
| 27 | protected bool $withoutBulk = false; |
||
|
|
|||
| 28 | |||
| 29 | /** |
||
| 30 | * Execute the console command. |
||
| 31 | * |
||
| 32 | * @return void |
||
| 33 | */ |
||
| 34 | public function handle() |
||
| 35 | { |
||
| 36 | $force = $this->option('force'); |
||
| 37 | |||
| 38 | if($this->option('without-bulk')){ |
||
| 39 | $this->withoutBulk = true; |
||
| 40 | } |
||
| 41 | |||
| 42 | if ($this->generateClass($force)){ |
||
| 43 | $this->info('Generating permissions for '.$this->modelBaseName.' finished'); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | protected function generateClass($force = false) : bool { |
||
| 48 | $fileName = 'fill_permissions_for_'.str_replace("-","_",$this->modelRouteAndViewName).'.php'; |
||
| 49 | $path = database_path('migrations/'.date('Y_m_d_His', time()).'_'.$fileName); |
||
| 50 | |||
| 51 | if ($oldPath = $this->alreadyExists($fileName)) { |
||
| 52 | $path = $oldPath; |
||
| 53 | if($force) { |
||
| 54 | $this->warn('File '.$path.' already exists! File will be deleted.'); |
||
| 55 | $this->files->delete($path); |
||
| 56 | } else { |
||
| 57 | $this->error('File '.$path.' already exists!'); |
||
| 58 | return false; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | $this->makeDirectory($path); |
||
| 63 | |||
| 64 | $this->files->put($path, $this->buildClass()); |
||
| 65 | return true; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Determine if the file already exists. |
||
| 70 | * |
||
| 71 | * @param $path |
||
| 72 | * @return bool |
||
| 73 | */ |
||
| 74 | protected function alreadyExists($path): bool |
||
| 75 | { |
||
| 76 | foreach ($this->files->files(database_path('migrations')) as $file) { |
||
| 77 | if(str_contains($file->getFilename(), $path)) { |
||
| 78 | return $file->getPathname(); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | return false; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | protected function buildClass() : string { |
||
| 88 | |||
| 89 | return view('jig::permissions', [ |
||
| 90 | 'modelBaseName' => $this->modelBaseName, |
||
| 91 | 'modelPlural' => $this->modelPlural, |
||
| 92 | 'titlePlural' => $this->titlePlural, |
||
| 93 | 'titleSingular' => $this->titleSingular, |
||
| 94 | 'modelPermissionName' => $this->modelRouteAndViewName, |
||
| 95 | 'modelDotNotation' => $this->modelDotNotation, |
||
| 96 | 'className' => $this->generateClassNameFromTable($this->tableName), |
||
| 97 | 'withoutBulk' => $this->withoutBulk, |
||
| 98 | ])->render(); |
||
| 99 | } |
||
| 100 | |||
| 101 | protected function getOptions() { |
||
| 102 | return [ |
||
| 103 | ['model-name', 'm', InputOption::VALUE_OPTIONAL, 'Generates a code for the given model'], |
||
| 104 | ['force', 'f', InputOption::VALUE_NONE, 'Force will delete files before regenerating request'], |
||
| 105 | ['without-bulk', 'wb', InputOption::VALUE_NONE, 'Generate without bulk options'], |
||
| 106 | ]; |
||
| 107 | } |
||
| 108 | |||
| 109 | public function generateClassNameFromTable($tableName): string |
||
| 110 | { |
||
| 114 |