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 |
||
27 | class MysqlAdapter extends PdoAdapter |
||
28 | { |
||
29 | protected $signedColumnTypes = [ |
||
30 | 'integer' => true, |
||
31 | 'smallinteger' => true, |
||
32 | 'biginteger' => true, |
||
33 | 'float' => true, |
||
34 | 'decimal' => true, |
||
35 | 'double' => true, |
||
36 | 'boolean' => true, |
||
37 | ]; |
||
38 | |||
39 | const TEXT_TINY = 255; |
||
40 | const TEXT_SMALL = 255; /* deprecated, alias of TEXT_TINY */ |
||
41 | const TEXT_REGULAR = 65535; |
||
42 | const TEXT_MEDIUM = 16777215; |
||
43 | const TEXT_LONG = 4294967295; |
||
44 | |||
45 | // According to https://dev.mysql.com/doc/refman/5.0/en/blob.html BLOB sizes are the same as TEXT |
||
46 | const BLOB_TINY = 255; |
||
47 | const BLOB_SMALL = 255; /* deprecated, alias of BLOB_TINY */ |
||
48 | const BLOB_REGULAR = 65535; |
||
49 | const BLOB_MEDIUM = 16777215; |
||
50 | const BLOB_LONG = 4294967295; |
||
51 | |||
52 | const INT_TINY = 255; |
||
53 | const INT_SMALL = 65535; |
||
54 | const INT_MEDIUM = 16777215; |
||
55 | const INT_REGULAR = 4294967295; |
||
56 | const INT_BIG = 18446744073709551615; |
||
57 | |||
58 | const BIT = 64; |
||
59 | |||
60 | const TYPE_YEAR = 'year'; |
||
61 | |||
62 | /** |
||
63 | * {@inheritDoc} |
||
64 | * |
||
65 | * @throws \RuntimeException |
||
66 | * @throws \InvalidArgumentException |
||
67 | * |
||
68 | * @return void |
||
69 | */ |
||
70 | 80 | public function connect() |
|
71 | { |
||
72 | 80 | if ($this->connection === null) { |
|
73 | 80 | if (!class_exists('PDO') || !in_array('mysql', PDO::getAvailableDrivers(), true)) { |
|
74 | // @codeCoverageIgnoreStart |
||
75 | throw new RuntimeException('You need to enable the PDO_Mysql extension for Phinx to run properly.'); |
||
76 | // @codeCoverageIgnoreEnd |
||
77 | } |
||
78 | |||
79 | 80 | $options = $this->getOptions(); |
|
80 | 80 | ||
81 | $dsn = 'mysql:'; |
||
82 | 80 | ||
83 | if (!empty($options['unix_socket'])) { |
||
84 | 80 | // use socket connection |
|
85 | $dsn .= 'unix_socket=' . $options['unix_socket']; |
||
86 | } else { |
||
87 | // use network connection |
||
88 | $dsn .= 'host=' . $options['host']; |
||
89 | 80 | if (!empty($options['port'])) { |
|
90 | 80 | $dsn .= ';port=' . $options['port']; |
|
91 | 80 | } |
|
92 | 80 | } |
|
93 | |||
94 | $dsn .= ';dbname=' . $options['name']; |
||
95 | 80 | ||
96 | // charset support |
||
97 | if (!empty($options['charset'])) { |
||
98 | 80 | $dsn .= ';charset=' . $options['charset']; |
|
99 | } |
||
100 | |||
101 | $driverOptions = []; |
||
102 | 80 | ||
103 | // use custom data fetch mode |
||
104 | View Code Duplication | if (!empty($options['fetch_mode'])) { |
|
|
|||
105 | $driverOptions[PDO::ATTR_DEFAULT_FETCH_MODE] = constant('\PDO::FETCH_' . strtoupper($options['fetch_mode'])); |
||
106 | 80 | } |
|
107 | 80 | ||
108 | // support arbitrary \PDO::MYSQL_ATTR_* driver options and pass them to PDO |
||
109 | // http://php.net/manual/en/ref.pdo-mysql.php#pdo-mysql.constants |
||
110 | 80 | View Code Duplication | foreach ($options as $key => $option) { |
111 | if (strpos($key, 'mysql_attr_') === 0) { |
||
112 | $driverOptions[constant('\PDO::' . strtoupper($key))] = $option; |
||
113 | 80 | } |
|
114 | 80 | } |
|
115 | 1 | ||
116 | 1 | $db = $this->createPdoConnection($dsn, $options['user'], $options['pass'], $driverOptions); |
|
117 | 1 | ||
118 | 1 | $this->setConnection($db); |
|
119 | } |
||
120 | } |
||
121 | 80 | ||
122 | 80 | /** |
|
123 | 80 | * {@inheritDoc} |
|
124 | * |
||
125 | * @return void |
||
126 | */ |
||
127 | public function disconnect() |
||
128 | 81 | { |
|
129 | $this->connection = null; |
||
130 | 81 | } |
|
131 | 81 | ||
132 | /** |
||
133 | * @inheritDoc |
||
134 | */ |
||
135 | public function hasTransactions() |
||
136 | 6 | { |
|
137 | return true; |
||
138 | 6 | } |
|
139 | |||
140 | /** |
||
141 | * {@inheritDoc} |
||
142 | * |
||
143 | * @return void |
||
144 | 6 | */ |
|
145 | public function beginTransaction() |
||
146 | 6 | { |
|
147 | 6 | $this->execute('START TRANSACTION'); |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * {@inheritDoc} |
||
152 | 6 | * |
|
153 | * @return void |
||
154 | 6 | */ |
|
155 | 6 | public function commitTransaction() |
|
156 | { |
||
157 | $this->execute('COMMIT'); |
||
158 | } |
||
159 | |||
160 | 1 | /** |
|
161 | * {@inheritDoc} |
||
162 | 1 | * |
|
163 | 1 | * @return void |
|
164 | */ |
||
165 | public function rollbackTransaction() |
||
166 | { |
||
167 | $this->execute('ROLLBACK'); |
||
168 | 112 | } |
|
169 | |||
170 | 112 | /** |
|
171 | * @inheritDoc |
||
172 | */ |
||
173 | public function quoteTableName($tableName) |
||
174 | { |
||
175 | return str_replace('.', '`.`', $this->quoteColumnName($tableName)); |
||
176 | 112 | } |
|
177 | |||
178 | 112 | /** |
|
179 | * @inheritDoc |
||
180 | */ |
||
181 | public function quoteColumnName($columnName) |
||
182 | { |
||
183 | return '`' . str_replace('`', '``', $columnName) . '`'; |
||
184 | 82 | } |
|
185 | |||
186 | 82 | /** |
|
187 | * @inheritDoc |
||
188 | 82 | */ |
|
189 | public function hasTable($tableName) |
||
190 | { |
||
191 | 82 | if ($this->hasCreatedTable($tableName)) { |
|
192 | 82 | return true; |
|
193 | } |
||
194 | 82 | ||
195 | if (strpos($tableName, '.') !== false) { |
||
196 | 82 | list($schema, $table) = explode('.', $tableName); |
|
197 | $exists = $this->hasTableWithSchema($schema, $table); |
||
198 | // Only break here on success, because it is possible for table names to contain a dot. |
||
199 | if ($exists) { |
||
200 | return true; |
||
201 | } |
||
202 | 82 | } |
|
203 | |||
204 | $options = $this->getOptions(); |
||
205 | |||
206 | 82 | return $this->hasTableWithSchema($options['name'], $tableName); |
|
207 | } |
||
208 | 82 | ||
209 | 82 | /** |
|
210 | * @param string $schema The table schema |
||
211 | * @param string $tableName The table name |
||
212 | 82 | * |
|
213 | 82 | * @return bool |
|
214 | 68 | */ |
|
215 | 68 | private function hasTableWithSchema($schema, $tableName) |
|
216 | 68 | { |
|
217 | 68 | $result = $this->fetchRow(sprintf( |
|
218 | 68 | "SELECT TABLE_NAME |
|
219 | FROM INFORMATION_SCHEMA.TABLES |
||
220 | 68 | WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s'", |
|
221 | 68 | $schema, |
|
222 | 82 | $tableName |
|
223 | )); |
||
224 | 2 | ||
225 | 2 | return !empty($result); |
|
226 | 2 | } |
|
227 | 2 | /** |
|
228 | * {@inheritDoc} |
||
229 | 2 | * |
|
230 | 2 | * @return void |
|
231 | 2 | */ |
|
232 | public function createTable(Table $table, array $columns = [], array $indexes = []) |
||
233 | { |
||
234 | // This method is based on the MySQL docs here: http://dev.mysql.com/doc/refman/5.1/en/create-index.html |
||
235 | $defaultOptions = [ |
||
236 | 82 | 'engine' => 'InnoDB', |
|
237 | 82 | 'collation' => 'utf8_general_ci', |
|
238 | 82 | ]; |
|
239 | 82 | ||
240 | $options = array_merge( |
||
241 | $defaultOptions, |
||
242 | 82 | array_intersect_key($this->getOptions(), $defaultOptions), |
|
243 | 82 | $table->getOptions() |
|
244 | 82 | ); |
|
245 | 82 | ||
246 | 82 | // Add the default primary key |
|
247 | View Code Duplication | if (!isset($options['id']) || (isset($options['id']) && $options['id'] === true)) { |
|
248 | $options['id'] = 'id'; |
||
249 | 82 | } |
|
250 | 2 | ||
251 | 2 | if (isset($options['id']) && is_string($options['id'])) { |
|
252 | // Handle id => "field_name" to support AUTO_INCREMENT |
||
253 | 82 | $column = new Column(); |
|
254 | 82 | $column->setName($options['id']) |
|
255 | 82 | ->setType('integer') |
|
256 | 82 | ->setSigned(isset($options['signed']) ? $options['signed'] : true) |
|
257 | 82 | ->setIdentity(true); |
|
258 | |||
259 | array_unshift($columns, $column); |
||
260 | 82 | if (isset($options['primary_key']) && (array)$options['id'] !== (array)$options['primary_key']) { |
|
261 | 82 | throw new InvalidArgumentException('You cannot enable an auto incrementing ID field and a primary key'); |
|
262 | 82 | } |
|
263 | 82 | $options['primary_key'] = $options['id']; |
|
264 | 81 | } |
|
265 | 82 | ||
266 | // TODO - process table options like collation etc |
||
267 | |||
268 | 2 | // process table engine (default to InnoDB) |
|
269 | 2 | $optionsStr = 'ENGINE = InnoDB'; |
|
270 | 2 | if (isset($options['engine'])) { |
|
271 | 2 | $optionsStr = sprintf('ENGINE = %s', $options['engine']); |
|
272 | 2 | } |
|
273 | 2 | ||
274 | 2 | // process table collation |
|
275 | 2 | if (isset($options['collation'])) { |
|
276 | 2 | $charset = explode('_', $options['collation']); |
|
277 | 2 | $optionsStr .= sprintf(' CHARACTER SET %s', $charset[0]); |
|
278 | 82 | $optionsStr .= sprintf(' COLLATE %s', $options['collation']); |
|
279 | 82 | } |
|
280 | 1 | ||
281 | // set the table comment |
||
282 | if (isset($options['comment'])) { |
||
283 | $optionsStr .= sprintf(' COMMENT=%s ', $this->getConnection()->quote($options['comment'])); |
||
284 | 82 | } |
|
285 | 82 | ||
286 | 10 | // set the table row format |
|
287 | 82 | if (isset($options['row_format'])) { |
|
288 | $optionsStr .= sprintf(' ROW_FORMAT=%s ', $options['row_format']); |
||
289 | } |
||
290 | 82 | ||
291 | 82 | $sql = 'CREATE TABLE '; |
|
292 | 2 | $sql .= $this->quoteTableName($table->getName()) . ' ('; |
|
293 | 82 | foreach ($columns as $column) { |
|
294 | $sql .= $this->quoteColumnName($column->getName()) . ' ' . $this->getColumnSqlDefinition($column) . ', '; |
||
295 | 82 | } |
|
296 | 82 | ||
297 | // set the primary key(s) |
||
298 | View Code Duplication | if (isset($options['primary_key'])) { |
|
299 | 82 | $sql = rtrim($sql); |
|
300 | 82 | $sql .= ' PRIMARY KEY ('; |
|
301 | if (is_string($options['primary_key'])) { // handle primary_key => 'id' |
||
302 | $sql .= $this->quoteColumnName($options['primary_key']); |
||
303 | } elseif (is_array($options['primary_key'])) { // handle primary_key => array('tag_id', 'resource_id') |
||
304 | $sql .= implode(',', array_map([$this, 'quoteColumnName'], $options['primary_key'])); |
||
305 | 5 | } |
|
306 | $sql .= ')'; |
||
307 | 5 | } else { |
|
308 | 5 | $sql = substr(rtrim($sql), 0, -1); // no primary keys |
|
309 | } |
||
310 | |||
311 | // set the indexes |
||
312 | foreach ($indexes as $index) { |
||
313 | 5 | $sql .= ', ' . $this->getIndexSqlDefinition($index); |
|
314 | } |
||
315 | 5 | ||
316 | 5 | $sql .= ') ' . $optionsStr; |
|
317 | $sql = rtrim($sql); |
||
318 | |||
319 | // execute the sql |
||
320 | $this->execute($sql); |
||
321 | 1 | ||
322 | $this->addCreatedTable($table->getName()); |
||
323 | 1 | } |
|
324 | 1 | ||
325 | 1 | /** |
|
326 | 1 | * {@inheritDoc} |
|
327 | * |
||
328 | 1 | * @throws \InvalidArgumentException |
|
329 | 1 | */ |
|
330 | protected function getChangePrimaryKeyInstructions(Table $table, $newColumns) |
||
359 | 12 | ||
360 | /** |
||
361 | * @inheritDoc |
||
362 | */ |
||
363 | protected function getChangeCommentInstructions(Table $table, $newComment) |
||
376 | |||
377 | /** |
||
378 | * @inheritDoc |
||
379 | */ |
||
380 | View Code Duplication | protected function getRenameTableInstructions($tableName, $newTableName) |
|
391 | |||
392 | /** |
||
393 | * @inheritDoc |
||
394 | */ |
||
395 | View Code Duplication | protected function getDropTableInstructions($tableName) |
|
402 | 18 | ||
403 | 18 | /** |
|
404 | * {@inheritDoc} |
||
405 | 18 | * |
|
406 | 2 | * @return void |
|
407 | 2 | */ |
|
408 | public function truncateTable($tableName) |
||
409 | 18 | { |
|
410 | 18 | $sql = sprintf( |
|
411 | 'TRUNCATE TABLE %s', |
||
412 | $this->quoteTableName($tableName) |
||
413 | ); |
||
414 | |||
415 | 7 | $this->execute($sql); |
|
416 | } |
||
417 | 7 | ||
418 | 7 | /** |
|
419 | 7 | * @inheritDoc |
|
420 | 5 | */ |
|
421 | 5 | public function getColumns($tableName) |
|
450 | |||
451 | 5 | /** |
|
452 | 5 | * @inheritDoc |
|
453 | 5 | */ |
|
454 | 5 | View Code Duplication | public function hasColumn($tableName, $columnName) |
465 | |||
466 | /** |
||
467 | 5 | * @inheritDoc |
|
468 | */ |
||
469 | 5 | protected function getAddColumnInstructions(Table $table, Column $column) |
|
483 | |||
484 | 19 | /** |
|
485 | * {@inheritDoc} |
||
486 | 19 | * |
|
487 | 19 | * @throws \InvalidArgumentException |
|
488 | 19 | */ |
|
489 | 18 | protected function getRenameColumnInstructions($tableName, $columnName, $newColumnName) |
|
519 | |||
520 | /** |
||
521 | 1 | * @inheritDoc |
|
522 | */ |
||
523 | 1 | protected function getChangeColumnInstructions($tableName, $columnName, Column $newColumn) |
|
536 | |||
537 | 4 | /** |
|
538 | * @inheritDoc |
||
539 | 4 | */ |
|
540 | 4 | protected function getDropColumnInstructions($tableName, $columnName) |
|
546 | 4 | ||
547 | /** |
||
548 | * Get an array of indexes from a particular table. |
||
549 | * |
||
550 | * @param string $tableName Table Name |
||
551 | 3 | * |
|
552 | * @return array |
||
553 | 3 | */ |
|
554 | 2 | protected function getIndexes($tableName) |
|
567 | 3 | ||
568 | 3 | /** |
|
569 | 3 | * @inheritDoc |
|
570 | */ |
||
571 | 3 | View Code Duplication | public function hasIndex($tableName, $columns) |
588 | 2 | ||
589 | 2 | /** |
|
590 | 2 | * @inheritDoc |
|
591 | 2 | */ |
|
592 | View Code Duplication | public function hasIndexByName($tableName, $indexName) |
|
604 | 21 | ||
605 | 21 | /** |
|
606 | 6 | * @inheritDoc |
|
607 | 4 | */ |
|
608 | protected function getAddIndexInstructions(Table $table, Index $index) |
||
633 | |||
634 | /** |
||
635 | * {@inheritDoc} |
||
636 | * |
||
637 | * @throws \InvalidArgumentException |
||
638 | */ |
||
639 | protected function getDropIndexByColumnsInstructions($tableName, $columns) |
||
662 | 15 | ||
663 | 15 | /** |
|
664 | 15 | * {@inheritDoc} |
|
665 | * |
||
666 | * @throws \InvalidArgumentException |
||
667 | */ |
||
668 | protected function getDropIndexByNameInstructions($tableName, $indexName) |
||
686 | 7 | ||
687 | 7 | /** |
|
688 | * @inheritDoc |
||
689 | */ |
||
690 | View Code Duplication | public function hasPrimaryKey($tableName, $columns, $constraint = null) |
|
709 | 96 | ||
710 | /** |
||
711 | * Get the primary key from a particular table. |
||
712 | 96 | * |
|
713 | 87 | * @param string $tableName Table Name |
|
714 | * |
||
715 | 96 | * @return array |
|
716 | 4 | */ |
|
717 | View Code Duplication | public function getPrimaryKey($tableName) |
|
741 | 95 | ||
742 | 1 | /** |
|
743 | * @inheritDoc |
||
744 | */ |
||
745 | 1 | public function hasForeignKey($tableName, $columns, $constraint = null) |
|
767 | 6 | ||
768 | /** |
||
769 | 6 | * Get an array of foreign keys from a particular table. |
|
770 | 6 | * |
|
771 | 6 | * @param string $tableName Table Name |
|
772 | 6 | * |
|
773 | 6 | * @return array |
|
774 | 6 | */ |
|
775 | 6 | protected function getForeignKeys($tableName) |
|
806 | |||
807 | 83 | /** |
|
808 | 80 | * @inheritDoc |
|
809 | */ |
||
810 | 10 | View Code Duplication | protected function getAddForeignKeyInstructions(Table $table, ForeignKey $foreignKey) |
819 | 5 | ||
820 | /** |
||
821 | 6 | * @inheritDoc |
|
822 | 4 | */ |
|
823 | protected function getDropForeignKeyInstructions($tableName, $constraint) |
||
832 | |||
833 | 2 | /** |
|
834 | 2 | * {@inheritDoc} |
|
835 | 2 | * |
|
836 | * @throws \InvalidArgumentException |
||
837 | */ |
||
838 | protected function getDropForeignKeyByColumnsInstructions($tableName, $columns) |
||
870 | 3 | ||
871 | 6 | /** |
|
872 | 16 | * {@inheritDoc} |
|
873 | 5 | * |
|
874 | 5 | * @throws \Phinx\Db\Adapter\UnsupportedColumnTypeException |
|
875 | 1 | */ |
|
876 | 1 | public function getSqlType($type, $limit = null) |
|
988 | |||
989 | 81 | /** |
|
990 | * Returns Phinx type by SQL type |
||
991 | 81 | * |
|
992 | 81 | * @internal param string $sqlType SQL type |
|
993 | * |
||
994 | * @param string $sqlTypeDef |
||
995 | * |
||
996 | * @throws \Phinx\Db\Adapter\UnsupportedColumnTypeException |
||
997 | * |
||
998 | * @return array Phinx type |
||
999 | */ |
||
1000 | 89 | public function getPhinxType($sqlTypeDef) |
|
1114 | 2 | ||
1115 | /** |
||
1116 | 2 | * {@inheritDoc} |
|
1117 | * |
||
1118 | * @return void |
||
1119 | */ |
||
1120 | public function createDatabase($name, $options = []) |
||
1130 | |||
1131 | /** |
||
1132 | * @inheritDoc |
||
1133 | */ |
||
1134 | public function hasDatabase($name) |
||
1151 | |||
1152 | /** |
||
1153 | * {@inheritDoc} |
||
1154 | * |
||
1155 | * @return void |
||
1156 | */ |
||
1157 | public function dropDatabase($name) |
||
1161 | |||
1162 | /** |
||
1163 | * Gets the MySQL Column Definition for a Column object. |
||
1164 | * |
||
1165 | * @param \Phinx\Db\Table\Column $column Column |
||
1166 | * |
||
1167 | * @return string |
||
1168 | */ |
||
1169 | protected function getColumnSqlDefinition(Column $column) |
||
1202 | |||
1203 | /** |
||
1204 | * Gets the MySQL Index Definition for an Index object. |
||
1205 | * |
||
1206 | * @param \Phinx\Db\Table\Index $index Index |
||
1207 | * |
||
1208 | * @return string |
||
1209 | */ |
||
1210 | protected function getIndexSqlDefinition(Index $index) |
||
1248 | |||
1249 | /** |
||
1250 | * Gets the MySQL Foreign Key Definition for an ForeignKey object. |
||
1251 | * |
||
1252 | * @param \Phinx\Db\Table\ForeignKey $foreignKey |
||
1253 | * |
||
1254 | * @return string |
||
1255 | */ |
||
1256 | View Code Duplication | protected function getForeignKeySqlDefinition(ForeignKey $foreignKey) |
|
1281 | |||
1282 | /** |
||
1283 | * Describes a database table. This is a MySQL adapter specific method. |
||
1284 | * |
||
1285 | * @param string $tableName Table name |
||
1286 | * |
||
1287 | * @return array |
||
1288 | */ |
||
1289 | public function describeTable($tableName) |
||
1305 | |||
1306 | /** |
||
1307 | * Returns MySQL column types (inherited and MySQL specified). |
||
1308 | * |
||
1309 | * @return array |
||
1310 | */ |
||
1311 | public function getColumnTypes() |
||
1315 | |||
1316 | /** |
||
1317 | * @inheritDoc |
||
1318 | */ |
||
1319 | View Code Duplication | public function getDecoratedConnection() |
|
1334 | } |
||
1335 |
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.