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' => ['type' => 'serial', 'autoIncrement' => true, 'nullable' => false], |
||
24 | 'bigPrimary' => ['type' => 'bigserial', 'autoIncrement' => true, 'nullable' => false], |
||
25 | |||
26 | //Enum type (mapped via method) |
||
27 | 'enum' => 'enum', |
||
28 | |||
29 | //Logical types |
||
30 | 'boolean' => 'boolean', |
||
31 | |||
32 | //Integer types (size can always be changed with size method), longInteger has method alias |
||
33 | //bigInteger |
||
34 | 'integer' => 'integer', |
||
35 | 'tinyInteger' => 'smallint', |
||
36 | 'bigInteger' => 'bigint', |
||
37 | |||
38 | //String with specified length (mapped via method) |
||
39 | 'string' => 'character varying', |
||
40 | |||
41 | //Generic types |
||
42 | 'text' => 'text', |
||
43 | 'tinyText' => 'text', |
||
44 | 'longText' => 'text', |
||
45 | |||
46 | //Real types |
||
47 | 'double' => 'double precision', |
||
48 | 'float' => 'real', |
||
49 | |||
50 | //Decimal type (mapped via method) |
||
51 | 'decimal' => 'numeric', |
||
52 | |||
53 | //Date and Time types |
||
54 | 'datetime' => 'timestamp without time zone', |
||
55 | 'date' => 'date', |
||
56 | 'time' => 'time', |
||
57 | 'timestamp' => 'timestamp without time zone', |
||
58 | |||
59 | //Binary types |
||
60 | 'binary' => 'bytea', |
||
61 | 'tinyBinary' => 'bytea', |
||
62 | 'longBinary' => 'bytea', |
||
63 | |||
64 | //Additional types |
||
65 | 'json' => 'json' |
||
66 | ]; |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | protected $reverseMapping = [ |
||
72 | 'primary' => ['serial'], |
||
73 | 'bigPrimary' => ['bigserial'], |
||
74 | 'enum' => ['enum'], |
||
75 | 'boolean' => ['boolean'], |
||
76 | 'integer' => ['int', 'integer', 'int4'], |
||
77 | 'tinyInteger' => ['smallint'], |
||
78 | 'bigInteger' => ['bigint', 'int8'], |
||
79 | 'string' => ['character varying', 'character'], |
||
80 | 'text' => ['text'], |
||
81 | 'double' => ['double precision'], |
||
82 | 'float' => ['real', 'money'], |
||
83 | 'decimal' => ['numeric'], |
||
84 | 'date' => ['date'], |
||
85 | 'time' => ['time', 'time with time zone', 'time without time zone'], |
||
86 | 'timestamp' => ['timestamp', 'timestamp with time zone', 'timestamp without time zone'], |
||
87 | 'binary' => ['bytea'], |
||
88 | 'json' => ['json'] |
||
89 | ]; |
||
90 | |||
91 | /** |
||
92 | * Field is auto incremental. |
||
93 | * |
||
94 | * @var bool |
||
95 | */ |
||
96 | protected $autoIncrement = false; |
||
97 | |||
98 | /** |
||
99 | * Name of enum constraint associated with field. |
||
100 | * |
||
101 | * @var string |
||
102 | */ |
||
103 | protected $enumConstraint = ''; |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | public function getConstraints() |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | public function abstractType() |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | public function primary() |
||
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | public function bigPrimary() |
||
164 | |||
165 | /** |
||
166 | * {@inheritdoc} |
||
167 | */ |
||
168 | View Code Duplication | public function enum($values) |
|
179 | |||
180 | /** |
||
181 | * {@inheritdoc} |
||
182 | */ |
||
183 | public function sqlStatement() |
||
200 | |||
201 | /** |
||
202 | * Generate set of altering operations should be applied to column to change it's type, size, |
||
203 | * default value or null flag. |
||
204 | * |
||
205 | * @param AbstractColumn $original |
||
206 | * @return array |
||
207 | */ |
||
208 | public function alteringOperations(AbstractColumn $original) |
||
268 | |||
269 | /** |
||
270 | * {@inheritdoc} |
||
271 | */ |
||
272 | protected function resolveSchema($schema) |
||
321 | |||
322 | /** |
||
323 | * {@inheritdoc} |
||
324 | */ |
||
325 | protected function prepareEnum() |
||
329 | |||
330 | /** |
||
331 | * Get name of enum constraint. |
||
332 | * |
||
333 | * @param bool $quote |
||
334 | * @param bool $temporary If true enumConstraint identifier will be generated only for visual |
||
335 | * purposes only. |
||
336 | * @return string |
||
337 | */ |
||
338 | private function enumConstraint($quote = false, $temporary = false) |
||
350 | |||
351 | /** |
||
352 | * Normalize default value. |
||
353 | */ |
||
354 | private function normalizeDefault() |
||
369 | |||
370 | /** |
||
371 | * Resolve native enum type. |
||
372 | **/ |
||
373 | private function resolveNativeEnum() |
||
390 | |||
391 | /** |
||
392 | * Check if column was declared with check constrain. I love name of this method. |
||
393 | * |
||
394 | * @param string $tableOID |
||
395 | */ |
||
396 | private function checkCheckConstrain($tableOID) |
||
422 | } |
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.