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 PostgresColumn 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 PostgresColumn, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class PostgresColumn extends AbstractColumn |
||
19 | { |
||
20 | /** |
||
21 | * Default timestamp expression (driver specific). |
||
22 | */ |
||
23 | const DATETIME_NOW = 'now()'; |
||
24 | |||
25 | /** |
||
26 | * Private state related values. |
||
27 | */ |
||
28 | const EXCLUDE_FROM_COMPARE = [ |
||
29 | 'timezone', |
||
30 | 'constrained', |
||
31 | 'constrainName' |
||
32 | ]; |
||
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | protected $mapping = [ |
||
38 | //Primary sequences |
||
39 | 'primary' => ['type' => 'serial', 'autoIncrement' => true, 'nullable' => false], |
||
40 | 'bigPrimary' => ['type' => 'bigserial', 'autoIncrement' => true, 'nullable' => false], |
||
41 | |||
42 | //Enum type (mapped via method) |
||
43 | 'enum' => 'enum', |
||
44 | |||
45 | //Logical types |
||
46 | 'boolean' => 'boolean', |
||
47 | |||
48 | //Integer types (size can always be changed with size method), longInteger has method alias |
||
49 | //bigInteger |
||
50 | 'integer' => 'integer', |
||
51 | 'tinyInteger' => 'smallint', |
||
52 | 'bigInteger' => 'bigint', |
||
53 | |||
54 | //String with specified length (mapped via method) |
||
55 | 'string' => 'character varying', |
||
56 | |||
57 | //Generic types |
||
58 | 'text' => 'text', |
||
59 | 'tinyText' => 'text', |
||
60 | 'longText' => 'text', |
||
61 | |||
62 | //Real types |
||
63 | 'double' => 'double precision', |
||
64 | 'float' => 'real', |
||
65 | |||
66 | //Decimal type (mapped via method) |
||
67 | 'decimal' => 'numeric', |
||
68 | |||
69 | //Date and Time types |
||
70 | 'datetime' => 'timestamp without time zone', |
||
71 | 'date' => 'date', |
||
72 | 'time' => 'time without time zone', |
||
73 | 'timestamp' => 'timestamp without time zone', |
||
74 | |||
75 | //Binary types |
||
76 | 'binary' => 'bytea', |
||
77 | 'tinyBinary' => 'bytea', |
||
78 | 'longBinary' => 'bytea', |
||
79 | |||
80 | //Additional types |
||
81 | 'json' => 'text', |
||
82 | ]; |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | protected $reverseMapping = [ |
||
88 | 'primary' => ['serial'], |
||
89 | 'bigPrimary' => ['bigserial'], |
||
90 | 'enum' => ['enum'], |
||
91 | 'boolean' => ['boolean'], |
||
92 | 'integer' => ['int', 'integer', 'int4'], |
||
93 | 'tinyInteger' => ['smallint'], |
||
94 | 'bigInteger' => ['bigint', 'int8'], |
||
95 | 'string' => ['character varying', 'character'], |
||
96 | 'text' => ['text'], |
||
97 | 'double' => ['double precision'], |
||
98 | 'float' => ['real', 'money'], |
||
99 | 'decimal' => ['numeric'], |
||
100 | 'date' => ['date'], |
||
101 | 'time' => ['time', 'time with time zone', 'time without time zone'], |
||
102 | 'timestamp' => ['timestamp', 'timestamp with time zone', 'timestamp without time zone'], |
||
103 | 'binary' => ['bytea'], |
||
104 | 'json' => ['text'], |
||
105 | ]; |
||
106 | |||
107 | /** |
||
108 | * Field is auto incremental. |
||
109 | * |
||
110 | * @var bool |
||
111 | */ |
||
112 | protected $autoIncrement = false; |
||
113 | |||
114 | /** |
||
115 | * Indication that column have enum constrain. |
||
116 | * |
||
117 | * @var bool |
||
118 | */ |
||
119 | protected $constrained = false; |
||
120 | |||
121 | /** |
||
122 | * Name of enum constraint associated with field. |
||
123 | * |
||
124 | * @var string |
||
125 | */ |
||
126 | protected $constrainName = ''; |
||
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | public function getConstraints(): array |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | public function abstractType(): string |
||
153 | |||
154 | /** |
||
155 | * {@inheritdoc} |
||
156 | */ |
||
157 | public function primary(): AbstractColumn |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | public function bigPrimary(): AbstractColumn |
||
183 | |||
184 | /** |
||
185 | * {@inheritdoc} |
||
186 | */ |
||
187 | View Code Duplication | public function enum($values): AbstractColumn |
|
198 | |||
199 | /** |
||
200 | * {@inheritdoc} |
||
201 | */ |
||
202 | public function sqlStatement(Driver $driver): string |
||
223 | |||
224 | /** |
||
225 | * Generate set of operations need to change column. |
||
226 | * |
||
227 | * @param Driver $driver |
||
228 | * @param AbstractColumn $initial |
||
229 | * |
||
230 | * @return array |
||
231 | */ |
||
232 | public function alterOperations(Driver $driver, AbstractColumn $initial): array |
||
300 | |||
301 | /** |
||
302 | * {@inheritdoc} |
||
303 | */ |
||
304 | protected function quoteEnum(Driver $driver): string |
||
309 | |||
310 | /** |
||
311 | * Get/generate name for enum constraint. |
||
312 | * |
||
313 | * @return string |
||
314 | */ |
||
315 | View Code Duplication | private function enumConstraint(): string |
|
323 | |||
324 | /** |
||
325 | * Normalize default value. |
||
326 | */ |
||
327 | private function normalizeDefault() |
||
342 | |||
343 | /** |
||
344 | * @param string $table Table name. |
||
345 | * @param array $schema |
||
346 | * @param Driver $driver Postgres columns are bit more complex. |
||
347 | * |
||
348 | * @return PostgresColumn |
||
349 | */ |
||
350 | public static function createInstance(string $table, array $schema, Driver $driver): self |
||
398 | |||
399 | /** |
||
400 | * Resolving enum constrain and converting it into proper enum values set. |
||
401 | * |
||
402 | * @param Driver $driver |
||
403 | * @param string|int $tableOID |
||
404 | * @param PostgresColumn $column |
||
405 | */ |
||
406 | private static function resolveConstrains(Driver $driver, $tableOID, PostgresColumn $column) |
||
434 | |||
435 | /** |
||
436 | * Resolve native ENUM type if presented. |
||
437 | * |
||
438 | * @param Driver $driver |
||
439 | * @param PostgresColumn $column |
||
440 | */ |
||
441 | private static function resolveEnum(Driver $driver, PostgresColumn $column) |
||
456 | |||
457 | /** |
||
458 | * {@inheritdoc} |
||
459 | */ |
||
460 | public function compare(ColumnInterface $initial): bool |
||
476 | } |
||
477 |
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.