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 |
||
19 | class ScaffoldCommand extends AbstractGenerateCommand |
||
20 | { |
||
21 | /** |
||
22 | * Command configuration |
||
23 | */ |
||
24 | 14 | protected function configure() |
|
25 | { |
||
26 | $this |
||
27 | // the name of the command (the part after "bin/bluzman") |
||
28 | 14 | ->setName('generate:scaffold') |
|
29 | // the short description shown while running "php bin/bluzman list" |
||
30 | 14 | ->setDescription('Generate a new model and module with crud and grid') |
|
31 | // the full command description shown when running the command with |
||
32 | // the "--help" option |
||
33 | 14 | ->setHelp('This command allows you to generate a scaffolding') |
|
34 | ; |
||
35 | |||
36 | 14 | $this->addModelArgument(); |
|
37 | 14 | $this->addTableArgument(); |
|
38 | 14 | $this->addModuleArgument(); |
|
39 | 14 | $this->addForceOption(); |
|
40 | 14 | } |
|
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | protected function execute(InputInterface $input, OutputInterface $output) : void |
||
46 | { |
||
47 | $this->write('Running <info>generate:scaffold</info> command'); |
||
48 | try { |
||
49 | // generate |
||
50 | $this->runGenerateModel(); |
||
51 | $this->runGenerateModule(); |
||
52 | $this->runGenerateCrud(); |
||
53 | $this->runGenerateGrid(); |
||
54 | |||
55 | // verify it |
||
56 | $this->verify($input, $output); |
||
57 | } catch (\Exception $e) { |
||
58 | $this->error("ERROR: {$e->getMessage()}"); |
||
59 | } |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Generate Model |
||
64 | * |
||
65 | * @return void |
||
66 | */ |
||
67 | View Code Duplication | protected function runGenerateModel() : void |
|
81 | |||
82 | /** |
||
83 | * Generate Module |
||
84 | * |
||
85 | * @return void |
||
86 | */ |
||
87 | protected function runGenerateModule() : void |
||
100 | |||
101 | /** |
||
102 | * Generate Crud |
||
103 | * |
||
104 | * @return void |
||
105 | */ |
||
106 | View Code Duplication | protected function runGenerateCrud() : void |
|
120 | |||
121 | /** |
||
122 | * Generate Grid |
||
123 | * |
||
124 | * @return void |
||
125 | */ |
||
126 | View Code Duplication | protected function runGenerateGrid() : void |
|
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | * |
||
144 | * @throws GeneratorException |
||
145 | */ |
||
146 | public function verify(InputInterface $input, OutputInterface $output) : void |
||
173 | } |
||
174 |
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.