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 ColumnSchema 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 ColumnSchema, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class ColumnSchema extends AbstractColumn |
||
17 | { |
||
18 | /** |
||
19 | * {@inheritdoc} |
||
20 | */ |
||
21 | protected $mapping = [ |
||
22 | //Primary sequences |
||
23 | 'primary' => [ |
||
24 | 'type' => 'int', |
||
25 | 'identity' => true, |
||
26 | 'nullable' => false |
||
27 | ], |
||
28 | 'bigPrimary' => [ |
||
29 | 'type' => 'bigint', |
||
30 | 'identity' => true, |
||
31 | 'nullable' => false |
||
32 | ], |
||
33 | |||
34 | //Enum type (mapped via method) |
||
35 | 'enum' => 'enum', |
||
36 | |||
37 | //Logical types |
||
38 | 'boolean' => 'bit', |
||
39 | |||
40 | //Integer types (size can always be changed with size method), longInteger has method alias |
||
41 | //bigInteger |
||
42 | 'integer' => 'int', |
||
43 | 'tinyInteger' => 'tinyint', |
||
44 | 'bigInteger' => 'bigint', |
||
45 | |||
46 | //String with specified length (mapped via method) |
||
47 | 'string' => 'varchar', |
||
48 | |||
49 | //Generic types |
||
50 | 'text' => ['type' => 'varchar', 'size' => 0], |
||
51 | 'tinyText' => ['type' => 'varchar', 'size' => 0], |
||
52 | 'longText' => ['type' => 'varchar', 'size' => 0], |
||
53 | |||
54 | //Real types |
||
55 | 'double' => 'float', |
||
56 | 'float' => 'real', |
||
57 | |||
58 | //Decimal type (mapped via method) |
||
59 | 'decimal' => 'decimal', |
||
60 | |||
61 | //Date and Time types |
||
62 | 'datetime' => 'datetime', |
||
63 | 'date' => 'date', |
||
64 | 'time' => 'time', |
||
65 | 'timestamp' => 'datetime', |
||
66 | |||
67 | //Binary types |
||
68 | 'binary' => ['type' => 'varbinary', 'size' => 0], |
||
69 | 'tinyBinary' => ['type' => 'varbinary', 'size' => 0], |
||
70 | 'longBinary' => ['type' => 'varbinary', 'size' => 0], |
||
71 | |||
72 | //Additional types |
||
73 | 'json' => ['type' => 'varchar', 'size' => 0] |
||
74 | ]; |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | protected $reverseMapping = [ |
||
80 | 'primary' => [['type' => 'int', 'identity' => true]], |
||
81 | 'bigPrimary' => [['type' => 'bigint', 'identity' => true]], |
||
82 | 'enum' => ['enum'], |
||
83 | 'boolean' => ['bit'], |
||
84 | 'integer' => ['int'], |
||
85 | 'tinyInteger' => ['tinyint', 'smallint'], |
||
86 | 'bigInteger' => ['bigint'], |
||
87 | 'text' => [['type' => 'varchar', 'size' => 0]], |
||
88 | 'string' => ['varchar', 'char'], |
||
89 | 'double' => ['float'], |
||
90 | 'float' => ['real'], |
||
91 | 'decimal' => ['decimal'], |
||
92 | 'timestamp' => ['datetime'], |
||
93 | 'date' => ['date'], |
||
94 | 'time' => ['time'], |
||
95 | 'binary' => ['varbinary'], |
||
96 | ]; |
||
97 | |||
98 | /** |
||
99 | * Field is table identity. |
||
100 | * |
||
101 | * @var bool |
||
102 | */ |
||
103 | protected $identity = false; |
||
104 | |||
105 | /** |
||
106 | * Name of default constraint. |
||
107 | * |
||
108 | * @var string |
||
109 | */ |
||
110 | protected $defaultConstraint = ''; |
||
111 | |||
112 | /** |
||
113 | * Name of enum constraint. |
||
114 | * |
||
115 | * @var string |
||
116 | */ |
||
117 | protected $enumConstraint = ''; |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | public function getConstraints() |
||
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | public function abstractType() |
||
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | View Code Duplication | public function enum($values) |
|
164 | |||
165 | /** |
||
166 | * {@inheritdoc} |
||
167 | * |
||
168 | * @param bool $ignoreEnum If true ENUM declaration statement will be returned only. Internal |
||
169 | * helper. |
||
170 | */ |
||
171 | public function sqlStatement($ignoreEnum = false) |
||
199 | |||
200 | /** |
||
201 | * Generate set of altering operations should be applied to column to change it's type, size, |
||
202 | * default value or null flag. |
||
203 | * |
||
204 | * @param ColumnSchema $initial |
||
205 | * @return array |
||
206 | */ |
||
207 | public function alteringOperations(ColumnSchema $initial) |
||
271 | |||
272 | /** |
||
273 | * {@inheritdoc} |
||
274 | */ |
||
275 | protected function resolveSchema($schema) |
||
315 | |||
316 | /** |
||
317 | * {@inheritdoc} |
||
318 | */ |
||
319 | protected function prepareDefault() |
||
328 | |||
329 | /** |
||
330 | * Get name of enum constraint. |
||
331 | * |
||
332 | * @param bool $quoted True to quote identifier. |
||
333 | * @return string |
||
334 | */ |
||
335 | View Code Duplication | protected function enumConstraint($quoted = false) |
|
345 | |||
346 | /** |
||
347 | * Default constrain name. |
||
348 | * |
||
349 | * @param bool $quoted |
||
350 | * @return string |
||
351 | */ |
||
352 | View Code Duplication | protected function defaultConstrain($quoted = false) |
|
362 | |||
363 | /** |
||
364 | * Enum constrain statement. |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | private function enumStatement() |
||
380 | |||
381 | /** |
||
382 | * Normalizing default value. |
||
383 | */ |
||
384 | private function normalizeDefault() |
||
409 | |||
410 | /** |
||
411 | * Check if column is enum. |
||
412 | * |
||
413 | * @param array $schema |
||
414 | * @param Driver $tableDriver |
||
415 | */ |
||
416 | private function resolveEnum(array $schema, $tableDriver) |
||
442 | |||
443 | /** |
||
444 | * Generate constrain name. |
||
445 | * |
||
446 | * @param string $type |
||
447 | * @return string |
||
448 | */ |
||
449 | private function generateName($type) |
||
453 | } |
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.