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 |
||
41 | abstract class AbstractAdapter implements AdapterInterface |
||
42 | { |
||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $options = []; |
||
47 | |||
48 | /** |
||
49 | * @var \Symfony\Component\Console\Input\InputInterface |
||
50 | */ |
||
51 | protected $input; |
||
52 | |||
53 | /** |
||
54 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
55 | */ |
||
56 | protected $output; |
||
57 | |||
58 | /** |
||
59 | * @var string[] |
||
60 | */ |
||
61 | protected $createdTables = []; |
||
62 | |||
63 | /** |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $schemaTableName = 'phinxlog'; |
||
67 | |||
68 | /** |
||
69 | 341 | * Class Constructor. |
|
70 | * |
||
71 | 341 | * @param array $options Options |
|
72 | 341 | * @param \Symfony\Component\Console\Input\InputInterface $input Input Interface |
|
73 | 264 | * @param \Symfony\Component\Console\Output\OutputInterface $output Output Interface |
|
74 | 264 | */ |
|
75 | 341 | public function __construct(array $options, InputInterface $input = null, OutputInterface $output = null) |
|
85 | 287 | ||
86 | /** |
||
87 | 287 | * {@inheritdoc} |
|
88 | 6 | */ |
|
89 | 6 | public function setOptions(array $options) |
|
99 | 196 | ||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | public function getOptions() |
||
107 | 8 | ||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | public function hasOption($name) |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | public function getOption($name) |
||
127 | 269 | ||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | public function setInput(InputInterface $input) |
||
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | 269 | public function getInput() |
|
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | public function setOutput(OutputInterface $output) |
||
155 | |||
156 | 8 | /** |
|
157 | * {@inheritdoc} |
||
158 | */ |
||
159 | public function getOutput() |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | * |
||
172 | * @return array |
||
173 | */ |
||
174 | public function getVersions() |
||
180 | |||
181 | /** |
||
182 | * Gets the schema table name. |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | public function getSchemaTableName() |
||
190 | 7 | ||
191 | /** |
||
192 | * Sets the schema table name. |
||
193 | * |
||
194 | * @param string $schemaTableName Schema Table Name |
||
195 | * @return $this |
||
196 | 193 | */ |
|
197 | public function setSchemaTableName($schemaTableName) |
||
203 | |||
204 | 191 | /** |
|
205 | * {@inheritdoc} |
||
206 | */ |
||
207 | public function hasSchemaTable() |
||
211 | |||
212 | 191 | /** |
|
213 | 191 | * {@inheritdoc} |
|
214 | 191 | */ |
|
215 | 191 | public function createSchemaTable() |
|
238 | |||
239 | /** |
||
240 | * {@inheritdoc} |
||
241 | */ |
||
242 | public function getAdapterType() |
||
246 | |||
247 | 218 | /** |
|
248 | * {@inheritdoc} |
||
249 | 218 | */ |
|
250 | public function isValidColumnType(Column $column) |
||
254 | |||
255 | /** |
||
256 | * Determines if instead of executing queries a dump to standard output is needed |
||
257 | * |
||
258 | * @return bool |
||
259 | */ |
||
260 | public function isDryRunEnabled() |
||
266 | |||
267 | /** |
||
268 | * Adds user-created tables (e.g. not phinxlog) to a cached list |
||
269 | * |
||
270 | * @param string $tableName The name of the table |
||
271 | * @return void |
||
272 | */ |
||
273 | protected function addCreatedTable($tableName) |
||
279 | |||
280 | /** |
||
281 | * Updates the name of the cached table |
||
282 | * |
||
283 | * @param string $tableName Original name of the table |
||
284 | * @param string $newTableName New name of the table |
||
285 | * @return void |
||
286 | */ |
||
287 | View Code Duplication | protected function updateCreatedTableName($tableName, $newTableName) |
|
294 | |||
295 | /** |
||
296 | * Removes table from the cached created list |
||
297 | * |
||
298 | * @param string $tableName The name of the table |
||
299 | * @return void |
||
300 | */ |
||
301 | View Code Duplication | protected function removeCreatedTable($tableName) |
|
308 | |||
309 | /** |
||
310 | * Check if the table is in the cached list of created tables |
||
311 | * |
||
312 | * @param string $tableName The name of the table |
||
313 | * @return bool |
||
314 | */ |
||
315 | protected function hasCreatedTable($tableName) |
||
319 | } |
||
320 |
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.