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 MakeTableCommand extends AbstractGeneratorCommand |
||
21 | { |
||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | private $columnTypes = [ |
||
26 | Table::TYPE_BOOLEAN, |
||
27 | Table::TYPE_SMALLINT, |
||
28 | Table::TYPE_INTEGER, |
||
29 | Table::TYPE_BIGINT, |
||
30 | Table::TYPE_FLOAT, |
||
31 | Table::TYPE_NUMERIC, |
||
32 | Table::TYPE_DECIMAL, |
||
33 | Table::TYPE_DATE, |
||
34 | Table::TYPE_TIMESTAMP, |
||
35 | Table::TYPE_DATETIME, |
||
36 | Table::TYPE_TEXT, |
||
37 | Table::TYPE_BLOB, |
||
38 | Table::TYPE_VARBINARY, |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | private $identityColumn = null; |
||
45 | |||
46 | protected function configure() |
||
53 | |||
54 | /** |
||
55 | * @param InputInterface $input |
||
56 | * @param OutputInterface $output |
||
57 | * |
||
58 | * @return int|void |
||
59 | */ |
||
60 | protected function execute(InputInterface $input, OutputInterface $output) |
||
73 | |||
74 | /** |
||
75 | * @param InputInterface $input |
||
76 | * @param OutputInterface $output |
||
77 | * @param QuestionHelper $questionHelper |
||
78 | * @param DDLTable $table |
||
79 | * @return void |
||
80 | */ |
||
81 | private function processColumns( |
||
94 | |||
95 | /** |
||
96 | * @param InputInterface $input |
||
97 | * @param OutputInterface $output |
||
98 | * @param QuestionHelper $questionHelper |
||
99 | * @return DDLTableColumn |
||
100 | */ |
||
101 | private function processColumn( |
||
119 | |||
120 | /** |
||
121 | * @param InputInterface $input |
||
122 | * @param OutputInterface $output |
||
123 | * @param QuestionHelper $questionHelper |
||
124 | * @param DDLTableColumn $column |
||
125 | * @return void |
||
126 | */ |
||
127 | View Code Duplication | private function askForColumnName( |
|
141 | |||
142 | /** |
||
143 | * @param InputInterface $input |
||
144 | * @param OutputInterface $output |
||
145 | * @param QuestionHelper $questionHelper |
||
146 | * @param DDLTableColumn $column |
||
147 | * @return void |
||
148 | */ |
||
149 | private function askForIdentityColumn( |
||
166 | |||
167 | /** |
||
168 | * @param InputInterface $input |
||
169 | * @param OutputInterface $output |
||
170 | * @param QuestionHelper $questionHelper |
||
171 | * @param DDLTableColumn $data |
||
172 | * @return void |
||
173 | */ |
||
174 | private function askForColumnType( |
||
182 | |||
183 | /** |
||
184 | * @param InputInterface $input |
||
185 | * @param OutputInterface $output |
||
186 | * @param QuestionHelper $questionHelper |
||
187 | * @param DDLTableColumn $column |
||
188 | * @return void |
||
189 | */ |
||
190 | View Code Duplication | private function askForColumnIsUnsigned( |
|
207 | |||
208 | /** |
||
209 | * @param InputInterface $input |
||
210 | * @param OutputInterface $output |
||
211 | * @param QuestionHelper $questionHelper |
||
212 | * @param DDLTableColumn $column |
||
213 | * @return void |
||
214 | */ |
||
215 | private function askForColumnSize( |
||
249 | |||
250 | /** |
||
251 | * @param InputInterface $input |
||
252 | * @param OutputInterface $output |
||
253 | * @param QuestionHelper $questionHelper |
||
254 | * @param DDLTableColumn $column |
||
255 | * @return void |
||
256 | */ |
||
257 | View Code Duplication | private function askForColumnIsNullable( |
|
270 | |||
271 | /** |
||
272 | * @param InputInterface $input |
||
273 | * @param OutputInterface $output |
||
274 | * @param QuestionHelper $questionHelper |
||
275 | * @param DDLTableColumn $column |
||
276 | * @return void |
||
277 | */ |
||
278 | private function askForColumnDefault( |
||
297 | |||
298 | /** |
||
299 | * @param InputInterface $input |
||
300 | * @param OutputInterface $output |
||
301 | * @param QuestionHelper $questionHelper |
||
302 | * @param DDLTableColumn $column |
||
303 | * @return void |
||
304 | */ |
||
305 | View Code Duplication | private function askForColumnComment( |
|
320 | |||
321 | /** |
||
322 | * @param InputInterface $input |
||
323 | * @param OutputInterface $output |
||
324 | * @param $questionHelper |
||
325 | * @param DDLTable $table |
||
326 | * @return void |
||
327 | */ |
||
328 | View Code Duplication | private function askForTableName(InputInterface $input, OutputInterface $output, $questionHelper, DDLTable $table) |
|
342 | |||
343 | /** |
||
344 | * @param InputInterface $input |
||
345 | * @param OutputInterface $output |
||
346 | * @param $questionHelper |
||
347 | * @param DDLTable $column |
||
348 | * @return void |
||
349 | */ |
||
350 | View Code Duplication | private function askForTableComment( |
|
365 | |||
366 | /** |
||
367 | * @param InputInterface $input |
||
368 | * @param OutputInterface $output |
||
369 | * @param DDLTable $table |
||
370 | * @return void |
||
371 | */ |
||
372 | private function generateCode( |
||
379 | } |
||
380 |
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.