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 |
||
| 18 | class ModuleCommand extends AbstractGenerateCommand |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Command configuration |
||
| 22 | */ |
||
| 23 | 15 | protected function configure() |
|
| 38 | |||
| 39 | /** |
||
| 40 | * @param InputInterface $input |
||
| 41 | * @param OutputInterface $output |
||
| 42 | * @return void |
||
| 43 | */ |
||
| 44 | 2 | View Code Duplication | protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|||
| 45 | { |
||
| 46 | 2 | $this->write('Running <info>generate:module</info> command'); |
|
| 47 | try { |
||
| 48 | // validate |
||
| 49 | 2 | $this->validateModuleArgument(); |
|
| 50 | |||
| 51 | // create main folder and subfolders |
||
| 52 | 1 | $this->generate($input, $output); |
|
| 53 | |||
| 54 | // verify it |
||
| 55 | 1 | $this->verify($input, $output); |
|
| 56 | 1 | } catch (\Exception $e) { |
|
| 57 | 1 | $this->error("ERROR: {$e->getMessage()}"); |
|
| 58 | } |
||
| 59 | 2 | } |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @param InputInterface $input |
||
| 63 | * @param OutputInterface $output |
||
| 64 | * @return void |
||
| 65 | */ |
||
| 66 | 1 | protected function generate(InputInterface $input, OutputInterface $output) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * @param string $path |
||
| 80 | * @param string[] $subFolders |
||
| 81 | */ |
||
| 82 | 1 | protected function createSubFolders($path, array $subFolders = []) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @param InputInterface $input |
||
| 101 | * @param OutputInterface $output |
||
| 102 | * @return void |
||
| 103 | * @throws GeneratorException |
||
| 104 | */ |
||
| 105 | 1 | View Code Duplication | public function verify(InputInterface $input, OutputInterface $output) : void |
| 124 | } |
||
| 125 |
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.