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 |
||
43 | class MysqlAdapter extends PdoAdapter implements AdapterInterface |
||
44 | { |
||
45 | |||
46 | protected $signedColumnTypes = ['integer' => true, 'biginteger' => true, 'float' => true, 'decimal' => true, 'boolean' => true]; |
||
47 | |||
48 | const TEXT_TINY = 255; |
||
49 | const TEXT_SMALL = 255; /* deprecated, alias of TEXT_TINY */ |
||
50 | const TEXT_REGULAR = 65535; |
||
51 | const TEXT_MEDIUM = 16777215; |
||
52 | const TEXT_LONG = 4294967295; |
||
53 | |||
54 | // According to https://dev.mysql.com/doc/refman/5.0/en/blob.html BLOB sizes are the same as TEXT |
||
55 | const BLOB_TINY = 255; |
||
56 | const BLOB_SMALL = 255; /* deprecated, alias of BLOB_TINY */ |
||
57 | const BLOB_REGULAR = 65535; |
||
58 | const BLOB_MEDIUM = 16777215; |
||
59 | const BLOB_LONG = 4294967295; |
||
60 | |||
61 | const INT_TINY = 255; |
||
62 | const INT_SMALL = 65535; |
||
63 | const INT_MEDIUM = 16777215; |
||
64 | const INT_REGULAR = 4294967295; |
||
65 | const INT_BIG = 18446744073709551615; |
||
66 | |||
67 | const BIT = 64; |
||
68 | |||
69 | const TYPE_YEAR = 'year'; |
||
70 | 80 | ||
71 | /** |
||
72 | 80 | * {@inheritdoc} |
|
73 | 80 | */ |
|
74 | public function connect() |
||
75 | { |
||
76 | if ($this->connection === null) { |
||
77 | if (!class_exists('PDO') || !in_array('mysql', \PDO::getAvailableDrivers(), true)) { |
||
78 | // @codeCoverageIgnoreStart |
||
79 | 80 | throw new \RuntimeException('You need to enable the PDO_Mysql extension for Phinx to run properly.'); |
|
80 | 80 | // @codeCoverageIgnoreEnd |
|
81 | } |
||
82 | 80 | ||
83 | $db = null; |
||
84 | 80 | $options = $this->getOptions(); |
|
85 | |||
86 | $dsn = 'mysql:'; |
||
87 | |||
88 | if (!empty($options['unix_socket'])) { |
||
89 | 80 | // use socket connection |
|
90 | 80 | $dsn .= 'unix_socket=' . $options['unix_socket']; |
|
91 | 80 | } else { |
|
92 | 80 | // use network connection |
|
93 | $dsn .= 'host=' . $options['host']; |
||
94 | if (!empty($options['port'])) { |
||
95 | 80 | $dsn .= ';port=' . $options['port']; |
|
96 | } |
||
97 | } |
||
98 | 80 | ||
99 | $dsn .= ';dbname=' . $options['name']; |
||
100 | |||
101 | // charset support |
||
102 | 80 | if (!empty($options['charset'])) { |
|
103 | $dsn .= ';charset=' . $options['charset']; |
||
104 | } |
||
105 | |||
106 | 80 | $driverOptions = [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]; |
|
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 | try { |
|
117 | 1 | $db = new \PDO($dsn, $options['user'], $options['pass'], $driverOptions); |
|
118 | 1 | } catch (\PDOException $exception) { |
|
119 | throw new \InvalidArgumentException(sprintf( |
||
120 | 'There was a problem connecting to the database: %s', |
||
121 | 80 | $exception->getMessage() |
|
122 | 80 | )); |
|
123 | 80 | } |
|
124 | |||
125 | $this->setConnection($db); |
||
126 | } |
||
127 | } |
||
128 | 81 | ||
129 | /** |
||
130 | 81 | * {@inheritdoc} |
|
131 | 81 | */ |
|
132 | public function disconnect() |
||
133 | { |
||
134 | $this->connection = null; |
||
135 | } |
||
136 | 6 | ||
137 | /** |
||
138 | 6 | * {@inheritdoc} |
|
139 | */ |
||
140 | public function hasTransactions() |
||
141 | { |
||
142 | return true; |
||
143 | } |
||
144 | 6 | ||
145 | /** |
||
146 | 6 | * {@inheritdoc} |
|
147 | 6 | */ |
|
148 | public function beginTransaction() |
||
149 | { |
||
150 | $this->execute('START TRANSACTION'); |
||
151 | } |
||
152 | 6 | ||
153 | /** |
||
154 | 6 | * {@inheritdoc} |
|
155 | 6 | */ |
|
156 | public function commitTransaction() |
||
157 | { |
||
158 | $this->execute('COMMIT'); |
||
159 | } |
||
160 | 1 | ||
161 | /** |
||
162 | 1 | * {@inheritdoc} |
|
163 | 1 | */ |
|
164 | public function rollbackTransaction() |
||
165 | { |
||
166 | $this->execute('ROLLBACK'); |
||
167 | } |
||
168 | 112 | ||
169 | /** |
||
170 | 112 | * {@inheritdoc} |
|
171 | */ |
||
172 | public function quoteTableName($tableName) |
||
173 | { |
||
174 | return str_replace('.', '`.`', $this->quoteColumnName($tableName)); |
||
175 | } |
||
176 | 112 | ||
177 | /** |
||
178 | 112 | * {@inheritdoc} |
|
179 | */ |
||
180 | public function quoteColumnName($columnName) |
||
181 | { |
||
182 | return '`' . str_replace('`', '``', $columnName) . '`'; |
||
183 | } |
||
184 | 82 | ||
185 | /** |
||
186 | 82 | * {@inheritdoc} |
|
187 | */ |
||
188 | 82 | public function hasTable($tableName) |
|
189 | { |
||
190 | if (strpos($tableName, '.') !== false) { |
||
191 | 82 | list($schema, $table) = explode('.', $tableName); |
|
192 | 82 | ||
193 | $exists = $this->queryTableExists($schema, $table); |
||
194 | 82 | ||
195 | // Only break here on success, because it is possible for table names to contain a dot. |
||
196 | 82 | if (!empty($exists)) { |
|
197 | return true; |
||
198 | } |
||
199 | } |
||
200 | |||
201 | $options = $this->getOptions(); |
||
202 | 82 | ||
203 | $exists = $this->queryTableExists($options['name'], $tableName); |
||
204 | |||
205 | return !empty($exists); |
||
206 | 82 | } |
|
207 | |||
208 | 82 | /** |
|
209 | 82 | * @param string $schema The table schema |
|
210 | * @param string $tableName The table name |
||
211 | * |
||
212 | 82 | * @return array|mixed |
|
213 | 82 | */ |
|
214 | 68 | private function queryTableExists($schema, $tableName) |
|
215 | 68 | { |
|
216 | 68 | return $this->fetchRow(sprintf( |
|
217 | 68 | "SELECT TABLE_NAME |
|
218 | 68 | FROM INFORMATION_SCHEMA.TABLES |
|
219 | WHERE TABLE_SCHEMA = '%s' AND TABLE_NAME = '%s'", |
||
220 | 68 | $schema, |
|
221 | 68 | $tableName |
|
222 | 82 | )); |
|
223 | } |
||
224 | 2 | ||
225 | 2 | /** |
|
226 | 2 | * {@inheritdoc} |
|
227 | 2 | */ |
|
228 | public function createTable(Table $table, array $columns = [], array $indexes = []) |
||
229 | 2 | { |
|
230 | 2 | // This method is based on the MySQL docs here: http://dev.mysql.com/doc/refman/5.1/en/create-index.html |
|
231 | 2 | $defaultOptions = [ |
|
232 | 'engine' => 'InnoDB', |
||
233 | 'collation' => 'utf8_general_ci' |
||
234 | ]; |
||
235 | |||
236 | 82 | $options = array_merge( |
|
237 | 82 | $defaultOptions, |
|
238 | 82 | array_intersect_key($this->getOptions(), $defaultOptions), |
|
239 | 82 | $table->getOptions() |
|
240 | ); |
||
241 | |||
242 | 82 | // Add the default primary key |
|
243 | 82 | if (!isset($options['id']) || (isset($options['id']) && $options['id'] === true)) { |
|
244 | 82 | $column = new Column(); |
|
245 | 82 | $column->setName('id') |
|
246 | 82 | ->setType('integer') |
|
247 | ->setSigned(isset($options['signed']) ? $options['signed'] : true) |
||
248 | ->setIdentity(true); |
||
249 | 82 | ||
250 | 2 | array_unshift($columns, $column); |
|
251 | 2 | $options['primary_key'] = 'id'; |
|
252 | } elseif (isset($options['id']) && is_string($options['id'])) { |
||
253 | 82 | // Handle id => "field_name" to support AUTO_INCREMENT |
|
254 | 82 | $column = new Column(); |
|
255 | 82 | $column->setName($options['id']) |
|
256 | 82 | ->setType('integer') |
|
257 | 82 | ->setSigned(isset($options['signed']) ? $options['signed'] : true) |
|
258 | ->setIdentity(true); |
||
259 | |||
260 | 82 | array_unshift($columns, $column); |
|
261 | 82 | $options['primary_key'] = $options['id']; |
|
262 | 82 | } |
|
263 | 82 | ||
264 | 81 | // TODO - process table options like collation etc |
|
265 | 82 | ||
266 | // process table engine (default to InnoDB) |
||
267 | $optionsStr = 'ENGINE = InnoDB'; |
||
268 | 2 | if (isset($options['engine'])) { |
|
269 | 2 | $optionsStr = sprintf('ENGINE = %s', $options['engine']); |
|
270 | 2 | } |
|
271 | 2 | ||
272 | 2 | // process table collation |
|
273 | 2 | if (isset($options['collation'])) { |
|
274 | 2 | $charset = explode('_', $options['collation']); |
|
275 | 2 | $optionsStr .= sprintf(' CHARACTER SET %s', $charset[0]); |
|
276 | 2 | $optionsStr .= sprintf(' COLLATE %s', $options['collation']); |
|
277 | 2 | } |
|
278 | 82 | ||
279 | 82 | // set the table comment |
|
280 | 1 | if (isset($options['comment'])) { |
|
281 | $optionsStr .= sprintf(" COMMENT=%s ", $this->getConnection()->quote($options['comment'])); |
||
282 | } |
||
283 | |||
284 | 82 | $sql = 'CREATE TABLE '; |
|
285 | 82 | $sql .= $this->quoteTableName($table->getName()) . ' ('; |
|
286 | 10 | foreach ($columns as $column) { |
|
287 | 82 | $sql .= $this->quoteColumnName($column->getName()) . ' ' . $this->getColumnSqlDefinition($column) . ', '; |
|
288 | } |
||
289 | |||
290 | 82 | // set the primary key(s) |
|
291 | 82 | View Code Duplication | if (isset($options['primary_key'])) { |
292 | 2 | $sql = rtrim($sql); |
|
293 | 82 | $sql .= ' PRIMARY KEY ('; |
|
294 | if (is_string($options['primary_key'])) { // handle primary_key => 'id' |
||
295 | 82 | $sql .= $this->quoteColumnName($options['primary_key']); |
|
296 | 82 | } elseif (is_array($options['primary_key'])) { // handle primary_key => array('tag_id', 'resource_id') |
|
297 | $sql .= implode(',', array_map([$this, 'quoteColumnName'], $options['primary_key'])); |
||
298 | } |
||
299 | 82 | $sql .= ')'; |
|
300 | 82 | } else { |
|
301 | $sql = substr(rtrim($sql), 0, -1); // no primary keys |
||
302 | } |
||
303 | |||
304 | // set the indexes |
||
305 | 5 | foreach ($indexes as $index) { |
|
306 | $sql .= ', ' . $this->getIndexSqlDefinition($index); |
||
307 | 5 | } |
|
308 | 5 | ||
309 | $sql .= ') ' . $optionsStr; |
||
310 | $sql = rtrim($sql) . ';'; |
||
311 | |||
312 | // execute the sql |
||
313 | 5 | $this->execute($sql); |
|
314 | } |
||
315 | 5 | ||
316 | 5 | /** |
|
317 | * {@inheritdoc} |
||
318 | */ |
||
319 | View Code Duplication | protected function getRenameTableInstructions($tableName, $newTableName) |
|
320 | { |
||
321 | 1 | $sql = sprintf( |
|
322 | 'RENAME TABLE %s TO %s', |
||
323 | 1 | $this->quoteTableName($tableName), |
|
324 | 1 | $this->quoteTableName($newTableName) |
|
325 | 1 | ); |
|
326 | 1 | ||
327 | return new AlterInstructions([], [$sql]); |
||
328 | 1 | } |
|
329 | 1 | ||
330 | /** |
||
331 | * {@inheritdoc} |
||
332 | */ |
||
333 | protected function getDropTableInstructions($tableName) |
||
334 | 12 | { |
|
335 | $sql = sprintf('DROP TABLE %s', $this->quoteTableName($tableName)); |
||
336 | 12 | ||
337 | 12 | return new AlterInstructions([], [$sql]); |
|
338 | 12 | } |
|
339 | 12 | ||
340 | /** |
||
341 | 12 | * {@inheritdoc} |
|
342 | 12 | */ |
|
343 | 12 | public function truncateTable($tableName) |
|
344 | 12 | { |
|
345 | 12 | $sql = sprintf( |
|
346 | 12 | 'TRUNCATE TABLE %s', |
|
347 | $this->quoteTableName($tableName) |
||
348 | 12 | ); |
|
349 | 12 | ||
350 | 12 | $this->execute($sql); |
|
351 | } |
||
352 | 12 | ||
353 | 3 | /** |
|
354 | 3 | * {@inheritdoc} |
|
355 | */ |
||
356 | 12 | public function getColumns($tableName) |
|
357 | 12 | { |
|
358 | $columns = []; |
||
359 | 12 | $rows = $this->fetchAll(sprintf('SHOW COLUMNS FROM %s', $this->quoteTableName($tableName))); |
|
360 | foreach ($rows as $columnInfo) { |
||
361 | $phinxType = $this->getPhinxType($columnInfo['Type']); |
||
362 | |||
363 | $column = new Column(); |
||
364 | $column->setName($columnInfo['Field']) |
||
365 | 79 | ->setNull($columnInfo['Null'] !== 'NO') |
|
366 | ->setDefault($columnInfo['Default']) |
||
367 | 79 | ->setType($phinxType['name']) |
|
368 | 79 | ->setSigned(strpos($columnInfo['Type'], 'unsigned') === false) |
|
369 | 79 | ->setLimit($phinxType['limit']); |
|
370 | 77 | ||
371 | if ($columnInfo['Extra'] === 'auto_increment') { |
||
372 | 77 | $column->setIdentity(true); |
|
373 | } |
||
374 | 21 | ||
375 | if (isset($phinxType['values'])) { |
||
376 | $column->setValues($phinxType['values']); |
||
377 | } |
||
378 | |||
379 | $columns[] = $column; |
||
380 | } |
||
381 | |||
382 | return $columns; |
||
383 | 95 | } |
|
384 | |||
385 | 95 | /** |
|
386 | 10 | * {@inheritdoc} |
|
387 | 95 | */ |
|
388 | 79 | View Code Duplication | public function hasColumn($tableName, $columnName) |
389 | 79 | { |
|
390 | 95 | $rows = $this->fetchAll(sprintf('SHOW COLUMNS FROM %s', $this->quoteTableName($tableName))); |
|
391 | foreach ($rows as $column) { |
||
392 | if (strcasecmp($column['Field'], $columnName) === 0) { |
||
393 | return true; |
||
394 | } |
||
395 | } |
||
396 | 18 | ||
397 | return false; |
||
398 | 18 | } |
|
399 | 18 | ||
400 | 18 | /** |
|
401 | 18 | * {@inheritdoc} |
|
402 | 18 | */ |
|
403 | 18 | protected function getAddColumnInstructions(Table $table, Column $column) |
|
404 | { |
||
405 | 18 | $alter = sprintf( |
|
406 | 2 | 'ADD %s %s', |
|
407 | 2 | $this->quoteColumnName($column->getName()), |
|
408 | $this->getColumnSqlDefinition($column) |
||
409 | 18 | ); |
|
410 | 18 | ||
411 | if ($column->getAfter()) { |
||
412 | $alter .= ' AFTER ' . $this->quoteColumnName($column->getAfter()); |
||
413 | } |
||
414 | |||
415 | 7 | return new AlterInstructions([$alter]); |
|
416 | } |
||
417 | 7 | ||
418 | 7 | /** |
|
419 | 7 | * {@inheritdoc} |
|
420 | 5 | */ |
|
421 | 5 | protected function getRenameColumnInstructions($tableName, $columnName, $newColumnName) |
|
422 | 5 | { |
|
423 | 1 | $rows = $this->fetchAll(sprintf('DESCRIBE %s', $this->quoteTableName($tableName))); |
|
424 | 1 | foreach ($rows as $row) { |
|
425 | 5 | if (strcasecmp($row['Field'], $columnName) === 0) { |
|
426 | $null = ($row['Null'] == 'NO') ? 'NOT NULL' : 'NULL'; |
||
427 | 5 | $extra = ' ' . strtoupper($row['Extra']); |
|
428 | 5 | if (!is_null($row['Default'])) { |
|
429 | 5 | $extra .= $this->getDefaultValueDefinition($row['Default']); |
|
430 | 5 | } |
|
431 | 5 | $definition = $row['Type'] . ' ' . $null . $extra; |
|
432 | 5 | ||
433 | $alter = sprintf( |
||
434 | 5 | 'CHANGE COLUMN %s %s %s', |
|
435 | 5 | $this->quoteColumnName($columnName), |
|
436 | 5 | $this->quoteColumnName($newColumnName), |
|
437 | $definition |
||
438 | 6 | ); |
|
439 | |||
440 | 2 | return new AlterInstructions([$alter]); |
|
441 | } |
||
442 | } |
||
443 | 2 | ||
444 | throw new \InvalidArgumentException(sprintf( |
||
445 | 'The specified column doesn\'t exist: ' . |
||
446 | $columnName |
||
447 | )); |
||
448 | } |
||
449 | 5 | ||
450 | /** |
||
451 | 5 | * {@inheritdoc} |
|
452 | 5 | */ |
|
453 | 5 | protected function getChangeColumnInstructions($tableName, $columnName, Column $newColumn) |
|
454 | 5 | { |
|
455 | 5 | $after = $newColumn->getAfter() ? ' AFTER ' . $this->quoteColumnName($newColumn->getAfter()) : ''; |
|
456 | 5 | $alter = sprintf( |
|
457 | 5 | 'CHANGE %s %s %s%s', |
|
458 | 5 | $this->quoteColumnName($columnName), |
|
459 | $this->quoteColumnName($newColumn->getName()), |
||
460 | 5 | $this->getColumnSqlDefinition($newColumn), |
|
461 | 5 | $after |
|
462 | 5 | ); |
|
463 | |||
464 | return new AlterInstructions([$alter]); |
||
465 | } |
||
466 | |||
467 | 5 | /** |
|
468 | * {@inheritdoc} |
||
469 | 5 | */ |
|
470 | 5 | protected function getDropColumnInstructions($tableName, $columnName) |
|
471 | 5 | { |
|
472 | 5 | $alter = sprintf('DROP COLUMN %s', $this->quoteColumnName($columnName)); |
|
473 | 5 | ||
474 | 5 | return new AlterInstructions([$alter]); |
|
475 | 5 | } |
|
476 | 5 | ||
477 | /** |
||
478 | * Get an array of indexes from a particular table. |
||
479 | * |
||
480 | * @param string $tableName Table Name |
||
481 | * @return array |
||
482 | */ |
||
483 | View Code Duplication | protected function getIndexes($tableName) |
|
484 | 19 | { |
|
485 | $indexes = []; |
||
486 | 19 | $rows = $this->fetchAll(sprintf('SHOW INDEXES FROM %s', $this->quoteTableName($tableName))); |
|
487 | 19 | foreach ($rows as $row) { |
|
488 | 19 | if (!isset($indexes[$row['Key_name']])) { |
|
489 | 18 | $indexes[$row['Key_name']] = ['columns' => []]; |
|
490 | 18 | } |
|
491 | 18 | $indexes[$row['Key_name']]['columns'][] = strtolower($row['Column_name']); |
|
492 | 18 | } |
|
493 | 19 | ||
494 | 19 | return $indexes; |
|
495 | } |
||
496 | |||
497 | /** |
||
498 | * {@inheritdoc} |
||
499 | */ |
||
500 | 14 | public function hasIndex($tableName, $columns) |
|
517 | |||
518 | /** |
||
519 | * {@inheritdoc} |
||
520 | */ |
||
521 | 1 | View Code Duplication | public function hasIndexByName($tableName, $indexName) |
533 | |||
534 | /** |
||
535 | * {@inheritdoc} |
||
536 | */ |
||
537 | 4 | protected function getAddIndexInstructions(Table $table, Index $index) |
|
538 | { |
||
539 | 4 | $alter = sprintf( |
|
540 | 4 | 'ADD %s', |
|
541 | 4 | $this->getIndexSqlDefinition($index) |
|
542 | 4 | ); |
|
543 | 4 | ||
544 | 4 | return new AlterInstructions([$alter]); |
|
545 | 4 | } |
|
546 | 4 | ||
547 | /** |
||
548 | * {@inheritdoc} |
||
549 | */ |
||
550 | protected function getDropIndexByColumnsInstructions($tableName, $columns) |
||
551 | 3 | { |
|
552 | if (is_string($columns)) { |
||
553 | 3 | $columns = [$columns]; // str to array |
|
554 | 2 | } |
|
555 | 2 | ||
556 | $indexes = $this->getIndexes($tableName); |
||
557 | 3 | $columns = array_map('strtolower', $columns); |
|
558 | 3 | ||
559 | View Code Duplication | foreach ($indexes as $indexName => $index) { |
|
560 | 3 | if ($columns == $index['columns']) { |
|
561 | 3 | return new AlterInstructions([sprintf( |
|
562 | 3 | 'DROP INDEX %s', |
|
563 | 3 | $this->quoteColumnName($indexName) |
|
564 | 3 | )]); |
|
565 | 3 | } |
|
566 | 3 | } |
|
567 | 3 | ||
568 | 3 | throw new \InvalidArgumentException(sprintf( |
|
569 | 3 | "The specified index on columns '%s' does not exist", |
|
570 | implode(',', $columns) |
||
571 | 3 | )); |
|
572 | 1 | } |
|
573 | |||
574 | /** |
||
575 | * {@inheritdoc} |
||
576 | */ |
||
577 | 2 | protected function getDropIndexByNameInstructions($tableName, $indexName) |
|
578 | { |
||
579 | 2 | ||
580 | $indexes = $this->getIndexes($tableName); |
||
581 | 2 | ||
582 | View Code Duplication | foreach ($indexes as $name => $index) { |
|
583 | 2 | if ($name === $indexName) { |
|
584 | 2 | return new AlterInstructions([sprintf( |
|
585 | 2 | 'DROP INDEX %s', |
|
586 | 2 | $this->quoteColumnName($indexName) |
|
587 | 2 | )]); |
|
588 | 2 | } |
|
589 | 2 | } |
|
590 | 2 | ||
591 | 2 | throw new \InvalidArgumentException(sprintf( |
|
592 | "The specified index name '%s' does not exist", |
||
593 | 2 | $indexName |
|
594 | )); |
||
595 | } |
||
596 | |||
597 | /** |
||
598 | * {@inheritdoc} |
||
599 | 21 | */ |
|
600 | View Code Duplication | public function hasForeignKey($tableName, $columns, $constraint = null) |
|
622 | |||
623 | /** |
||
624 | * Get an array of foreign keys from a particular table. |
||
625 | * |
||
626 | 22 | * @param string $tableName Table Name |
|
627 | * @return array |
||
628 | 22 | */ |
|
629 | 22 | protected function getForeignKeys($tableName) |
|
630 | { |
||
631 | if (strpos($tableName, '.') !== false) { |
||
632 | list($schema, $tableName) = explode('.', $tableName); |
||
633 | } |
||
634 | |||
635 | $foreignKeys = []; |
||
636 | $rows = $this->fetchAll(sprintf( |
||
637 | "SELECT |
||
638 | CONSTRAINT_NAME, |
||
639 | CONCAT(TABLE_SCHEMA, '.', TABLE_NAME) AS TABLE_NAME, |
||
640 | 22 | COLUMN_NAME, |
|
641 | CONCAT(REFERENCED_TABLE_SCHEMA, '.', REFERENCED_TABLE_NAME) AS REFERENCED_TABLE_NAME, |
||
642 | 22 | REFERENCED_COLUMN_NAME |
|
643 | 22 | FROM information_schema.KEY_COLUMN_USAGE |
|
644 | 19 | WHERE REFERENCED_TABLE_NAME IS NOT NULL |
|
645 | 19 | AND TABLE_SCHEMA = %s |
|
660 | 15 | ||
661 | 15 | /** |
|
662 | 15 | * {@inheritdoc} |
|
663 | 15 | */ |
|
664 | 15 | View Code Duplication | protected function getAddForeignKeyInstructions(Table $table, ForeignKey $foreignKey) |
673 | 3 | ||
674 | /** |
||
675 | * {@inheritdoc} |
||
676 | 8 | */ |
|
677 | 8 | protected function getDropForeignKeyInstructions($tableName, $constraint) |
|
686 | 7 | ||
687 | 7 | /** |
|
688 | * {@inheritdoc} |
||
689 | */ |
||
690 | protected function getDropForeignKeyByColumnsInstructions($tableName, $columns) |
||
722 | 6 | ||
723 | 6 | /** |
|
724 | 6 | * {@inheritdoc} |
|
725 | 6 | */ |
|
726 | 6 | public function getSqlType($type, $limit = null) |
|
834 | 2 | ||
835 | 2 | /** |
|
836 | * Returns Phinx type by SQL type |
||
837 | * |
||
838 | * @param string $sqlTypeDef |
||
839 | * @throws \RuntimeException |
||
840 | * @internal param string $sqlType SQL type |
||
841 | * @returns string Phinx type |
||
842 | */ |
||
843 | public function getPhinxType($sqlTypeDef) |
||
953 | |||
954 | 83 | /** |
|
955 | * {@inheritdoc} |
||
956 | 83 | */ |
|
957 | public function createDatabase($name, $options = []) |
||
967 | |||
968 | 4 | /** |
|
969 | * {@inheritdoc} |
||
970 | 4 | */ |
|
971 | 4 | public function hasDatabase($name) |
|
988 | |||
989 | 81 | /** |
|
990 | * {@inheritdoc} |
||
991 | 81 | */ |
|
992 | 81 | public function dropDatabase($name) |
|
996 | |||
997 | /** |
||
998 | * Gets the MySQL Column Definition for a Column object. |
||
999 | * |
||
1000 | 89 | * @param \Phinx\Db\Table\Column $column Column |
|
1001 | * @return string |
||
1002 | 89 | */ |
|
1003 | protected function getColumnSqlDefinition(Column $column) |
||
1036 | |||
1037 | /** |
||
1038 | 16 | * Gets the MySQL Index Definition for an Index object. |
|
1039 | * |
||
1040 | 16 | * @param \Phinx\Db\Table\Index $index Index |
|
1041 | 16 | * @return string |
|
1042 | 16 | */ |
|
1043 | 2 | protected function getIndexSqlDefinition(Index $index) |
|
1069 | |||
1070 | /** |
||
1071 | 17 | * Gets the MySQL Foreign Key Definition for an ForeignKey object. |
|
1072 | * |
||
1073 | 17 | * @param \Phinx\Db\Table\ForeignKey $foreignKey |
|
1074 | 17 | * @return string |
|
1075 | 5 | */ |
|
1076 | 5 | View Code Duplication | protected function getForeignKeySqlDefinition(ForeignKey $foreignKey) |
1101 | |||
1102 | 2 | /** |
|
1103 | * Describes a database table. This is a MySQL adapter specific method. |
||
1104 | 2 | * |
|
1105 | * @param string $tableName Table name |
||
1106 | * @return array |
||
1107 | 2 | */ |
|
1108 | public function describeTable($tableName) |
||
1124 | |||
1125 | 85 | /** |
|
1126 | * Returns MySQL column types (inherited and MySQL specified). |
||
1127 | * @return array |
||
1128 | */ |
||
1129 | public function getColumnTypes() |
||
1133 | } |
||
1134 |
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.