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 PdoAdapter 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 PdoAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
57 | 3 | abstract class PdoAdapter extends AbstractAdapter implements DirectActionInterface |
|
58 | { |
||
59 | 287 | /** |
|
60 | * @var \PDO|null |
||
61 | */ |
||
62 | protected $connection; |
||
63 | |||
64 | /** |
||
65 | * Writes a message to stdout if verbose output is on |
||
66 | * |
||
67 | * @param string $message The message to show |
||
68 | 193 | * @return void |
|
69 | */ |
||
70 | 193 | protected function verboseLog($message) |
|
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function setOptions(array $options) |
||
93 | 193 | ||
94 | /** |
||
95 | * Sets the database connection. |
||
96 | * |
||
97 | * @param \PDO $connection Connection |
||
98 | * @return \Phinx\Db\Adapter\AdapterInterface |
||
99 | */ |
||
100 | public function setConnection(\PDO $connection) |
||
127 | |||
128 | 218 | /** |
|
129 | 3 | * Gets the database connection |
|
130 | 3 | * |
|
131 | * @return \PDO |
||
132 | */ |
||
133 | 217 | public function getConnection() |
|
141 | |||
142 | 220 | /** |
|
143 | * {@inheritdoc} |
||
144 | 220 | */ |
|
145 | public function connect() |
||
148 | |||
149 | /** |
||
150 | 151 | * {@inheritdoc} |
|
151 | */ |
||
152 | 151 | public function disconnect() |
|
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | */ |
||
159 | 213 | public function execute($sql) |
|
169 | |||
170 | /** |
||
171 | * Returns the Cake\Database connection object using the same underlying |
||
172 | 1 | * PDO object as this connection. |
|
173 | * |
||
174 | 1 | * @return \Cake\Database\Connection |
|
175 | 1 | */ |
|
176 | 1 | abstract public function getDecoratedConnection(); |
|
177 | 1 | ||
178 | /** |
||
179 | 1 | * {@inheritdoc} |
|
180 | 1 | */ |
|
181 | 1 | public function getQueryBuilder() |
|
185 | 1 | ||
186 | /** |
||
187 | * Executes a query and returns PDOStatement. |
||
188 | * |
||
189 | * @param string $sql SQL |
||
190 | 11 | * @return \PDOStatement |
|
191 | */ |
||
192 | 11 | public function query($sql) |
|
196 | |||
197 | 11 | /** |
|
198 | 11 | * {@inheritdoc} |
|
199 | 11 | */ |
|
200 | public function fetchRow($sql) |
||
206 | 11 | ||
207 | /** |
||
208 | 11 | * {@inheritdoc} |
|
209 | 11 | */ |
|
210 | public function fetchAll($sql) |
||
220 | |||
221 | /** |
||
222 | 5 | * {@inheritdoc} |
|
223 | */ |
||
224 | 5 | public function insert(Table $table, $row) |
|
248 | 7 | ||
249 | 7 | /** |
|
250 | 7 | * Quotes a database value. |
|
251 | * |
||
252 | 7 | * @param mixed $value The value to quote |
|
253 | * @return mixed |
||
254 | */ |
||
255 | private function quoteValue($value) |
||
267 | 5 | ||
268 | 5 | /** |
|
269 | 5 | * Quotes a database string. |
|
270 | 5 | * |
|
271 | 5 | * @param string $value The string to quote |
|
272 | 5 | * @return string |
|
273 | 5 | */ |
|
274 | 5 | protected function quoteString($value) |
|
278 | 5 | ||
279 | /** |
||
280 | 3 | * {@inheritdoc} |
|
281 | 3 | */ |
|
282 | 3 | public function bulkinsert(Table $table, $rows) |
|
320 | 1 | ||
321 | 1 | /** |
|
322 | 1 | * {@inheritdoc} |
|
323 | 1 | */ |
|
324 | 1 | public function getVersions() |
|
330 | |||
331 | /** |
||
332 | * {@inheritdoc} |
||
333 | */ |
||
334 | public function getVersionLog() |
||
335 | { |
||
336 | $result = []; |
||
337 | |||
338 | switch ($this->options['version_order']) { |
||
339 | case \Phinx\Config\Config::VERSION_ORDER_CREATION_TIME: |
||
340 | $orderBy = 'version ASC'; |
||
341 | break; |
||
342 | case \Phinx\Config\Config::VERSION_ORDER_EXECUTION_TIME: |
||
343 | $orderBy = 'start_time ASC, version ASC'; |
||
344 | break; |
||
345 | default: |
||
346 | throw new \RuntimeException('Invalid version_order configuration option'); |
||
347 | } |
||
348 | |||
349 | 208 | $rows = $this->fetchAll(sprintf('SELECT * FROM %s ORDER BY %s', $this->getSchemaTableName(), $orderBy)); |
|
350 | foreach ($rows as $version) { |
||
351 | $result[$version['version']] = $version; |
||
352 | 208 | } |
|
353 | 208 | ||
354 | 208 | return $result; |
|
355 | 208 | } |
|
356 | 208 | ||
357 | 208 | /** |
|
358 | 208 | * {@inheritdoc} |
|
359 | 208 | */ |
|
360 | 208 | public function migrated(MigrationInterface $migration, $direction, $startTime, $endTime) |
|
394 | |||
395 | /** |
||
396 | * {@inheritdoc} |
||
397 | */ |
||
398 | public function toggleBreakpoint(MigrationInterface $migration) |
||
399 | { |
||
400 | $this->query( |
||
401 | sprintf( |
||
402 | 'UPDATE %1$s SET %2$s = CASE %2$s WHEN %3$s THEN %4$s ELSE %3$s END, %7$s = %7$s WHERE %5$s = \'%6$s\';', |
||
403 | $this->getSchemaTableName(), |
||
404 | $this->quoteColumnName('breakpoint'), |
||
405 | $this->castToBool(true), |
||
406 | $this->castToBool(false), |
||
407 | $this->quoteColumnName('version'), |
||
408 | $migration->getVersion(), |
||
409 | $this->quoteColumnName('start_time') |
||
410 | ) |
||
411 | ); |
||
412 | |||
413 | return $this; |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * {@inheritdoc} |
||
418 | */ |
||
419 | public function resetAllBreakpoints() |
||
420 | { |
||
421 | return $this->execute( |
||
422 | sprintf( |
||
423 | 'UPDATE %1$s SET %2$s = %3$s, %4$s = %4$s WHERE %2$s <> %3$s;', |
||
424 | $this->getSchemaTableName(), |
||
425 | $this->quoteColumnName('breakpoint'), |
||
426 | $this->castToBool(false), |
||
427 | $this->quoteColumnName('start_time') |
||
428 | ) |
||
429 | ); |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * {@inheritdoc} |
||
434 | */ |
||
435 | public function setBreakpoint(MigrationInterface $migration) |
||
439 | |||
440 | /** |
||
441 | * {@inheritdoc} |
||
442 | */ |
||
443 | public function unsetBreakpoint(MigrationInterface $migration) |
||
447 | |||
448 | /** |
||
449 | * Mark a migration breakpoint. |
||
450 | * |
||
451 | * @param \Phinx\Migration\MigrationInterface $migration The migration target for the breakpoint |
||
452 | * @param bool $state The required state of the breakpoint |
||
453 | * |
||
454 | * @return \Phinx\Db\Adapter\AdapterInterface |
||
455 | */ |
||
456 | protected function markBreakpoint(MigrationInterface $migration, $state) |
||
472 | |||
473 | /** |
||
474 | * {@inheritdoc} |
||
475 | */ |
||
476 | public function createSchema($schemaName = 'public') |
||
480 | |||
481 | /** |
||
482 | * {@inheritdoc} |
||
483 | */ |
||
484 | public function dropSchema($name) |
||
488 | |||
489 | /** |
||
490 | * {@inheritdoc} |
||
491 | */ |
||
492 | public function getColumnTypes() |
||
521 | |||
522 | /** |
||
523 | * {@inheritdoc} |
||
524 | */ |
||
525 | public function castToBool($value) |
||
526 | { |
||
527 | return (bool)$value ? 1 : 0; |
||
528 | } |
||
529 | |||
530 | /** |
||
531 | * Retrieve a database connection attribute |
||
532 | * @see http://php.net/manual/en/pdo.getattribute.php |
||
533 | * |
||
534 | * @param int $attribute One of the PDO::ATTR_* constants |
||
535 | * @return mixed |
||
536 | */ |
||
537 | public function getAttribute($attribute) |
||
541 | |||
542 | /** |
||
543 | * Get the definition for a `DEFAULT` statement. |
||
544 | * |
||
545 | * @param mixed $default Default value |
||
546 | * @param string $columnType column type added |
||
547 | * @return string |
||
548 | */ |
||
549 | View Code Duplication | protected function getDefaultValueDefinition($default, $columnType = null) |
|
561 | |||
562 | /** |
||
563 | * Executes all the ALTER TABLE instructions passed for the given table |
||
564 | * |
||
565 | * @param string $tableName The table name to use in the ALTER statement |
||
566 | * @param AlterInstructions $instructions The object containing the alter sequence |
||
567 | * @return void |
||
568 | */ |
||
569 | protected function executeAlterSteps($tableName, AlterInstructions $instructions) |
||
574 | |||
575 | /** |
||
576 | * {@inheritdoc} |
||
577 | */ |
||
578 | public function addColumn(Table $table, Column $column) |
||
583 | |||
584 | /** |
||
585 | * Returns the instructions to add the specified column to a database table. |
||
586 | * |
||
587 | * @param \Phinx\Db\Table\Table $table Table |
||
588 | * @param \Phinx\Db\Table\Column $column Column |
||
589 | * @return AlterInstructions |
||
590 | */ |
||
591 | abstract protected function getAddColumnInstructions(Table $table, Column $column); |
||
592 | |||
593 | /** |
||
594 | * {@inheritdoc} |
||
595 | */ |
||
596 | public function renameColumn($tableName, $columnName, $newColumnName) |
||
601 | |||
602 | /** |
||
603 | * Returns the instructions to rename the specified column. |
||
604 | * |
||
605 | * @param string $tableName Table Name |
||
606 | * @param string $columnName Column Name |
||
607 | * @param string $newColumnName New Column Name |
||
608 | * @return AlterInstructions:w |
||
609 | * |
||
610 | */ |
||
611 | abstract protected function getRenameColumnInstructions($tableName, $columnName, $newColumnName); |
||
612 | |||
613 | /** |
||
614 | * {@inheritdoc} |
||
615 | */ |
||
616 | public function changeColumn($tableName, $columnName, Column $newColumn) |
||
621 | |||
622 | /** |
||
623 | * Returns the instructions to change a table column type. |
||
624 | * |
||
625 | * @param string $tableName Table Name |
||
626 | * @param string $columnName Column Name |
||
627 | * @param \Phinx\Db\Table\Column $newColumn New Column |
||
628 | * @return AlterInstructions |
||
629 | */ |
||
630 | abstract protected function getChangeColumnInstructions($tableName, $columnName, Column $newColumn); |
||
631 | |||
632 | /** |
||
633 | * {@inheritdoc} |
||
634 | */ |
||
635 | public function dropColumn($tableName, $columnName) |
||
640 | |||
641 | /** |
||
642 | * Returns the instructions to drop the specified column. |
||
643 | * |
||
644 | * @param string $tableName Table Name |
||
645 | * @param string $columnName Column Name |
||
646 | * @return AlterInstructions |
||
647 | */ |
||
648 | abstract protected function getDropColumnInstructions($tableName, $columnName); |
||
649 | |||
650 | /** |
||
651 | * {@inheritdoc} |
||
652 | */ |
||
653 | public function addIndex(Table $table, Index $index) |
||
658 | |||
659 | /** |
||
660 | * Returns the instructions to add the specified index to a database table. |
||
661 | * |
||
662 | * @param \Phinx\Db\Table\Table $table Table |
||
663 | * @param \Phinx\Db\Table\Index $index Index |
||
664 | * @return AlterInstructions |
||
665 | */ |
||
666 | abstract protected function getAddIndexInstructions(Table $table, Index $index); |
||
667 | |||
668 | /** |
||
669 | * {@inheritdoc} |
||
670 | */ |
||
671 | public function dropIndex($tableName, $columns) |
||
676 | |||
677 | /** |
||
678 | * Returns the instructions to drop the specified index from a database table. |
||
679 | * |
||
680 | * @param string $tableName The name of of the table where the index is |
||
681 | * @param mixed $columns Column(s) |
||
682 | * @return AlterInstructions |
||
683 | */ |
||
684 | abstract protected function getDropIndexByColumnsInstructions($tableName, $columns); |
||
685 | |||
686 | /** |
||
687 | * {@inheritdoc} |
||
688 | */ |
||
689 | public function dropIndexByName($tableName, $indexName) |
||
694 | |||
695 | /** |
||
696 | * Returns the instructions to drop the index specified by name from a database table. |
||
697 | * |
||
698 | * @param string $tableName The table name whe the index is |
||
699 | * @param string $indexName The name of the index |
||
700 | * @return AlterInstructions |
||
701 | */ |
||
702 | abstract protected function getDropIndexByNameInstructions($tableName, $indexName); |
||
703 | |||
704 | /** |
||
705 | * {@inheritdoc} |
||
706 | */ |
||
707 | public function addForeignKey(Table $table, ForeignKey $foreignKey) |
||
712 | |||
713 | /** |
||
714 | * Returns the instructions to adds the specified foreign key to a database table. |
||
715 | * |
||
716 | * @param \Phinx\Db\Table\Table $table The table to add the constraint to |
||
717 | * @param \Phinx\Db\Table\ForeignKey $foreignKey The foreign key to add |
||
718 | * @return AlterInstructions |
||
719 | */ |
||
720 | abstract protected function getAddForeignKeyInstructions(Table $table, ForeignKey $foreignKey); |
||
721 | |||
722 | /** |
||
723 | * {@inheritdoc} |
||
724 | */ |
||
725 | public function dropForeignKey($tableName, $columns, $constraint = null) |
||
735 | |||
736 | /** |
||
737 | * Returns the instructions to drop the specified foreign key from a database table. |
||
738 | * |
||
739 | * @param string $tableName The table where the foreign key constraint is |
||
740 | * @param string $constraint Constraint name |
||
741 | * @return AlterInstructions |
||
742 | */ |
||
743 | abstract protected function getDropForeignKeyInstructions($tableName, $constraint); |
||
744 | |||
745 | /** |
||
746 | * Returns the instructions to drop the specified foreign key from a database table. |
||
747 | * |
||
748 | * @param string $tableName The table where the foreign key constraint is |
||
749 | * @param array $columns The list of column names |
||
750 | * @return AlterInstructions |
||
751 | */ |
||
752 | abstract protected function getDropForeignKeyByColumnsInstructions($tableName, $columns); |
||
753 | |||
754 | /** |
||
755 | * {@inheritdoc} |
||
756 | */ |
||
757 | public function dropTable($tableName) |
||
762 | |||
763 | /** |
||
764 | * Returns the instructions to drop the specified database table. |
||
765 | * |
||
766 | * @param string $tableName Table Name |
||
767 | * @return AlterInstructions |
||
768 | */ |
||
769 | abstract protected function getDropTableInstructions($tableName); |
||
770 | |||
771 | /** |
||
772 | * {@inheritdoc} |
||
773 | */ |
||
774 | public function renameTable($tableName, $newTableName) |
||
779 | |||
780 | /** |
||
781 | * Returns the instructions to rename the specified database table. |
||
782 | * |
||
783 | * @param string $tableName Table Name |
||
784 | * @param string $newTableName New Name |
||
785 | * @return AlterInstructions |
||
786 | */ |
||
787 | abstract protected function getRenameTableInstructions($tableName, $newTableName); |
||
788 | |||
789 | /** |
||
790 | * {@inheritdoc} |
||
791 | */ |
||
792 | public function changePrimaryKey(Table $table, $newColumns) |
||
797 | |||
798 | /** |
||
799 | * Returns the instructions to change the primary key for the specified database table. |
||
800 | * |
||
801 | * @param Table $table Table |
||
802 | * @param string|array|null $newColumns Column name(s) to belong to the primary key, or null to drop the key |
||
803 | * @return AlterInstructions |
||
804 | */ |
||
805 | abstract protected function getChangePrimaryKeyInstructions(Table $table, $newColumns); |
||
806 | |||
807 | /** |
||
808 | * {@inheritdoc} |
||
809 | */ |
||
810 | public function changeComment(Table $table, $newComment) |
||
815 | |||
816 | /** |
||
817 | * Returns the instruction to change the comment for the specified database table. |
||
818 | * |
||
819 | * @param Table $table Table |
||
820 | * @param string|null $newComment New comment string, or null to drop the comment |
||
821 | * @return AlterInstructions |
||
822 | */ |
||
823 | abstract protected function getChangeCommentInstructions(Table $table, $newComment); |
||
824 | |||
825 | /** |
||
826 | * {@inheritdoc} |
||
827 | */ |
||
828 | public function executeActions(Table $table, array $actions) |
||
933 | } |
||
934 |
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.