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 PostgresAdapter 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 PostgresAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class PostgresAdapter extends PdoAdapter implements AdapterInterface |
||
| 37 | { |
||
| 38 | const INT_SMALL = 65535; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Columns with comments |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $columnsWithComments = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * {@inheritdoc} |
||
| 49 | */ |
||
| 50 | 68 | public function connect() |
|
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | */ |
||
| 85 | 68 | public function disconnect() |
|
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritdoc} |
||
| 92 | */ |
||
| 93 | public function hasTransactions() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | */ |
||
| 101 | public function beginTransaction() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | */ |
||
| 109 | public function commitTransaction() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritdoc} |
||
| 116 | */ |
||
| 117 | public function rollbackTransaction() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Quotes a schema name for use in a query. |
||
| 124 | * |
||
| 125 | * @param string $schemaName Schema Name |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | 68 | public function quoteSchemaName($schemaName) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | */ |
||
| 136 | 68 | public function quoteTableName($tableName) |
|
| 142 | |||
| 143 | /** |
||
| 144 | 68 | * {@inheritdoc} |
|
| 145 | */ |
||
| 146 | 68 | public function quoteColumnName($columnName) |
|
| 150 | |||
| 151 | /** |
||
| 152 | 68 | * {@inheritdoc} |
|
| 153 | */ |
||
| 154 | 68 | public function hasTable($tableName) |
|
| 170 | |||
| 171 | 68 | /** |
|
| 172 | * {@inheritdoc} |
||
| 173 | 68 | */ |
|
| 174 | public function createTable(Table $table) |
||
| 175 | { |
||
| 176 | 68 | $options = $table->getOptions(); |
|
| 177 | 68 | $parts = $this->getSchemaName($table->getName()); |
|
| 178 | 48 | ||
| 179 | 48 | // Add the default primary key |
|
| 180 | 48 | $columns = $table->getPendingColumns(); |
|
| 181 | 48 | View Code Duplication | if (!isset($options['id']) || (isset($options['id']) && $options['id'] === true)) { |
| 182 | $column = new Column(); |
||
| 183 | 48 | $column->setName('id') |
|
| 184 | 48 | ->setType('integer') |
|
| 185 | 68 | ->setIdentity(true); |
|
| 186 | |||
| 187 | 2 | array_unshift($columns, $column); |
|
| 188 | 2 | $options['primary_key'] = 'id'; |
|
| 189 | 2 | } elseif (isset($options['id']) && is_string($options['id'])) { |
|
| 190 | 2 | // Handle id => "field_name" to support AUTO_INCREMENT |
|
| 191 | $column = new Column(); |
||
| 192 | 2 | $column->setName($options['id']) |
|
| 193 | 2 | ->setType('integer') |
|
| 194 | 2 | ->setIdentity(true); |
|
| 195 | |||
| 196 | array_unshift($columns, $column); |
||
| 197 | 68 | $options['primary_key'] = $options['id']; |
|
| 198 | 68 | } |
|
| 199 | |||
| 200 | 68 | // TODO - process table options like collation etc |
|
| 201 | 68 | $sql = 'CREATE TABLE '; |
|
| 202 | 68 | $sql .= $this->quoteTableName($table->getName()) . ' ('; |
|
| 203 | |||
| 204 | $this->columnsWithComments = []; |
||
| 205 | 68 | View Code Duplication | foreach ($columns as $column) { |
| 206 | 6 | $sql .= $this->quoteColumnName($column->getName()) . ' ' . $this->getColumnSqlDefinition($column) . ', '; |
|
| 207 | 6 | ||
| 208 | 68 | // set column comments, if needed |
|
| 209 | if ($column->getComment()) { |
||
| 210 | $this->columnsWithComments[] = $column; |
||
| 211 | 68 | } |
|
| 212 | 68 | } |
|
| 213 | 68 | ||
| 214 | 68 | // set the primary key(s) |
|
| 215 | 68 | if (isset($options['primary_key'])) { |
|
| 216 | 68 | $sql = rtrim($sql); |
|
| 217 | $sql .= sprintf(' CONSTRAINT %s PRIMARY KEY (', $this->quoteColumnName($parts['table'] . '_pkey')); |
||
| 218 | if (is_string($options['primary_key'])) { // handle primary_key => 'id' |
||
| 219 | 1 | $sql .= $this->quoteColumnName($options['primary_key']); |
|
| 220 | 1 | } elseif (is_array($options['primary_key'])) { // handle primary_key => array('tag_id', 'resource_id') |
|
| 221 | 1 | // PHP 5.4 will allow access of $this, so we can call quoteColumnName() directly in the anonymous function, |
|
| 222 | 1 | // but for now just hard-code the adapter quotes |
|
| 223 | 1 | $sql .= implode( |
|
| 224 | 1 | ',', |
|
| 225 | 1 | array_map( |
|
| 226 | 1 | function ($v) { |
|
| 227 | 1 | return '"' . $v . '"'; |
|
| 228 | 1 | }, |
|
| 229 | 68 | $options['primary_key'] |
|
| 230 | 68 | ) |
|
| 231 | 2 | ); |
|
| 232 | } |
||
| 233 | $sql .= ')'; |
||
| 234 | } else { |
||
| 235 | 68 | $sql = substr(rtrim($sql), 0, -1); // no primary keys |
|
| 236 | 68 | } |
|
| 237 | 1 | ||
| 238 | 1 | // set the foreign keys |
|
| 239 | 1 | $foreignKeys = $table->getForeignKeys(); |
|
| 240 | 1 | if (!empty($foreignKeys)) { |
|
| 241 | foreach ($foreignKeys as $foreignKey) { |
||
| 242 | 68 | $sql .= ', ' . $this->getForeignKeySqlDefinition($foreignKey, $table->getName()); |
|
| 243 | } |
||
| 244 | } |
||
| 245 | 68 | ||
| 246 | 6 | $sql .= ');'; |
|
| 247 | 6 | ||
| 248 | 6 | // process column comments |
|
| 249 | 6 | if (!empty($this->columnsWithComments)) { |
|
| 250 | foreach ($this->columnsWithComments as $column) { |
||
| 251 | $sql .= $this->getColumnCommentSqlDefinition($column, $table->getName()); |
||
| 252 | } |
||
| 253 | 68 | } |
|
| 254 | 68 | ||
| 255 | 5 | // set the indexes |
|
| 256 | 5 | $indexes = $table->getIndexes(); |
|
| 257 | 5 | if (!empty($indexes)) { |
|
| 258 | 5 | foreach ($indexes as $index) { |
|
| 259 | $sql .= $this->getIndexSqlDefinition($index, $table->getName()); |
||
| 260 | } |
||
| 261 | 68 | } |
|
| 262 | |||
| 263 | // execute the sql |
||
| 264 | 68 | $this->execute($sql); |
|
| 265 | 1 | ||
| 266 | 1 | // process table comments |
|
| 267 | 1 | if (isset($options['comment'])) { |
|
| 268 | 1 | $sql = sprintf( |
|
| 269 | 1 | 'COMMENT ON TABLE %s IS %s', |
|
| 270 | 1 | $this->quoteTableName($table->getName()), |
|
| 271 | 1 | $this->getConnection()->quote($options['comment']) |
|
| 272 | 68 | ); |
|
| 273 | $this->execute($sql); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | 1 | /** |
|
| 278 | * {@inheritdoc} |
||
| 279 | 1 | */ |
|
| 280 | 1 | public function renameTable($tableName, $newTableName) |
|
| 281 | 1 | { |
|
| 282 | 1 | $sql = sprintf( |
|
| 283 | 1 | 'ALTER TABLE %s RENAME TO %s', |
|
| 284 | 1 | $this->quoteTableName($tableName), |
|
| 285 | 1 | $this->quoteColumnName($newTableName) |
|
| 286 | ); |
||
| 287 | $this->execute($sql); |
||
| 288 | } |
||
| 289 | |||
| 290 | 1 | /** |
|
| 291 | * {@inheritdoc} |
||
| 292 | 1 | */ |
|
| 293 | 1 | public function dropTable($tableName) |
|
| 294 | { |
||
| 295 | $this->execute(sprintf('DROP TABLE %s', $this->quoteTableName($tableName))); |
||
| 296 | } |
||
| 297 | |||
| 298 | 1 | /** |
|
| 299 | * {@inheritdoc} |
||
| 300 | 1 | */ |
|
| 301 | 1 | public function truncateTable($tableName) |
|
| 302 | 1 | { |
|
| 303 | 1 | $sql = sprintf( |
|
| 304 | 'TRUNCATE TABLE %s', |
||
| 305 | 1 | $this->quoteTableName($tableName) |
|
| 306 | 1 | ); |
|
| 307 | |||
| 308 | $this->execute($sql); |
||
| 309 | } |
||
| 310 | |||
| 311 | 9 | /** |
|
| 312 | * {@inheritdoc} |
||
| 313 | 9 | */ |
|
| 314 | 9 | public function getColumns($tableName) |
|
| 315 | { |
||
| 316 | $parts = $this->getSchemaName($tableName); |
||
| 317 | $columns = []; |
||
| 318 | 9 | $sql = sprintf( |
|
| 319 | "SELECT column_name, data_type, is_identity, is_nullable, |
||
| 320 | 9 | column_default, character_maximum_length, numeric_precision, numeric_scale |
|
| 321 | 9 | FROM information_schema.columns |
|
| 322 | WHERE table_schema = %s AND table_name = %s", |
||
| 323 | 9 | $this->getConnection()->quote($parts['schema']), |
|
| 324 | 9 | $this->getConnection()->quote($parts['table']) |
|
| 325 | 9 | ); |
|
| 326 | 9 | $columnsInfo = $this->fetchAll($sql); |
|
| 327 | 9 | ||
| 328 | 9 | foreach ($columnsInfo as $columnInfo) { |
|
| 329 | 9 | $column = new Column(); |
|
| 330 | 9 | $column->setName($columnInfo['column_name']) |
|
| 331 | 9 | ->setType($this->getPhinxType($columnInfo['data_type'])) |
|
| 332 | ->setNull($columnInfo['is_nullable'] === 'YES') |
||
| 333 | 9 | ->setDefault($columnInfo['column_default']) |
|
| 334 | 1 | ->setIdentity($columnInfo['is_identity'] === 'YES') |
|
| 335 | 1 | ->setPrecision($columnInfo['numeric_precision']) |
|
| 336 | ->setScale($columnInfo['numeric_scale']); |
||
| 337 | 9 | ||
| 338 | 5 | if (preg_match('/\bwith time zone$/', $columnInfo['data_type'])) { |
|
| 339 | 5 | $column->setTimezone(true); |
|
| 340 | 9 | } |
|
| 341 | 9 | ||
| 342 | 9 | if (isset($columnInfo['character_maximum_length'])) { |
|
| 343 | $column->setLimit($columnInfo['character_maximum_length']); |
||
| 344 | } |
||
| 345 | $columns[] = $column; |
||
| 346 | } |
||
| 347 | |||
| 348 | 24 | return $columns; |
|
| 349 | } |
||
| 350 | 24 | ||
| 351 | /** |
||
| 352 | * {@inheritdoc} |
||
| 353 | 24 | */ |
|
| 354 | 24 | public function hasColumn($tableName, $columnName) |
|
| 370 | 18 | ||
| 371 | 18 | /** |
|
| 372 | 18 | * {@inheritdoc} |
|
| 373 | 18 | */ |
|
| 374 | View Code Duplication | public function addColumn(Table $table, Column $column) |
|
| 385 | |||
| 386 | 3 | /** |
|
| 387 | 3 | * {@inheritdoc} |
|
| 388 | */ |
||
| 389 | 3 | public function renameColumn($tableName, $columnName, $newColumnName) |
|
| 413 | 5 | ||
| 414 | 5 | /** |
|
| 415 | 5 | * {@inheritdoc} |
|
| 416 | 5 | */ |
|
| 417 | public function changeColumn($tableName, $columnName, Column $newColumn) |
||
| 483 | 1 | ||
| 484 | 1 | /** |
|
| 485 | 1 | * {@inheritdoc} |
|
| 486 | 1 | */ |
|
| 487 | public function dropColumn($tableName, $columnName) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Get an array of indexes from a particular table. |
||
| 500 | * |
||
| 501 | * @param string $tableName Table Name |
||
| 502 | * @return array |
||
| 503 | */ |
||
| 504 | protected function getIndexes($tableName) |
||
| 544 | |||
| 545 | /** |
||
| 546 | 1 | * {@inheritdoc} |
|
| 547 | */ |
||
| 548 | 1 | View Code Duplication | public function hasIndex($tableName, $columns) |
| 562 | 2 | ||
| 563 | 2 | /** |
|
| 564 | 2 | * {@inheritdoc} |
|
| 565 | */ |
||
| 566 | View Code Duplication | public function hasIndexByName($tableName, $indexName) |
|
| 577 | |||
| 578 | 1 | /** |
|
| 579 | 1 | * {@inheritdoc} |
|
| 580 | 1 | */ |
|
| 581 | 1 | public function addIndex(Table $table, Index $index) |
|
| 586 | 1 | ||
| 587 | /** |
||
| 588 | 1 | * {@inheritdoc} |
|
| 589 | */ |
||
| 590 | public function dropIndex($tableName, $columns) |
||
| 613 | 3 | ||
| 614 | 3 | /** |
|
| 615 | * {@inheritdoc} |
||
| 616 | */ |
||
| 617 | public function dropIndexByName($tableName, $indexName) |
||
| 627 | |||
| 628 | /** |
||
| 629 | * {@inheritdoc} |
||
| 630 | */ |
||
| 631 | View Code Duplication | public function hasForeignKey($tableName, $columns, $constraint = null) |
|
| 654 | 3 | ||
| 655 | 3 | /** |
|
| 656 | 3 | * Get an array of foreign keys from a particular table. |
|
| 657 | 3 | * |
|
| 658 | 3 | * @param string $tableName Table Name |
|
| 659 | 3 | * @return array |
|
| 660 | */ |
||
| 661 | protected function getForeignKeys($tableName) |
||
| 689 | |||
| 690 | 1 | /** |
|
| 691 | 1 | * {@inheritdoc} |
|
| 692 | 1 | */ |
|
| 693 | 1 | View Code Duplication | public function addForeignKey(Table $table, ForeignKey $foreignKey) |
| 702 | 1 | ||
| 703 | /** |
||
| 704 | 1 | * {@inheritdoc} |
|
| 705 | */ |
||
| 706 | 1 | public function dropForeignKey($tableName, $columns, $constraint = null) |
|
| 743 | |||
| 744 | 68 | /** |
|
| 745 | 68 | * {@inheritdoc} |
|
| 746 | 68 | */ |
|
| 747 | 1 | public function getSqlType($type, $limit = null) |
|
| 810 | 5 | ||
| 811 | 3 | /** |
|
| 812 | 4 | * Returns Phinx type by SQL type |
|
| 813 | 4 | * |
|
| 814 | 2 | * @param string $sqlType SQL type |
|
| 815 | 4 | * @returns string Phinx type |
|
| 816 | 4 | */ |
|
| 817 | 2 | public function getPhinxType($sqlType) |
|
| 880 | |||
| 881 | /** |
||
| 882 | * {@inheritdoc} |
||
| 883 | */ |
||
| 884 | 68 | public function createDatabase($name, $options = []) |
|
| 889 | 68 | ||
| 890 | 68 | /** |
|
| 891 | 68 | * {@inheritdoc} |
|
| 892 | */ |
||
| 893 | public function hasDatabase($name) |
||
| 900 | 68 | ||
| 901 | /** |
||
| 902 | 68 | * {@inheritdoc} |
|
| 903 | 68 | */ |
|
| 904 | 50 | public function dropDatabase($name) |
|
| 910 | 1 | ||
| 911 | 1 | /** |
|
| 912 | 1 | * Get the defintion for a `DEFAULT` statement. |
|
| 913 | 1 | * |
|
| 914 | 1 | * @param mixed $default |
|
| 915 | 68 | * @return string |
|
| 916 | */ |
||
| 917 | View Code Duplication | protected function getDefaultValueDefinition($default) |
|
| 927 | |||
| 928 | /** |
||
| 929 | 68 | * Gets the PostgreSQL Column Definition for a Column object. |
|
| 930 | 68 | * |
|
| 931 | 68 | * @param \Phinx\Db\Table\Column $column Column |
|
| 932 | 68 | * @return string |
|
| 933 | 1 | */ |
|
| 934 | 1 | protected function getColumnSqlDefinition(Column $column) |
|
| 979 | 3 | ||
| 980 | 5 | /** |
|
| 981 | 5 | * Gets the PostgreSQL Column Comment Definition for a column object. |
|
| 982 | * |
||
| 983 | * @param \Phinx\Db\Table\Column $column Column |
||
| 984 | 5 | * @param string $tableName Table name |
|
| 985 | * @return string |
||
| 986 | 7 | */ |
|
| 987 | 7 | protected function getColumnCommentSqlDefinition(Column $column, $tableName) |
|
| 1001 | |||
| 1002 | /** |
||
| 1003 | 3 | * Gets the PostgreSQL Index Definition for an Index object. |
|
| 1004 | * |
||
| 1005 | 3 | * @param \Phinx\Db\Table\Index $index Index |
|
| 1006 | 3 | * @param string $tableName Table name |
|
| 1007 | 3 | * @return string |
|
| 1008 | 3 | */ |
|
| 1009 | protected function getIndexSqlDefinition(Index $index, $tableName) |
||
| 1032 | |||
| 1033 | /** |
||
| 1034 | * Gets the MySQL Foreign Key Definition for an ForeignKey object. |
||
| 1035 | * |
||
| 1036 | * @param \Phinx\Db\Table\ForeignKey $foreignKey |
||
| 1037 | * @param string $tableName Table name |
||
| 1038 | 68 | * @return string |
|
| 1039 | */ |
||
| 1040 | 68 | protected function getForeignKeySqlDefinition(ForeignKey $foreignKey, $tableName) |
|
| 1063 | |||
| 1064 | /** |
||
| 1065 | * {@inheritdoc} |
||
| 1066 | */ |
||
| 1067 | public function createSchemaTable() |
||
| 1078 | |||
| 1079 | 68 | /** |
|
| 1080 | * Creates the specified schema. |
||
| 1081 | 68 | * |
|
| 1082 | 68 | * @param string $schemaName Schema Name |
|
| 1083 | 68 | * @return void |
|
| 1084 | 68 | */ |
|
| 1085 | public function createSchema($schemaName = 'public') |
||
| 1090 | |||
| 1091 | 68 | /** |
|
| 1092 | * Checks to see if a schema exists. |
||
| 1093 | * |
||
| 1094 | * @param string $schemaName Schema Name |
||
| 1095 | 68 | * @return bool |
|
| 1096 | 68 | */ |
|
| 1097 | 68 | public function hasSchema($schemaName) |
|
| 1109 | 73 | ||
| 1110 | /** |
||
| 1111 | * Drops the specified schema table. |
||
| 1112 | * |
||
| 1113 | * @param string $schemaName Schema name |
||
| 1114 | * @return void |
||
| 1115 | 73 | */ |
|
| 1116 | public function dropSchema($schemaName) |
||
| 1121 | |||
| 1122 | /** |
||
| 1123 | * Drops all schemas. |
||
| 1124 | * |
||
| 1125 | * @return void |
||
| 1126 | */ |
||
| 1127 | 14 | public function dropAllSchemas() |
|
| 1133 | 13 | ||
| 1134 | 13 | /** |
|
| 1135 | * Returns schemas. |
||
| 1136 | * |
||
| 1137 | * @return array |
||
| 1138 | */ |
||
| 1139 | public function getAllSchemas() |
||
| 1152 | |||
| 1153 | 68 | /** |
|
| 1154 | * {@inheritdoc} |
||
| 1155 | */ |
||
| 1156 | public function getColumnTypes() |
||
| 1160 | |||
| 1161 | /** |
||
| 1162 | * {@inheritdoc} |
||
| 1163 | */ |
||
| 1164 | public function isValidColumnType(Column $column) |
||
| 1169 | |||
| 1170 | /** |
||
| 1171 | * Check if the given column is an array of a valid type. |
||
| 1172 | * |
||
| 1173 | * @param string $columnType |
||
| 1174 | * @return bool |
||
| 1175 | */ |
||
| 1176 | protected function isArrayType($columnType) |
||
| 1186 | |||
| 1187 | /** |
||
| 1188 | * @param string $tableName |
||
| 1189 | * @return array |
||
| 1190 | */ |
||
| 1191 | private function getSchemaName($tableName) |
||
| 1204 | |||
| 1205 | /** |
||
| 1206 | * Gets the schema name. |
||
| 1207 | * |
||
| 1208 | * @return string |
||
| 1209 | */ |
||
| 1210 | private function getGlobalSchemaName() |
||
| 1216 | |||
| 1217 | /** |
||
| 1218 | * {@inheritdoc} |
||
| 1219 | */ |
||
| 1220 | public function castToBool($value) |
||
| 1221 | { |
||
| 1222 | return (bool)$value ? 'TRUE' : 'FALSE'; |
||
| 1223 | } |
||
| 1224 | } |
||
| 1225 |
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.