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 |
||
| 10 | class JobMakeCommand extends GeneratorCommand |
||
| 11 | { |
||
| 12 | use ModuleCommandTrait; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * The console command name. |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $name = 'module:make-job'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The console command description. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $description = 'Create a new job class for the specified module'; |
||
| 27 | |||
| 28 | protected $argumentName = 'name'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @return string |
||
| 32 | */ |
||
| 33 | 3 | public function getDefaultNamespace() |
|
| 37 | |||
| 38 | /** |
||
| 39 | * Get the console command arguments. |
||
| 40 | * |
||
| 41 | * @return array |
||
| 42 | */ |
||
| 43 | 69 | View Code Duplication | protected function getArguments() |
| 50 | |||
| 51 | /** |
||
| 52 | * Get the console command options. |
||
| 53 | * |
||
| 54 | * @return array |
||
| 55 | */ |
||
| 56 | 69 | protected function getOptions() |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Get template contents. |
||
| 65 | * |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | 3 | protected function getTemplateContents() |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Get the destination file path. |
||
| 80 | * |
||
| 81 | * @return string |
||
| 82 | */ |
||
| 83 | 3 | protected function getDestinationFilePath() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | 3 | private function getFileName() |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | 3 | protected function getStubName(): string |
|
| 111 | } |
||
| 112 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.