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:
Complex classes like AbstractAdapter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | abstract class AbstractAdapter implements AdapterInterface |
||
23 | { |
||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $options = []; |
||
28 | |||
29 | /** |
||
30 | * @var \Symfony\Component\Console\Input\InputInterface |
||
31 | */ |
||
32 | protected $input; |
||
33 | |||
34 | /** |
||
35 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
36 | */ |
||
37 | protected $output; |
||
38 | |||
39 | /** |
||
40 | * @var string[] |
||
41 | */ |
||
42 | protected $createdTables = []; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $schemaTableName = 'phinxlog'; |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $dataDomain = []; |
||
53 | |||
54 | /** |
||
55 | * Class Constructor. |
||
56 | * |
||
57 | * @param array $options Options |
||
58 | * @param \Symfony\Component\Console\Input\InputInterface|null $input Input Interface |
||
59 | * @param \Symfony\Component\Console\Output\OutputInterface|null $output Output Interface |
||
60 | */ |
||
61 | View Code Duplication | public function __construct(array $options, InputInterface $input = null, OutputInterface $output = null) |
|
71 | 341 | ||
72 | 341 | /** |
|
73 | 264 | * @inheritDoc |
|
74 | 264 | */ |
|
75 | 341 | public function setOptions(array $options) |
|
89 | 6 | ||
90 | /** |
||
91 | 287 | * @inheritDoc |
|
92 | */ |
||
93 | public function getOptions() |
||
97 | 196 | ||
98 | /** |
||
99 | 196 | * @inheritDoc |
|
100 | */ |
||
101 | public function hasOption($name) |
||
105 | 8 | ||
106 | /** |
||
107 | 8 | * @inheritDoc |
|
108 | */ |
||
109 | public function getOption($name) |
||
117 | |||
118 | /** |
||
119 | * @inheritDoc |
||
120 | */ |
||
121 | public function setInput(InputInterface $input) |
||
127 | 269 | ||
128 | /** |
||
129 | * @inheritDoc |
||
130 | */ |
||
131 | public function getInput() |
||
135 | 218 | ||
136 | /** |
||
137 | * @inheritDoc |
||
138 | */ |
||
139 | public function setOutput(OutputInterface $output) |
||
145 | |||
146 | /** |
||
147 | * @inheritDoc |
||
148 | */ |
||
149 | public function getOutput() |
||
158 | |||
159 | /** |
||
160 | * @inheritDoc |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | public function getVersions() |
||
170 | |||
171 | /** |
||
172 | * Gets the schema table name. |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | 195 | public function getSchemaTableName() |
|
180 | |||
181 | /** |
||
182 | * Sets the schema table name. |
||
183 | * |
||
184 | * @param string $schemaTableName Schema Table Name |
||
185 | * |
||
186 | * @return $this |
||
187 | 7 | */ |
|
188 | public function setSchemaTableName($schemaTableName) |
||
194 | |||
195 | /** |
||
196 | 193 | * Gets the data domain. |
|
197 | * |
||
198 | 193 | * @return array |
|
199 | */ |
||
200 | public function getDataDomain() |
||
204 | 191 | ||
205 | /** |
||
206 | * Sets the data domain. |
||
207 | * |
||
208 | 191 | * @param array $dataDomain Array for the data domain |
|
209 | * @return $this |
||
210 | 191 | */ |
|
211 | public function setDataDomain(array $dataDomain) |
||
270 | |||
271 | /** |
||
272 | * @inheritdoc |
||
273 | */ |
||
274 | public function getColumnForType($columnName, $type, array $options) |
||
290 | |||
291 | /** |
||
292 | * @inheritdoc |
||
293 | */ |
||
294 | public function hasSchemaTable() |
||
298 | |||
299 | /** |
||
300 | * @inheritDoc |
||
301 | * |
||
302 | * @throws \InvalidArgumentException |
||
303 | * |
||
304 | * @return void |
||
305 | */ |
||
306 | public function createSchemaTable() |
||
329 | |||
330 | /** |
||
331 | * @inheritDoc |
||
332 | */ |
||
333 | public function getAdapterType() |
||
337 | |||
338 | /** |
||
339 | * @inheritDoc |
||
340 | */ |
||
341 | public function isValidColumnType(Column $column) |
||
345 | |||
346 | /** |
||
347 | * Determines if instead of executing queries a dump to standard output is needed |
||
348 | * |
||
349 | * @return bool |
||
350 | */ |
||
351 | public function isDryRunEnabled() |
||
358 | |||
359 | /** |
||
360 | * Adds user-created tables (e.g. not phinxlog) to a cached list |
||
361 | * |
||
362 | * @param string $tableName The name of the table |
||
363 | * |
||
364 | * @return void |
||
365 | */ |
||
366 | protected function addCreatedTable($tableName) |
||
372 | |||
373 | /** |
||
374 | * Updates the name of the cached table |
||
375 | * |
||
376 | * @param string $tableName Original name of the table |
||
377 | * @param string $newTableName New name of the table |
||
378 | * |
||
379 | * @return void |
||
380 | */ |
||
381 | View Code Duplication | protected function updateCreatedTableName($tableName, $newTableName) |
|
388 | |||
389 | /** |
||
390 | * Removes table from the cached created list |
||
391 | * |
||
392 | * @param string $tableName The name of the table |
||
393 | * |
||
394 | * @return void |
||
395 | */ |
||
396 | View Code Duplication | protected function removeCreatedTable($tableName) |
|
403 | |||
404 | /** |
||
405 | * Check if the table is in the cached list of created tables |
||
406 | * |
||
407 | * @param string $tableName The name of the table |
||
408 | * |
||
409 | * @return bool |
||
410 | */ |
||
411 | protected function hasCreatedTable($tableName) |
||
415 | } |
||
416 |
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.