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 Serverfireteam\Panel\Commands; |
||
| 6 | class CreateControllerPanelCommand extends GeneratorCommand { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * |
||
| 10 | * @var string contains the command name |
||
| 11 | */ |
||
| 12 | protected $name = 'panel:createcontroller'; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * |
||
| 16 | * @var string contains the description of command |
||
| 17 | */ |
||
| 18 | protected $description = 'Create a new resource controller class'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The type of class being generated. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $type = 'Controller'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Get the stub file for the generator. |
||
| 29 | * |
||
| 30 | * @return string Returns the stub file for generating the Controller |
||
| 31 | */ |
||
| 32 | protected function getStub() |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Get the default namespace for the class. |
||
| 44 | * |
||
| 45 | * @param string $rootNamespace |
||
| 46 | * @return string The namespace of the panel's controllers |
||
| 47 | */ |
||
| 48 | protected function getDefaultNamespace($rootNamespace) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Execute the console command. |
||
| 60 | * |
||
| 61 | * @return void |
||
| 62 | */ |
||
| 63 | View Code Duplication | public function handle() |
|
|
|
|||
| 64 | { |
||
| 65 | $name = $this->qualifyClass($this->getNameInput()) . 'Controller'; |
||
| 66 | |||
| 67 | if ($this->files->exists($path = $this->getPath($name))) |
||
| 68 | { |
||
| 69 | return $this->error($this->type.' already exists!'); |
||
| 70 | } |
||
| 71 | |||
| 72 | $this->makeDirectory($path); |
||
| 73 | |||
| 74 | $this->files->put($path, $this->buildClass($name)); |
||
| 75 | |||
| 76 | $this->info($this->type.' created successfully.'); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Get the console command options. |
||
| 81 | * |
||
| 82 | * @return array |
||
| 83 | */ |
||
| 84 | protected function getOptions() |
||
| 90 | |||
| 91 | } |
||
| 92 |
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.