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 MysqlAdapter 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 MysqlAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | class MysqlAdapter extends PdoAdapter implements AdapterInterface |
||
43 | { |
||
44 | |||
45 | protected $signedColumnTypes = ['integer' => true, 'biginteger' => true, 'float' => true, 'decimal' => true, 'boolean' => true]; |
||
46 | |||
47 | const TEXT_TINY = 255; |
||
48 | const TEXT_SMALL = 255; /* deprecated, alias of TEXT_TINY */ |
||
49 | const TEXT_REGULAR = 65535; |
||
50 | const TEXT_MEDIUM = 16777215; |
||
51 | const TEXT_LONG = 4294967295; |
||
52 | |||
53 | // According to https://dev.mysql.com/doc/refman/5.0/en/blob.html BLOB sizes are the same as TEXT |
||
54 | const BLOB_TINY = 255; |
||
55 | const BLOB_SMALL = 255; /* deprecated, alias of BLOB_TINY */ |
||
56 | const BLOB_REGULAR = 65535; |
||
57 | const BLOB_MEDIUM = 16777215; |
||
58 | const BLOB_LONG = 4294967295; |
||
59 | |||
60 | const INT_TINY = 255; |
||
61 | const INT_SMALL = 65535; |
||
62 | const INT_MEDIUM = 16777215; |
||
63 | const INT_REGULAR = 4294967295; |
||
64 | const INT_BIG = 18446744073709551615; |
||
65 | |||
66 | const TYPE_YEAR = 'year'; |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | 80 | */ |
|
71 | public function connect() |
||
72 | 80 | { |
|
73 | 80 | if ($this->connection === null) { |
|
74 | if (!class_exists('PDO') || !in_array('mysql', \PDO::getAvailableDrivers(), true)) { |
||
75 | // @codeCoverageIgnoreStart |
||
76 | throw new \RuntimeException('You need to enable the PDO_Mysql extension for Phinx to run properly.'); |
||
77 | // @codeCoverageIgnoreEnd |
||
78 | } |
||
79 | 80 | ||
80 | 80 | $db = null; |
|
81 | $options = $this->getOptions(); |
||
82 | 80 | ||
83 | $dsn = 'mysql:'; |
||
84 | 80 | ||
85 | if (!empty($options['unix_socket'])) { |
||
86 | // use socket connection |
||
87 | $dsn .= 'unix_socket=' . $options['unix_socket']; |
||
88 | } else { |
||
89 | 80 | // use network connection |
|
90 | 80 | $dsn .= 'host=' . $options['host']; |
|
91 | 80 | if (!empty($options['port'])) { |
|
92 | 80 | $dsn .= ';port=' . $options['port']; |
|
93 | } |
||
94 | } |
||
95 | 80 | ||
96 | $dsn .= ';dbname=' . $options['name']; |
||
97 | |||
98 | 80 | // charset support |
|
99 | if (!empty($options['charset'])) { |
||
100 | $dsn .= ';charset=' . $options['charset']; |
||
101 | } |
||
102 | 80 | ||
103 | $driverOptions = [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]; |
||
104 | |||
105 | // support arbitrary \PDO::MYSQL_ATTR_* driver options and pass them to PDO |
||
106 | 80 | // http://php.net/manual/en/ref.pdo-mysql.php#pdo-mysql.constants |
|
107 | 80 | View Code Duplication | foreach ($options as $key => $option) { |
|
|||
108 | if (strpos($key, 'mysql_attr_') === 0) { |
||
109 | $driverOptions[constant('\PDO::' . strtoupper($key))] = $option; |
||
110 | 80 | } |
|
111 | } |
||
112 | |||
113 | 80 | try { |
|
114 | 80 | $db = new \PDO($dsn, $options['user'], $options['pass'], $driverOptions); |
|
115 | 1 | } catch (\PDOException $exception) { |
|
116 | 1 | throw new \InvalidArgumentException(sprintf( |
|
117 | 1 | 'There was a problem connecting to the database: %s', |
|
118 | 1 | $exception->getMessage() |
|
119 | )); |
||
120 | } |
||
121 | 80 | ||
122 | 80 | $this->setConnection($db); |
|
123 | 80 | } |
|
124 | } |
||
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | 81 | */ |
|
129 | public function disconnect() |
||
130 | 81 | { |
|
131 | 81 | $this->connection = null; |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * {@inheritdoc} |
||
136 | 6 | */ |
|
137 | public function hasTransactions() |
||
138 | 6 | { |
|
139 | return true; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | 6 | */ |
|
145 | public function beginTransaction() |
||
146 | 6 | { |
|
147 | 6 | $this->execute('START TRANSACTION'); |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | 6 | */ |
|
153 | public function commitTransaction() |
||
154 | 6 | { |
|
155 | 6 | $this->execute('COMMIT'); |
|
156 | } |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | 1 | */ |
|
161 | public function rollbackTransaction() |
||
162 | 1 | { |
|
163 | 1 | $this->execute('ROLLBACK'); |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * {@inheritdoc} |
||
168 | 112 | */ |
|
169 | public function quoteTableName($tableName) |
||
170 | 112 | { |
|
171 | return str_replace('.', '`.`', $this->quoteColumnName($tableName)); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | 112 | */ |
|
177 | public function quoteColumnName($columnName) |
||
178 | 112 | { |
|
179 | return '`' . str_replace('`', '``', $columnName) . '`'; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | 82 | */ |
|
185 | View Code Duplication | public function hasTable($tableName) |
|
186 | 82 | { |
|
187 | $options = $this->getOptions(); |
||
188 | 82 | ||
189 | $exists = $this->fetchRow(sprintf( |
||
190 | "SELECT TABLE_NAME |
||
191 | 82 | FROM INFORMATION_SCHEMA.TABLES |
|
192 | 82 | WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s'", |
|
193 | $options['name'], |
||
194 | 82 | $tableName |
|
195 | )); |
||
196 | 82 | ||
197 | return !empty($exists); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * {@inheritdoc} |
||
202 | 82 | */ |
|
203 | public function createTable(Table $table) |
||
204 | { |
||
205 | // This method is based on the MySQL docs here: http://dev.mysql.com/doc/refman/5.1/en/create-index.html |
||
206 | 82 | $defaultOptions = [ |
|
207 | 'engine' => 'InnoDB', |
||
208 | 82 | 'collation' => 'utf8_general_ci' |
|
209 | 82 | ]; |
|
210 | |||
211 | $options = array_merge( |
||
212 | 82 | $defaultOptions, |
|
213 | 82 | array_intersect_key($this->getOptions(), $defaultOptions), |
|
214 | 68 | $table->getOptions() |
|
215 | 68 | ); |
|
216 | 68 | ||
217 | 68 | // Add the default primary key |
|
218 | 68 | $columns = $table->getPendingColumns(); |
|
219 | if (!isset($options['id']) || (isset($options['id']) && $options['id'] === true)) { |
||
220 | 68 | $column = new Column(); |
|
221 | 68 | $column->setName('id') |
|
222 | 82 | ->setType('integer') |
|
223 | ->setSigned(isset($options['signed']) ? $options['signed'] : true) |
||
224 | 2 | ->setIdentity(true); |
|
225 | 2 | ||
226 | 2 | array_unshift($columns, $column); |
|
227 | 2 | $options['primary_key'] = 'id'; |
|
228 | } elseif (isset($options['id']) && is_string($options['id'])) { |
||
229 | 2 | // Handle id => "field_name" to support AUTO_INCREMENT |
|
230 | 2 | $column = new Column(); |
|
231 | 2 | $column->setName($options['id']) |
|
232 | ->setType('integer') |
||
233 | ->setIdentity(true); |
||
234 | |||
235 | array_unshift($columns, $column); |
||
236 | 82 | $options['primary_key'] = $options['id']; |
|
237 | 82 | } |
|
238 | 82 | ||
239 | 82 | // TODO - process table options like collation etc |
|
240 | |||
241 | // process table engine (default to InnoDB) |
||
242 | 82 | $optionsStr = 'ENGINE = InnoDB'; |
|
243 | 82 | if (isset($options['engine'])) { |
|
244 | 82 | $optionsStr = sprintf('ENGINE = %s', $options['engine']); |
|
245 | 82 | } |
|
246 | 82 | ||
247 | // process table collation |
||
248 | if (isset($options['collation'])) { |
||
249 | 82 | $charset = explode('_', $options['collation']); |
|
250 | 2 | $optionsStr .= sprintf(' CHARACTER SET %s', $charset[0]); |
|
251 | 2 | $optionsStr .= sprintf(' COLLATE %s', $options['collation']); |
|
252 | } |
||
253 | 82 | ||
254 | 82 | // set the table comment |
|
255 | 82 | if (isset($options['comment'])) { |
|
256 | 82 | $optionsStr .= sprintf(" COMMENT=%s ", $this->getConnection()->quote($options['comment'])); |
|
257 | 82 | } |
|
258 | |||
259 | $sql = 'CREATE TABLE '; |
||
260 | 82 | $sql .= $this->quoteTableName($table->getName()) . ' ('; |
|
261 | 82 | View Code Duplication | foreach ($columns as $column) { |
262 | 82 | $sql .= $this->quoteColumnName($column->getName()) . ' ' . $this->getColumnSqlDefinition($column) . ', '; |
|
263 | 82 | } |
|
264 | 81 | ||
265 | 82 | // set the primary key(s) |
|
266 | View Code Duplication | if (isset($options['primary_key'])) { |
|
267 | $sql = rtrim($sql); |
||
268 | 2 | $sql .= ' PRIMARY KEY ('; |
|
269 | 2 | if (is_string($options['primary_key'])) { // handle primary_key => 'id' |
|
270 | 2 | $sql .= $this->quoteColumnName($options['primary_key']); |
|
271 | 2 | } elseif (is_array($options['primary_key'])) { // handle primary_key => array('tag_id', 'resource_id') |
|
272 | 2 | $sql .= implode(',', array_map([$this, 'quoteColumnName'], $options['primary_key'])); |
|
273 | 2 | } |
|
274 | 2 | $sql .= ')'; |
|
275 | 2 | } else { |
|
276 | 2 | $sql = substr(rtrim($sql), 0, -1); // no primary keys |
|
277 | 2 | } |
|
278 | 82 | ||
279 | 82 | // set the indexes |
|
280 | 1 | $indexes = $table->getIndexes(); |
|
281 | foreach ($indexes as $index) { |
||
282 | $sql .= ', ' . $this->getIndexSqlDefinition($index); |
||
283 | } |
||
284 | 82 | ||
285 | 82 | // set the foreign keys |
|
286 | 10 | $foreignKeys = $table->getForeignKeys(); |
|
287 | 82 | foreach ($foreignKeys as $foreignKey) { |
|
288 | $sql .= ', ' . $this->getForeignKeySqlDefinition($foreignKey); |
||
289 | } |
||
290 | 82 | ||
291 | 82 | $sql .= ') ' . $optionsStr; |
|
292 | 2 | $sql = rtrim($sql) . ';'; |
|
293 | 82 | ||
294 | // execute the sql |
||
295 | 82 | $this->execute($sql); |
|
296 | 82 | } |
|
297 | |||
298 | /** |
||
299 | 82 | * {@inheritdoc} |
|
300 | 82 | */ |
|
301 | public function renameTable($tableName, $newTableName) |
||
305 | 5 | ||
306 | /** |
||
307 | 5 | * {@inheritdoc} |
|
308 | 5 | */ |
|
309 | public function dropTable($tableName) |
||
313 | 5 | ||
314 | /** |
||
315 | 5 | * {@inheritdoc} |
|
316 | 5 | */ |
|
317 | public function truncateTable($tableName) |
||
318 | { |
||
319 | $sql = sprintf( |
||
320 | 'TRUNCATE TABLE %s', |
||
321 | 1 | $this->quoteTableName($tableName) |
|
322 | ); |
||
323 | 1 | ||
324 | 1 | $this->execute($sql); |
|
325 | 1 | } |
|
326 | 1 | ||
327 | /** |
||
328 | 1 | * {@inheritdoc} |
|
329 | 1 | */ |
|
330 | public function getColumns($tableName) |
||
357 | 12 | ||
358 | /** |
||
359 | 12 | * {@inheritdoc} |
|
360 | */ |
||
361 | View Code Duplication | public function hasColumn($tableName, $columnName) |
|
372 | 77 | ||
373 | /** |
||
374 | 21 | * {@inheritdoc} |
|
375 | */ |
||
376 | View Code Duplication | public function addColumn(Table $table, Column $column) |
|
377 | { |
||
378 | $sql = sprintf( |
||
379 | 'ALTER TABLE %s ADD %s %s', |
||
380 | $this->quoteTableName($table->getName()), |
||
381 | $this->quoteColumnName($column->getName()), |
||
382 | $this->getColumnSqlDefinition($column) |
||
391 | |||
392 | /** |
||
393 | * {@inheritdoc} |
||
394 | */ |
||
395 | public function renameColumn($tableName, $columnName, $newColumnName) |
||
426 | |||
427 | 5 | /** |
|
428 | 5 | * {@inheritdoc} |
|
429 | 5 | */ |
|
430 | 5 | View Code Duplication | public function changeColumn($tableName, $columnName, Column $newColumn) |
444 | |||
445 | /** |
||
446 | * {@inheritdoc} |
||
447 | */ |
||
448 | public function dropColumn($tableName, $columnName) |
||
458 | 5 | ||
459 | /** |
||
460 | 5 | * Get an array of indexes from a particular table. |
|
461 | 5 | * |
|
462 | 5 | * @param string $tableName Table Name |
|
463 | * @return array |
||
464 | */ |
||
465 | View Code Duplication | protected function getIndexes($tableName) |
|
478 | |||
479 | /** |
||
480 | * {@inheritdoc} |
||
481 | */ |
||
482 | public function hasIndex($tableName, $columns) |
||
499 | |||
500 | 14 | /** |
|
501 | * {@inheritdoc} |
||
502 | 14 | */ |
|
503 | 6 | View Code Duplication | public function hasIndexByName($tableName, $indexName) |
515 | 11 | ||
516 | /** |
||
517 | * {@inheritdoc} |
||
518 | */ |
||
519 | public function addIndex(Table $table, Index $index) |
||
529 | 1 | ||
530 | /** |
||
531 | * {@inheritdoc} |
||
532 | */ |
||
533 | public function dropIndex($tableName, $columns) |
||
556 | |||
557 | 3 | /** |
|
558 | 3 | * {@inheritdoc} |
|
559 | */ |
||
560 | 3 | View Code Duplication | public function dropIndexByName($tableName, $indexName) |
579 | 2 | ||
580 | /** |
||
581 | 2 | * {@inheritdoc} |
|
582 | */ |
||
583 | 2 | View Code Duplication | public function hasForeignKey($tableName, $columns, $constraint = null) |
605 | 21 | ||
606 | 6 | /** |
|
607 | 4 | * Get an array of foreign keys from a particular table. |
|
608 | * |
||
609 | 4 | * @param string $tableName Table Name |
|
610 | * @return array |
||
611 | 15 | */ |
|
612 | 12 | View Code Duplication | protected function getForeignKeys($tableName) |
638 | |||
639 | /** |
||
640 | 22 | * {@inheritdoc} |
|
641 | */ |
||
642 | 22 | public function addForeignKey(Table $table, ForeignKey $foreignKey) |
|
652 | |||
653 | /** |
||
654 | * {@inheritdoc} |
||
655 | 15 | */ |
|
656 | View Code Duplication | public function dropForeignKey($tableName, $columns, $constraint = null) |
|
692 | |||
693 | /** |
||
694 | * {@inheritdoc} |
||
695 | 7 | */ |
|
696 | 7 | public function getSqlType($type, $limit = null) |
|
810 | 10 | ||
811 | 2 | /** |
|
812 | * Returns Phinx type by SQL type |
||
813 | 10 | * |
|
814 | 10 | * @param string $sqlTypeDef |
|
815 | 10 | * @throws \RuntimeException |
|
816 | 10 | * @internal param string $sqlType SQL type |
|
817 | 5 | * @returns string Phinx type |
|
818 | 8 | */ |
|
819 | 5 | public function getPhinxType($sqlTypeDef) |
|
923 | 2 | ||
924 | 9 | /** |
|
925 | 2 | * {@inheritdoc} |
|
926 | 2 | */ |
|
927 | 2 | public function createDatabase($name, $options = []) |
|
937 | |||
938 | 15 | /** |
|
939 | 15 | * {@inheritdoc} |
|
940 | */ |
||
941 | 15 | public function hasDatabase($name) |
|
958 | 83 | ||
959 | 1 | /** |
|
960 | 1 | * {@inheritdoc} |
|
961 | 82 | */ |
|
962 | public function dropDatabase($name) |
||
966 | |||
967 | /** |
||
968 | 4 | * Gets the MySQL Column Definition for a Column object. |
|
969 | * |
||
970 | 4 | * @param \Phinx\Db\Table\Column $column Column |
|
971 | 4 | * @return string |
|
972 | 4 | */ |
|
973 | protected function getColumnSqlDefinition(Column $column) |
||
1006 | 89 | ||
1007 | 2 | /** |
|
1008 | 89 | * Gets the MySQL Index Definition for an Index object. |
|
1009 | 86 | * |
|
1010 | 86 | * @param \Phinx\Db\Table\Index $index Index |
|
1011 | 89 | * @return string |
|
1012 | 5 | */ |
|
1013 | 5 | protected function getIndexSqlDefinition(Index $index) |
|
1039 | |||
1040 | 16 | /** |
|
1041 | 16 | * Gets the MySQL Foreign Key Definition for an ForeignKey object. |
|
1042 | 16 | * |
|
1043 | 2 | * @param \Phinx\Db\Table\ForeignKey $foreignKey |
|
1044 | 2 | * @return string |
|
1045 | */ |
||
1046 | 16 | View Code Duplication | protected function getForeignKeySqlDefinition(ForeignKey $foreignKey) |
1071 | 17 | ||
1072 | /** |
||
1073 | 17 | * Describes a database table. This is a MySQL adapter specific method. |
|
1074 | 17 | * |
|
1075 | 5 | * @param string $tableName Table name |
|
1076 | 5 | * @return array |
|
1077 | 17 | */ |
|
1078 | 17 | View Code Duplication | public function describeTable($tableName) |
1094 | |||
1095 | /** |
||
1096 | * Returns MySQL column types (inherited and MySQL specified). |
||
1097 | * @return array |
||
1098 | */ |
||
1099 | public function getColumnTypes() |
||
1103 | } |
||
1104 |
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.