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 SQLServerColumn 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 SQLServerColumn, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class SQLServerColumn extends AbstractColumn |
||
17 | { |
||
18 | /** |
||
19 | * {@inheritdoc} |
||
20 | */ |
||
21 | protected $mapping = [ |
||
22 | //Primary sequences |
||
23 | 'primary' => ['type' => 'int', 'identity' => true, 'nullable' => false], |
||
24 | 'bigPrimary' => ['type' => 'bigint', 'identity' => true, 'nullable' => false], |
||
25 | |||
26 | //Enum type (mapped via method) |
||
27 | 'enum' => 'enum', |
||
28 | |||
29 | //Logical types |
||
30 | 'boolean' => 'bit', |
||
31 | |||
32 | //Integer types (size can always be changed with size method), longInteger has method alias |
||
33 | //bigInteger |
||
34 | 'integer' => 'int', |
||
35 | 'tinyInteger' => 'tinyint', |
||
36 | 'bigInteger' => 'bigint', |
||
37 | |||
38 | //String with specified length (mapped via method) |
||
39 | 'string' => 'varchar', |
||
40 | |||
41 | //Generic types |
||
42 | 'text' => ['type' => 'varchar', 'size' => 0], |
||
43 | 'tinyText' => ['type' => 'varchar', 'size' => 0], |
||
44 | 'longText' => ['type' => 'varchar', 'size' => 0], |
||
45 | |||
46 | //Real types |
||
47 | 'double' => 'float', |
||
48 | 'float' => 'real', |
||
49 | |||
50 | //Decimal type (mapped via method) |
||
51 | 'decimal' => 'decimal', |
||
52 | |||
53 | //Date and Time types |
||
54 | 'datetime' => 'datetime', |
||
55 | 'date' => 'date', |
||
56 | 'time' => 'time', |
||
57 | 'timestamp' => 'datetime', |
||
58 | |||
59 | //Binary types |
||
60 | 'binary' => ['type' => 'varbinary', 'size' => 0], |
||
61 | 'tinyBinary' => ['type' => 'varbinary', 'size' => 0], |
||
62 | 'longBinary' => ['type' => 'varbinary', 'size' => 0], |
||
63 | |||
64 | //Additional types |
||
65 | 'json' => ['type' => 'varchar', 'size' => 0], |
||
66 | ]; |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | protected $reverseMapping = [ |
||
72 | 'primary' => [['type' => 'int', 'identity' => true]], |
||
73 | 'bigPrimary' => [['type' => 'bigint', 'identity' => true]], |
||
74 | 'enum' => ['enum'], |
||
75 | 'boolean' => ['bit'], |
||
76 | 'integer' => ['int'], |
||
77 | 'tinyInteger' => ['tinyint', 'smallint'], |
||
78 | 'bigInteger' => ['bigint'], |
||
79 | 'text' => [['type' => 'varchar', 'size' => 0]], |
||
80 | 'string' => ['varchar', 'char'], |
||
81 | 'double' => ['float'], |
||
82 | 'float' => ['real'], |
||
83 | 'decimal' => ['decimal'], |
||
84 | 'timestamp' => ['datetime'], |
||
85 | 'date' => ['date'], |
||
86 | 'time' => ['time'], |
||
87 | 'binary' => ['varbinary'], |
||
88 | ]; |
||
89 | |||
90 | /** |
||
91 | * Field is table identity. |
||
92 | * |
||
93 | * @var bool |
||
94 | */ |
||
95 | protected $identity = false; |
||
96 | |||
97 | /** |
||
98 | * @var bool |
||
99 | */ |
||
100 | protected $constrainedDefault = false; |
||
101 | |||
102 | /** |
||
103 | * Name of default constraint. |
||
104 | * |
||
105 | * @var string |
||
106 | */ |
||
107 | protected $defaultConstraint = ''; |
||
108 | |||
109 | /** |
||
110 | * @var bool |
||
111 | */ |
||
112 | protected $constrainedEnum = false; |
||
113 | |||
114 | /** |
||
115 | * Name of enum constraint. |
||
116 | * |
||
117 | * @var string |
||
118 | */ |
||
119 | protected $enumConstraint = ''; |
||
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | public function getConstraints(): array |
||
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | public function abstractType(): string |
||
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | View Code Duplication | public function enum($values): AbstractColumn |
|
166 | |||
167 | /** |
||
168 | * {@inheritdoc} |
||
169 | * |
||
170 | * @param bool $withEnum When true enum constrain will be included into definition. Set to false |
||
171 | * if you want to create constrain separately. |
||
172 | */ |
||
173 | public function sqlStatement(Driver $driver, bool $withEnum = true): string |
||
201 | |||
202 | /** |
||
203 | * {@inheritdoc} |
||
204 | */ |
||
205 | protected function prepareDefault(Driver $driver): string |
||
214 | |||
215 | /** |
||
216 | * Generate set of operations need to change column. We are expecting that column constrains |
||
217 | * will be dropped separately. |
||
218 | * |
||
219 | * @param Driver $driver |
||
220 | * @param AbstractColumn $initial |
||
221 | * |
||
222 | * @return array |
||
223 | */ |
||
224 | public function alterOperations(Driver $driver, AbstractColumn $initial): array |
||
283 | |||
284 | /** |
||
285 | * Get/generate name of enum constraint. |
||
286 | * |
||
287 | * @return string |
||
288 | */ |
||
289 | View Code Duplication | protected function enumConstraint() |
|
297 | |||
298 | /** |
||
299 | * Get/generate name of default constrain. |
||
300 | * |
||
301 | * @return string |
||
302 | */ |
||
303 | View Code Duplication | protected function defaultConstrain(): string |
|
311 | |||
312 | /** |
||
313 | * In SQLServer we can emulate enums similar way as in Postgres via column constrain. |
||
314 | * |
||
315 | * @param Driver $driver |
||
316 | * |
||
317 | * @return string |
||
318 | */ |
||
319 | private function enumStatement(Driver $driver): string |
||
332 | |||
333 | /** |
||
334 | * Normalize default value. |
||
335 | */ |
||
336 | private function normalizeDefault() |
||
359 | |||
360 | /** |
||
361 | * @param string $table Table name. |
||
362 | * @param array $schema |
||
363 | * @param Driver $driver SQLServer columns are bit more complex. |
||
364 | * |
||
365 | * @return SQLServerColumn |
||
366 | */ |
||
367 | public static function createInstance(string $table, array $schema, Driver $driver): self |
||
414 | |||
415 | /** |
||
416 | * Resolve enum values if any. |
||
417 | * |
||
418 | * @param Driver $driver |
||
419 | * @param array $schema |
||
420 | * @param SQLServerColumn $column |
||
421 | */ |
||
422 | private static function resolveEnum(Driver $driver, array $schema, SQLServerColumn $column) |
||
449 | } |
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.