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 |
||
42 | abstract class PdoAdapter extends AbstractAdapter implements DirectActionInterface |
||
43 | { |
||
44 | /** |
||
45 | * @var \PDO|null |
||
46 | */ |
||
47 | protected $connection; |
||
48 | |||
49 | /** |
||
50 | * Writes a message to stdout if verbose output is on |
||
51 | 287 | * |
|
52 | * @param string $message The message to show |
||
53 | 287 | * |
|
54 | * @return void |
||
55 | 287 | */ |
|
56 | 3 | protected function verboseLog($message) |
|
66 | |||
67 | /** |
||
68 | 193 | * Create PDO connection |
|
69 | * |
||
70 | 193 | * @param string $dsn Connection string |
|
71 | * @param string|null $username Database username |
||
72 | * @param string|null $password Database password |
||
73 | 193 | * @param array $options Connection options |
|
74 | 191 | * @return \PDO |
|
75 | 191 | */ |
|
76 | 74 | protected function createPdoConnection($dsn, $username = null, $password = null, array $options = []) |
|
90 | |||
91 | /** |
||
92 | * @inheritDoc |
||
93 | 193 | */ |
|
94 | public function setOptions(array $options) |
||
104 | 189 | ||
105 | 189 | /** |
|
106 | 191 | * Sets the database connection. |
|
107 | * |
||
108 | * @param \PDO $connection Connection |
||
109 | * |
||
110 | * @return \Phinx\Db\Adapter\AdapterInterface |
||
111 | */ |
||
112 | 1 | public function setConnection(PDO $connection) |
|
139 | |||
140 | /** |
||
141 | * Gets the database connection |
||
142 | 220 | * |
|
143 | * @return \PDO |
||
144 | 220 | */ |
|
145 | public function getConnection() |
||
153 | 151 | ||
154 | /** |
||
155 | * {@inheritDoc} |
||
156 | * |
||
157 | * @return void |
||
158 | */ |
||
159 | 213 | public function connect() |
|
162 | 213 | ||
163 | 213 | /** |
|
164 | 208 | * {@inheritDoc} |
|
165 | 208 | * |
|
166 | 213 | * @return void |
|
167 | */ |
||
168 | public function disconnect() |
||
171 | |||
172 | 1 | /** |
|
173 | * @inheritDoc |
||
174 | 1 | */ |
|
175 | 1 | public function execute($sql) |
|
186 | |||
187 | /** |
||
188 | * Returns the Cake\Database connection object using the same underlying |
||
189 | * PDO object as this connection. |
||
190 | 11 | * |
|
191 | * @return \Cake\Database\Connection |
||
192 | 11 | */ |
|
193 | 11 | abstract public function getDecoratedConnection(); |
|
194 | 11 | ||
195 | 11 | /** |
|
196 | * @inheritDoc |
||
197 | 11 | */ |
|
198 | 11 | public function getQueryBuilder() |
|
202 | 11 | ||
203 | 11 | /** |
|
204 | 11 | * Executes a query and returns PDOStatement. |
|
205 | 11 | * |
|
206 | 11 | * @param string $sql SQL |
|
207 | * |
||
208 | 11 | * @return \PDOStatement |
|
209 | 11 | */ |
|
210 | public function query($sql) |
||
214 | |||
215 | 11 | /** |
|
216 | 11 | * @inheritDoc |
|
217 | 11 | */ |
|
218 | public function fetchRow($sql) |
||
224 | 5 | ||
225 | /** |
||
226 | 5 | * @inheritDoc |
|
227 | */ |
||
228 | public function fetchAll($sql) |
||
238 | 6 | ||
239 | 6 | /** |
|
240 | 2 | * {@inheritDoc} |
|
241 | 1 | * |
|
242 | 1 | * @return void |
|
243 | 1 | */ |
|
244 | 1 | public function insert(Table $table, $row) |
|
268 | 5 | ||
269 | 5 | /** |
|
270 | 5 | * Quotes a database value. |
|
271 | 5 | * |
|
272 | 5 | * @param mixed $value The value to quote |
|
273 | 5 | * |
|
274 | 5 | * @return mixed |
|
275 | 5 | */ |
|
276 | private function quoteValue($value) |
||
288 | |||
289 | /** |
||
290 | 5 | * Quotes a database string. |
|
291 | * |
||
292 | * @param string $value The string to quote |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | 1 | protected function quoteString($value) |
|
300 | 1 | ||
301 | 1 | /** |
|
302 | 1 | * @inheritDoc |
|
303 | 1 | */ |
|
304 | 1 | public function bulkinsert(Table $table, $rows) |
|
342 | |||
343 | /** |
||
344 | * @inheritDoc |
||
345 | */ |
||
346 | public function getVersions() |
||
352 | 208 | ||
353 | 208 | /** |
|
354 | 208 | * {@inheritDoc} |
|
355 | 208 | * |
|
356 | 208 | * @throws \RuntimeException |
|
357 | 208 | */ |
|
358 | 208 | public function getVersionLog() |
|
359 | 208 | { |
|
360 | 208 | $result = []; |
|
361 | 208 | ||
362 | 208 | switch ($this->options['version_order']) { |
|
363 | 208 | case Config::VERSION_ORDER_CREATION_TIME: |
|
364 | 208 | $orderBy = 'version ASC'; |
|
365 | 208 | break; |
|
366 | 208 | case Config::VERSION_ORDER_EXECUTION_TIME: |
|
367 | 208 | $orderBy = 'start_time ASC, version ASC'; |
|
368 | break; |
||
369 | 208 | default: |
|
370 | 208 | throw new RuntimeException('Invalid version_order configuration option'); |
|
371 | 208 | } |
|
372 | 208 | ||
373 | 208 | $rows = $this->fetchAll(sprintf('SELECT * FROM %s ORDER BY %s', $this->getSchemaTableName(), $orderBy)); |
|
374 | foreach ($rows as $version) { |
||
375 | $result[$version['version']] = $version; |
||
376 | } |
||
377 | |||
378 | return $result; |
||
379 | 121 | } |
|
380 | |||
381 | 121 | /** |
|
382 | * @inheritDoc |
||
383 | */ |
||
384 | public function migrated(MigrationInterface $migration, $direction, $startTime, $endTime) |
||
418 | |||
419 | /** |
||
420 | * @inheritDoc |
||
421 | */ |
||
422 | public function toggleBreakpoint(MigrationInterface $migration) |
||
423 | { |
||
424 | $this->query( |
||
425 | sprintf( |
||
426 | '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\';', |
||
427 | $this->getSchemaTableName(), |
||
428 | $this->quoteColumnName('breakpoint'), |
||
429 | $this->castToBool(true), |
||
430 | $this->castToBool(false), |
||
431 | $this->quoteColumnName('version'), |
||
432 | $migration->getVersion(), |
||
433 | $this->quoteColumnName('start_time') |
||
434 | ) |
||
435 | ); |
||
436 | |||
437 | return $this; |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * @inheritDoc |
||
442 | */ |
||
443 | public function resetAllBreakpoints() |
||
444 | { |
||
445 | return $this->execute( |
||
446 | sprintf( |
||
447 | 'UPDATE %1$s SET %2$s = %3$s, %4$s = %4$s WHERE %2$s <> %3$s;', |
||
448 | $this->getSchemaTableName(), |
||
449 | $this->quoteColumnName('breakpoint'), |
||
450 | $this->castToBool(false), |
||
451 | $this->quoteColumnName('start_time') |
||
452 | ) |
||
453 | ); |
||
454 | } |
||
455 | |||
456 | /** |
||
457 | * @inheritDoc |
||
458 | */ |
||
459 | public function setBreakpoint(MigrationInterface $migration) |
||
463 | |||
464 | /** |
||
465 | * @inheritDoc |
||
466 | */ |
||
467 | public function unsetBreakpoint(MigrationInterface $migration) |
||
471 | |||
472 | /** |
||
473 | * Mark a migration breakpoint. |
||
474 | * |
||
475 | * @param \Phinx\Migration\MigrationInterface $migration The migration target for the breakpoint |
||
476 | * @param bool $state The required state of the breakpoint |
||
477 | * |
||
478 | * @return \Phinx\Db\Adapter\AdapterInterface |
||
479 | */ |
||
480 | protected function markBreakpoint(MigrationInterface $migration, $state) |
||
496 | |||
497 | /** |
||
498 | * {@inheritDoc} |
||
499 | * |
||
500 | * @throws \BadMethodCallException |
||
501 | * |
||
502 | * @return void |
||
503 | */ |
||
504 | public function createSchema($schemaName = 'public') |
||
508 | |||
509 | /** |
||
510 | * {@inheritDoc} |
||
511 | * |
||
512 | * @throws \BadMethodCallException |
||
513 | * |
||
514 | * @return void |
||
515 | */ |
||
516 | public function dropSchema($name) |
||
520 | |||
521 | /** |
||
522 | * @inheritDoc |
||
523 | */ |
||
524 | public function getColumnTypes() |
||
553 | |||
554 | /** |
||
555 | * @inheritDoc |
||
556 | */ |
||
557 | public function castToBool($value) |
||
558 | { |
||
559 | return (bool)$value ? 1 : 0; |
||
560 | } |
||
561 | |||
562 | /** |
||
563 | * Retrieve a database connection attribute |
||
564 | * |
||
565 | * @see http://php.net/manual/en/pdo.getattribute.php |
||
566 | * |
||
567 | * @param int $attribute One of the PDO::ATTR_* constants |
||
568 | * |
||
569 | * @return mixed |
||
570 | */ |
||
571 | public function getAttribute($attribute) |
||
575 | |||
576 | /** |
||
577 | * Get the definition for a `DEFAULT` statement. |
||
578 | * |
||
579 | * @param mixed $default Default value |
||
580 | * @param string|null $columnType column type added |
||
581 | * |
||
582 | * @return string |
||
583 | */ |
||
584 | View Code Duplication | protected function getDefaultValueDefinition($default, $columnType = null) |
|
596 | |||
597 | /** |
||
598 | * Executes all the ALTER TABLE instructions passed for the given table |
||
599 | * |
||
600 | * @param string $tableName The table name to use in the ALTER statement |
||
601 | * @param \Phinx\Db\Util\AlterInstructions $instructions The object containing the alter sequence |
||
602 | * |
||
603 | * @return void |
||
604 | */ |
||
605 | protected function executeAlterSteps($tableName, AlterInstructions $instructions) |
||
610 | |||
611 | /** |
||
612 | * {@inheritDoc} |
||
613 | * |
||
614 | * @return void |
||
615 | */ |
||
616 | public function addColumn(Table $table, Column $column) |
||
621 | |||
622 | /** |
||
623 | * Returns the instructions to add the specified column to a database table. |
||
624 | * |
||
625 | * @param \Phinx\Db\Table\Table $table Table |
||
626 | * @param \Phinx\Db\Table\Column $column Column |
||
627 | * |
||
628 | * @return \Phinx\Db\Util\AlterInstructions |
||
629 | */ |
||
630 | abstract protected function getAddColumnInstructions(Table $table, Column $column); |
||
631 | |||
632 | /** |
||
633 | * {@inheritDoc} |
||
634 | * |
||
635 | * @return void |
||
636 | */ |
||
637 | public function renameColumn($tableName, $columnName, $newColumnName) |
||
642 | |||
643 | /** |
||
644 | * Returns the instructions to rename the specified column. |
||
645 | * |
||
646 | * @param string $tableName Table Name |
||
647 | * @param string $columnName Column Name |
||
648 | * @param string $newColumnName New Column Name |
||
649 | * |
||
650 | * @return AlterInstructions:w |
||
651 | */ |
||
652 | abstract protected function getRenameColumnInstructions($tableName, $columnName, $newColumnName); |
||
653 | |||
654 | /** |
||
655 | * {@inheritDoc} |
||
656 | * |
||
657 | * @return void |
||
658 | */ |
||
659 | public function changeColumn($tableName, $columnName, Column $newColumn) |
||
664 | |||
665 | /** |
||
666 | * Returns the instructions to change a table column type. |
||
667 | * |
||
668 | * @param string $tableName Table Name |
||
669 | * @param string $columnName Column Name |
||
670 | * @param \Phinx\Db\Table\Column $newColumn New Column |
||
671 | * |
||
672 | * @return \Phinx\Db\Util\AlterInstructions |
||
673 | */ |
||
674 | abstract protected function getChangeColumnInstructions($tableName, $columnName, Column $newColumn); |
||
675 | |||
676 | /** |
||
677 | * {@inheritDoc} |
||
678 | * |
||
679 | * @return void |
||
680 | */ |
||
681 | public function dropColumn($tableName, $columnName) |
||
686 | |||
687 | /** |
||
688 | * Returns the instructions to drop the specified column. |
||
689 | * |
||
690 | * @param string $tableName Table Name |
||
691 | * @param string $columnName Column Name |
||
692 | * |
||
693 | * @return \Phinx\Db\Util\AlterInstructions |
||
694 | */ |
||
695 | abstract protected function getDropColumnInstructions($tableName, $columnName); |
||
696 | |||
697 | /** |
||
698 | * {@inheritDoc} |
||
699 | * |
||
700 | * @return void |
||
701 | */ |
||
702 | public function addIndex(Table $table, Index $index) |
||
707 | |||
708 | /** |
||
709 | * Returns the instructions to add the specified index to a database table. |
||
710 | * |
||
711 | * @param \Phinx\Db\Table\Table $table Table |
||
712 | * @param \Phinx\Db\Table\Index $index Index |
||
713 | * |
||
714 | * @return \Phinx\Db\Util\AlterInstructions |
||
715 | */ |
||
716 | abstract protected function getAddIndexInstructions(Table $table, Index $index); |
||
717 | |||
718 | /** |
||
719 | * {@inheritDoc} |
||
720 | * |
||
721 | * @return void |
||
722 | */ |
||
723 | public function dropIndex($tableName, $columns) |
||
728 | |||
729 | /** |
||
730 | * Returns the instructions to drop the specified index from a database table. |
||
731 | * |
||
732 | * @param string $tableName The name of of the table where the index is |
||
733 | * @param mixed $columns Column(s) |
||
734 | * |
||
735 | * @return \Phinx\Db\Util\AlterInstructions |
||
736 | */ |
||
737 | abstract protected function getDropIndexByColumnsInstructions($tableName, $columns); |
||
738 | |||
739 | /** |
||
740 | * {@inheritDoc} |
||
741 | * |
||
742 | * @return void |
||
743 | */ |
||
744 | public function dropIndexByName($tableName, $indexName) |
||
749 | |||
750 | /** |
||
751 | * Returns the instructions to drop the index specified by name from a database table. |
||
752 | * |
||
753 | * @param string $tableName The table name whe the index is |
||
754 | * @param string $indexName The name of the index |
||
755 | * |
||
756 | * @return \Phinx\Db\Util\AlterInstructions |
||
757 | */ |
||
758 | abstract protected function getDropIndexByNameInstructions($tableName, $indexName); |
||
759 | |||
760 | /** |
||
761 | * {@inheritDoc} |
||
762 | * |
||
763 | * @return void |
||
764 | */ |
||
765 | public function addForeignKey(Table $table, ForeignKey $foreignKey) |
||
770 | |||
771 | /** |
||
772 | * Returns the instructions to adds the specified foreign key to a database table. |
||
773 | * |
||
774 | * @param \Phinx\Db\Table\Table $table The table to add the constraint to |
||
775 | * @param \Phinx\Db\Table\ForeignKey $foreignKey The foreign key to add |
||
776 | * |
||
777 | * @return \Phinx\Db\Util\AlterInstructions |
||
778 | */ |
||
779 | abstract protected function getAddForeignKeyInstructions(Table $table, ForeignKey $foreignKey); |
||
780 | |||
781 | /** |
||
782 | * {@inheritDoc} |
||
783 | * |
||
784 | * @return void |
||
785 | */ |
||
786 | public function dropForeignKey($tableName, $columns, $constraint = null) |
||
796 | |||
797 | /** |
||
798 | * Returns the instructions to drop the specified foreign key from a database table. |
||
799 | * |
||
800 | * @param string $tableName The table where the foreign key constraint is |
||
801 | * @param string $constraint Constraint name |
||
802 | * |
||
803 | * @return \Phinx\Db\Util\AlterInstructions |
||
804 | */ |
||
805 | abstract protected function getDropForeignKeyInstructions($tableName, $constraint); |
||
806 | |||
807 | /** |
||
808 | * Returns the instructions to drop the specified foreign key from a database table. |
||
809 | * |
||
810 | * @param string $tableName The table where the foreign key constraint is |
||
811 | * @param array $columns The list of column names |
||
812 | * |
||
813 | * @return \Phinx\Db\Util\AlterInstructions |
||
814 | */ |
||
815 | abstract protected function getDropForeignKeyByColumnsInstructions($tableName, $columns); |
||
816 | |||
817 | /** |
||
818 | * {@inheritDoc} |
||
819 | * |
||
820 | * @return void |
||
821 | */ |
||
822 | public function dropTable($tableName) |
||
827 | |||
828 | /** |
||
829 | * Returns the instructions to drop the specified database table. |
||
830 | * |
||
831 | * @param string $tableName Table Name |
||
832 | * |
||
833 | * @return \Phinx\Db\Util\AlterInstructions |
||
834 | */ |
||
835 | abstract protected function getDropTableInstructions($tableName); |
||
836 | |||
837 | /** |
||
838 | * {@inheritDoc} |
||
839 | * |
||
840 | * @return void |
||
841 | */ |
||
842 | public function renameTable($tableName, $newTableName) |
||
847 | |||
848 | /** |
||
849 | * Returns the instructions to rename the specified database table. |
||
850 | * |
||
851 | * @param string $tableName Table Name |
||
852 | * @param string $newTableName New Name |
||
853 | * |
||
854 | * @return \Phinx\Db\Util\AlterInstructions |
||
855 | */ |
||
856 | abstract protected function getRenameTableInstructions($tableName, $newTableName); |
||
857 | |||
858 | /** |
||
859 | * {@inheritDoc} |
||
860 | * |
||
861 | * @return void |
||
862 | */ |
||
863 | public function changePrimaryKey(Table $table, $newColumns) |
||
868 | |||
869 | /** |
||
870 | * Returns the instructions to change the primary key for the specified database table. |
||
871 | * |
||
872 | * @param \Phinx\Db\Table\Table $table Table |
||
873 | * @param string|array|null $newColumns Column name(s) to belong to the primary key, or null to drop the key |
||
874 | * |
||
875 | * @return \Phinx\Db\Util\AlterInstructions |
||
876 | */ |
||
877 | abstract protected function getChangePrimaryKeyInstructions(Table $table, $newColumns); |
||
878 | |||
879 | /** |
||
880 | * {@inheritDoc} |
||
881 | * |
||
882 | * @return void |
||
883 | */ |
||
884 | public function changeComment(Table $table, $newComment) |
||
889 | |||
890 | /** |
||
891 | * Returns the instruction to change the comment for the specified database table. |
||
892 | * |
||
893 | * @param \Phinx\Db\Table\Table $table Table |
||
894 | * @param string|null $newComment New comment string, or null to drop the comment |
||
895 | * |
||
896 | * @return \Phinx\Db\Util\AlterInstructions |
||
897 | */ |
||
898 | abstract protected function getChangeCommentInstructions(Table $table, $newComment); |
||
899 | |||
900 | /** |
||
901 | * {@inheritDoc} |
||
902 | * |
||
903 | * @throws \InvalidArgumentException |
||
904 | * |
||
905 | * @return void |
||
906 | */ |
||
907 | public function executeActions(Table $table, array $actions) |
||
1012 | } |
||
1013 |
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.