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 |
||
| 20 | class ModuleCommand extends AbstractGenerateCommand |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Command configuration |
||
| 24 | */ |
||
| 25 | 14 | protected function configure() |
|
| 47 | |||
| 48 | /** |
||
| 49 | * @param InputInterface $input |
||
| 50 | * @param OutputInterface $output |
||
| 51 | * @return void |
||
| 52 | */ |
||
| 53 | 1 | protected function execute(InputInterface $input, OutputInterface $output) |
|
| 54 | { |
||
| 55 | try { |
||
| 56 | 1 | $this->write("Running <info>generate:module</info> command"); |
|
| 57 | |||
| 58 | 1 | $module = $input->getArgument('module'); |
|
| 59 | |||
| 60 | 1 | $argument = $this->getDefinition()->getArgument('module'); |
|
| 61 | 1 | $argument->validate($module); |
|
| 62 | |||
| 63 | // create main folder and subfolders |
||
| 64 | 1 | $this->generate($input, $output); |
|
| 65 | |||
| 66 | // verify it |
||
| 67 | 1 | $this->verify($input, $output); |
|
| 68 | |||
| 69 | 1 | $this->write("Module <info>$module</info> has been successfully created."); |
|
| 70 | |||
| 71 | // create controllers |
||
| 72 | 1 | $controllers = $input->getArgument('controller') ?? []; |
|
| 73 | |||
| 74 | 1 | $command = $this->getApplication()->find('generate:controller'); |
|
| 75 | |||
| 76 | 1 | foreach ($controllers as $controller) { |
|
| 77 | $arguments = [ |
||
| 78 | 'command' => 'generate:controller', |
||
| 79 | 'module' => $module, |
||
| 80 | 'controller' => $controller |
||
| 81 | ]; |
||
| 82 | $greetInput = new ArrayInput($arguments); |
||
| 83 | 1 | $command->run($greetInput, $output); |
|
| 84 | } |
||
| 85 | } catch (\Exception $e) { |
||
| 86 | $this->error("ERROR: {$e->getMessage()}"); |
||
| 87 | } |
||
| 88 | 1 | } |
|
| 89 | |||
| 90 | /** |
||
| 91 | * @param InputInterface $input |
||
| 92 | * @param OutputInterface $output |
||
| 93 | * @return void |
||
| 94 | */ |
||
| 95 | 1 | protected function generate(InputInterface $input, OutputInterface $output) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * @param string $path |
||
| 108 | * @param string[] $subFolders |
||
| 109 | */ |
||
| 110 | 1 | protected function addSubFolders($path, array $subFolders = []) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @param InputInterface $input |
||
| 129 | * @param OutputInterface $output |
||
| 130 | * @return void |
||
| 131 | * @throws GeneratorException |
||
| 132 | */ |
||
| 133 | 1 | View Code Duplication | public function verify(InputInterface $input, OutputInterface $output) |
| 149 | } |
||
| 150 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.