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 Index 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 Index, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class Index extends AbstractAsset implements Constraint |
||
25 | { |
||
26 | /** |
||
27 | * Asset identifier instances of the column names the index is associated with. |
||
28 | * array($columnName => Identifier) |
||
29 | * |
||
30 | * @var Identifier[] |
||
31 | */ |
||
32 | protected $_columns = array(); |
||
33 | |||
34 | /** |
||
35 | * @var boolean |
||
36 | */ |
||
37 | protected $_isUnique = false; |
||
38 | |||
39 | /** |
||
40 | * @var boolean |
||
41 | */ |
||
42 | protected $_isPrimary = false; |
||
43 | |||
44 | /** |
||
45 | * Platform specific flags for indexes. |
||
46 | * array($flagName => true) |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $_flags = array(); |
||
51 | |||
52 | /** |
||
53 | * Platform specific options |
||
54 | * |
||
55 | * @todo $_flags should eventually be refactored into options |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | private $options = array(); |
||
60 | |||
61 | /** |
||
62 | * @param string $indexName |
||
63 | * @param string[] $columns |
||
64 | * @param boolean $isUnique |
||
65 | * @param boolean $isPrimary |
||
66 | * @param string[] $flags |
||
67 | * @param array $options |
||
68 | */ |
||
69 | 621 | public function __construct($indexName, array $columns, $isUnique = false, $isPrimary = false, array $flags = array(), array $options = array()) |
|
85 | |||
86 | /** |
||
87 | * @param string $column |
||
88 | * |
||
89 | * @return void |
||
90 | * |
||
91 | * @throws \InvalidArgumentException |
||
92 | */ |
||
93 | 600 | protected function _addColumn($column) |
|
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | 306 | public function getColumns() |
|
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | 379 | View Code Duplication | public function getQuotedColumns(AbstractPlatform $platform) |
123 | |||
124 | /** |
||
125 | * @return string[] |
||
126 | */ |
||
127 | 1 | public function getUnquotedColumns() |
|
131 | |||
132 | /** |
||
133 | * Is the index neither unique nor primary key? |
||
134 | * |
||
135 | * @return boolean |
||
136 | */ |
||
137 | 1 | public function isSimpleIndex() |
|
141 | |||
142 | /** |
||
143 | * @return boolean |
||
144 | */ |
||
145 | 276 | public function isUnique() |
|
149 | |||
150 | /** |
||
151 | * @return boolean |
||
152 | */ |
||
153 | 576 | public function isPrimary() |
|
157 | |||
158 | /** |
||
159 | * @param string $columnName |
||
160 | * @param integer $pos |
||
161 | * |
||
162 | * @return boolean |
||
163 | */ |
||
164 | 1 | public function hasColumnAtPosition($columnName, $pos = 0) |
|
171 | |||
172 | /** |
||
173 | * Checks if this index exactly spans the given column names in the correct order. |
||
174 | * |
||
175 | * @param array $columnNames |
||
176 | * |
||
177 | * @return boolean |
||
178 | */ |
||
179 | 120 | public function spansColumns(array $columnNames) |
|
193 | |||
194 | /** |
||
195 | * Checks if the other index already fulfills all the indexing and constraint needs of the current one. |
||
196 | * |
||
197 | * @param Index $other |
||
198 | * |
||
199 | * @return boolean |
||
200 | */ |
||
201 | 120 | public function isFullfilledBy(Index $other) |
|
246 | |||
247 | /** |
||
248 | * Detects if the other index is a non-unique, non primary index that can be overwritten by this one. |
||
249 | * |
||
250 | * @param Index $other |
||
251 | * |
||
252 | * @return boolean |
||
253 | */ |
||
254 | 1 | public function overrules(Index $other) |
|
268 | |||
269 | /** |
||
270 | * Returns platform specific flags for indexes. |
||
271 | * |
||
272 | * @return string[] |
||
273 | */ |
||
274 | 12 | public function getFlags() |
|
278 | |||
279 | /** |
||
280 | * Adds Flag for an index that translates to platform specific handling. |
||
281 | * |
||
282 | * @example $index->addFlag('CLUSTERED') |
||
283 | * |
||
284 | * @param string $flag |
||
285 | * |
||
286 | * @return Index |
||
287 | */ |
||
288 | 33 | public function addFlag($flag) |
|
294 | |||
295 | /** |
||
296 | * Does this index have a specific flag? |
||
297 | * |
||
298 | * @param string $flag |
||
299 | * |
||
300 | * @return boolean |
||
301 | */ |
||
302 | 147 | public function hasFlag($flag) |
|
306 | |||
307 | /** |
||
308 | * Removes a flag. |
||
309 | * |
||
310 | * @param string $flag |
||
311 | * |
||
312 | * @return void |
||
313 | */ |
||
314 | 1 | public function removeFlag($flag) |
|
318 | |||
319 | /** |
||
320 | * @param string $name |
||
321 | * |
||
322 | * @return boolean |
||
323 | */ |
||
324 | 120 | public function hasOption($name) |
|
328 | |||
329 | /** |
||
330 | * @param string $name |
||
331 | * |
||
332 | * @return mixed |
||
333 | */ |
||
334 | 7 | public function getOption($name) |
|
338 | |||
339 | /** |
||
340 | * @return array |
||
341 | */ |
||
342 | 12 | public function getOptions() |
|
346 | |||
347 | /** |
||
348 | * Return whether the two indexes have the same partial index |
||
349 | * @param \Doctrine\DBAL\Schema\Index $other |
||
350 | * |
||
351 | * @return boolean |
||
352 | */ |
||
353 | 87 | private function samePartialIndex(Index $other) |
|
365 | |||
366 | } |
||
367 |
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.