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:
1 | <?php |
||
13 | class MySQLColumn extends AbstractColumn |
||
14 | { |
||
15 | /** |
||
16 | * {@inheritdoc} |
||
17 | */ |
||
18 | protected $mapping = [ |
||
19 | //Primary sequences |
||
20 | 'primary' => [ |
||
21 | 'type' => 'int', |
||
22 | 'size' => 11, |
||
23 | 'autoIncrement' => true, |
||
24 | 'nullable' => false, |
||
25 | ], |
||
26 | 'bigPrimary' => [ |
||
27 | 'type' => 'bigint', |
||
28 | 'size' => 20, |
||
29 | 'autoIncrement' => true, |
||
30 | 'nullable' => false, |
||
31 | ], |
||
32 | |||
33 | //Enum type (mapped via method) |
||
34 | 'enum' => 'enum', |
||
35 | |||
36 | //Logical types |
||
37 | 'boolean' => ['type' => 'tinyint', 'size' => 1], |
||
38 | |||
39 | //Integer types (size can always be changed with size method), longInteger has method alias |
||
40 | //bigInteger |
||
41 | 'integer' => ['type' => 'int', 'size' => 11], |
||
42 | 'tinyInteger' => ['type' => 'tinyint', 'size' => 4], |
||
43 | 'bigInteger' => ['type' => 'bigint', 'size' => 20], |
||
44 | |||
45 | //String with specified length (mapped via method) |
||
46 | 'string' => 'varchar', |
||
47 | |||
48 | //Generic types |
||
49 | 'text' => 'text', |
||
50 | 'tinyText' => 'tinytext', |
||
51 | 'longText' => 'longtext', |
||
52 | |||
53 | //Real types |
||
54 | 'double' => 'double', |
||
55 | 'float' => 'float', |
||
56 | |||
57 | //Decimal type (mapped via method) |
||
58 | 'decimal' => 'decimal', |
||
59 | |||
60 | //Date and Time types |
||
61 | 'datetime' => 'datetime', |
||
62 | 'date' => 'date', |
||
63 | 'time' => 'time', |
||
64 | 'timestamp' => [ |
||
65 | 'type' => 'timestamp', |
||
66 | 'defaultValue' => self::DATETIME_DEFAULT, |
||
67 | ], |
||
68 | |||
69 | //Binary types |
||
70 | 'binary' => 'blob', |
||
71 | 'tinyBinary' => 'tinyblob', |
||
72 | 'longBinary' => 'longblob', |
||
73 | |||
74 | //Additional types |
||
75 | 'json' => 'text', |
||
76 | ]; |
||
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | protected $reverseMapping = [ |
||
82 | 'primary' => [['type' => 'int', 'autoIncrement' => true]], |
||
83 | 'bigPrimary' => ['serial', ['type' => 'bigint', 'autoIncrement' => true]], |
||
84 | 'enum' => ['enum'], |
||
85 | 'boolean' => ['bool', 'boolean', ['type' => 'tinyint', 'size' => 1]], |
||
86 | 'integer' => ['int', 'integer', 'smallint', 'mediumint'], |
||
87 | 'tinyInteger' => ['tinyint'], |
||
88 | 'bigInteger' => ['bigint'], |
||
89 | 'string' => ['varchar', 'char'], |
||
90 | 'text' => ['text', 'mediumtext'], |
||
91 | 'tinyText' => ['tinytext'], |
||
92 | 'longText' => ['longtext'], |
||
93 | 'double' => ['double'], |
||
94 | 'float' => ['float', 'real'], |
||
95 | 'decimal' => ['decimal'], |
||
96 | 'datetime' => ['datetime'], |
||
97 | 'date' => ['date'], |
||
98 | 'time' => ['time'], |
||
99 | 'timestamp' => ['timestamp'], |
||
100 | 'binary' => ['blob', 'binary', 'varbinary'], |
||
101 | 'tinyBinary' => ['tinyblob'], |
||
102 | 'longBinary' => ['longblob'], |
||
103 | ]; |
||
104 | |||
105 | /** |
||
106 | * List of types forbids default value set. |
||
107 | * |
||
108 | * @var array |
||
109 | */ |
||
110 | protected $forbiddenDefaults = [ |
||
111 | 'text', |
||
112 | 'mediumtext', |
||
113 | 'tinytext', |
||
114 | 'longtext', |
||
115 | 'blog', |
||
116 | 'tinyblob', |
||
117 | 'longblob', |
||
118 | ]; |
||
119 | |||
120 | /** |
||
121 | * Column is auto incremental. |
||
122 | * |
||
123 | * @var bool |
||
124 | */ |
||
125 | protected $autoIncrement = false; |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | public function getDefaultValue() |
||
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | public function sqlStatement(Driver $driver): string |
||
160 | |||
161 | /** |
||
162 | * @param string $table Table name. |
||
163 | * @param array $schema |
||
164 | * |
||
165 | * @return MySQLColumn |
||
166 | */ |
||
167 | public static function createInstance(string $table, array $schema): self |
||
224 | |||
225 | /** |
||
226 | * {@inheritdoc} |
||
227 | */ |
||
228 | protected function prepareDefault(Driver $driver): string |
||
244 | } |
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.