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 |
||
12 | class ColumnSchema extends AbstractColumn |
||
13 | { |
||
14 | /** |
||
15 | * {@inheritdoc} |
||
16 | */ |
||
17 | protected $mapping = [ |
||
18 | //Primary sequences |
||
19 | 'primary' => [ |
||
20 | 'type' => 'integer', |
||
21 | 'primaryKey' => true, |
||
22 | 'nullable' => false, |
||
23 | ], |
||
24 | 'bigPrimary' => [ |
||
25 | 'type' => 'integer', |
||
26 | 'primaryKey' => true, |
||
27 | 'nullable' => false, |
||
28 | ], |
||
29 | |||
30 | //Enum type (mapped via method) |
||
31 | 'enum' => 'enum', |
||
32 | |||
33 | //Logical types |
||
34 | 'boolean' => 'boolean', |
||
35 | |||
36 | //Integer types (size can always be changed with size method), longInteger has method alias |
||
37 | //bigInteger |
||
38 | 'integer' => 'integer', |
||
39 | 'tinyInteger' => 'tinyint', |
||
40 | 'bigInteger' => 'bigint', |
||
41 | |||
42 | //String with specified length (mapped via method) |
||
43 | 'string' => 'text', |
||
44 | |||
45 | //Generic types |
||
46 | 'text' => 'text', |
||
47 | 'tinyText' => 'text', |
||
48 | 'longText' => 'text', |
||
49 | |||
50 | //Real types |
||
51 | 'double' => 'double', |
||
52 | 'float' => 'real', |
||
53 | |||
54 | //Decimal type (mapped via method) |
||
55 | 'decimal' => 'numeric', |
||
56 | |||
57 | //Date and Time types |
||
58 | 'datetime' => 'datetime', |
||
59 | 'date' => 'date', |
||
60 | 'time' => 'time', |
||
61 | 'timestamp' => 'timestamp', |
||
62 | |||
63 | //Binary types |
||
64 | 'binary' => 'blob', |
||
65 | 'tinyBinary' => 'blob', |
||
66 | 'longBinary' => 'blob', |
||
67 | |||
68 | //Additional types |
||
69 | 'json' => 'text', |
||
70 | ]; |
||
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | protected $reverseMapping = [ |
||
76 | 'primary' => [['type' => 'integer', 'primaryKey' => true]], |
||
77 | 'enum' => ['enum'], |
||
78 | 'boolean' => ['boolean'], |
||
79 | 'integer' => ['int', 'integer', 'smallint', 'mediumint'], |
||
80 | 'tinyInteger' => ['tinyint'], |
||
81 | 'bigInteger' => ['bigint'], |
||
82 | 'text' => ['text', 'string'], |
||
83 | 'double' => ['double'], |
||
84 | 'float' => ['real'], |
||
85 | 'decimal' => ['numeric'], |
||
86 | 'datetime' => ['datetime'], |
||
87 | 'date' => ['date'], |
||
88 | 'time' => ['time'], |
||
89 | 'timestamp' => ['timestamp'], |
||
90 | 'binary' => ['blob'], |
||
91 | ]; |
||
92 | |||
93 | /** |
||
94 | * Indication that column is primary key. |
||
95 | * |
||
96 | * @var bool |
||
97 | */ |
||
98 | private $primaryKey = false; |
||
99 | |||
100 | |||
101 | /** |
||
102 | * DBMS specific reverse mapping must map database specific type into limited set of abstract |
||
103 | * types. |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | public function abstractType(): string |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | public function sqlStatement(Driver $driver): string |
||
135 | |||
136 | /** |
||
137 | * Driver specific column initialization.s |
||
138 | * |
||
139 | * @param string $table Table name. |
||
140 | * @param array $schema |
||
141 | * |
||
142 | * @return self |
||
143 | */ |
||
144 | public static function createInstance(string $table, array $schema): self |
||
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | */ |
||
219 | protected function prepareEnum(Driver $driver): string |
||
223 | } |
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.