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 OracleAdapter 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 OracleAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class OracleAdapter extends PdoAdapter implements AdapterInterface |
||
| 43 | { |
||
| 44 | protected $schema = 'dbo'; |
||
| 45 | |||
| 46 | protected $signedColumnTypes = ['integer' => true, 'biginteger' => true, 'float' => true, 'decimal' => true]; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * {@inheritdoc} |
||
| 50 | */ |
||
| 51 | public function connect() |
||
| 52 | { |
||
| 53 | if ($this->connection === null) { |
||
| 54 | if (!extension_loaded('pdo_oci')) { |
||
| 55 | // @codeCoverageIgnoreStart |
||
| 56 | throw new \RuntimeException('You need to enable the PDO_OCI extension for Phinx to run properly.'); |
||
| 57 | // @codeCoverageIgnoreEnd |
||
| 58 | } |
||
| 59 | |||
| 60 | $options = $this->getOptions(); |
||
| 61 | |||
| 62 | // if port is specified use it, otherwise use the Oracle default |
||
| 63 | View Code Duplication | if (empty($options['port'])) { |
|
|
|
|||
| 64 | $dsn = "oci:dbname=//".$options['host']."/".$options['sid'].""; |
||
| 65 | } else { |
||
| 66 | $dsn = "oci:dbname=//".$options['host'].":".$options['port']."/".$options['sid'].""; |
||
| 67 | } |
||
| 68 | |||
| 69 | $driverOptions = [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]; |
||
| 70 | |||
| 71 | try { |
||
| 72 | $db = new \PDO($dsn, $options['user'], $options['pass'], $driverOptions); |
||
| 73 | } catch (\PDOException $exception) { |
||
| 74 | throw new \InvalidArgumentException(sprintf( |
||
| 75 | 'There was a problem connecting to the database: %s', |
||
| 76 | $exception->getMessage() |
||
| 77 | )); |
||
| 78 | } |
||
| 79 | $this->setConnection($db); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | public function disconnect() |
||
| 87 | { |
||
| 88 | $this->connection = null; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * {@inheritdoc} |
||
| 93 | */ |
||
| 94 | public function hasTransactions() |
||
| 95 | { |
||
| 96 | return true; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * {@inheritdoc} |
||
| 101 | */ |
||
| 102 | public function beginTransaction() |
||
| 103 | { |
||
| 104 | // $this->execute('BEGIN TRANSACTION'); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritdoc} |
||
| 109 | */ |
||
| 110 | public function commitTransaction() |
||
| 111 | { |
||
| 112 | // $this->execute('COMMIT TRANSACTION'); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * {@inheritdoc} |
||
| 117 | */ |
||
| 118 | public function rollbackTransaction() |
||
| 119 | { |
||
| 120 | // $this->execute('ROLLBACK TRANSACTION'); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritdoc} |
||
| 125 | */ |
||
| 126 | public function quoteTableName($tableName) |
||
| 127 | { |
||
| 128 | return str_replace('.', '].[', $this->quoteColumnName($tableName)); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | public function quoteColumnName($columnName) |
||
| 135 | { |
||
| 136 | return '"' . str_replace(']', '"', $columnName) . '"'; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritdoc} |
||
| 141 | */ |
||
| 142 | public function hasTable($tableName) |
||
| 143 | { |
||
| 144 | $result = $this->fetchRow( |
||
| 145 | sprintf( |
||
| 146 | 'SELECT count(*) as count FROM ALL_TABLES WHERE table_name = \'%s\'', |
||
| 147 | $tableName |
||
| 148 | ) |
||
| 149 | ); |
||
| 150 | return $result['COUNT'] > 0; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * {@inheritdoc} |
||
| 155 | */ |
||
| 156 | public function createTable(Table $table) |
||
| 157 | { |
||
| 158 | $options = $table->getOptions(); |
||
| 159 | |||
| 160 | // Add the default primary key |
||
| 161 | $columns = $table->getPendingColumns(); |
||
| 162 | |||
| 163 | if (!isset($options['id']) || (isset($options['id']) && $options['id'] === true)) { |
||
| 164 | $column = new Column(); |
||
| 165 | $column->setName('id') |
||
| 166 | ->setType('integer') |
||
| 167 | ->setIdentity(true); |
||
| 168 | |||
| 169 | array_unshift($columns, $column); |
||
| 170 | $options['primary_key'] = 'id'; |
||
| 171 | } elseif (isset($options['id']) && is_string($options['id'])) { |
||
| 172 | // Handle id => "field_name" to support AUTO_INCREMENT |
||
| 173 | $column = new Column(); |
||
| 174 | $column->setName($options['id']) |
||
| 175 | ->setType('integer') |
||
| 176 | ->setIdentity(true); |
||
| 177 | |||
| 178 | array_unshift($columns, $column); |
||
| 179 | $options['primary_key'] = $options['id']; |
||
| 180 | } |
||
| 181 | |||
| 182 | $sql = 'CREATE TABLE '; |
||
| 183 | $sql .= $this->quoteTableName($table->getName()) . ' ('; |
||
| 184 | $sqlBuffer = []; |
||
| 185 | $columnsWithComments = []; |
||
| 186 | |||
| 187 | View Code Duplication | foreach ($columns as $column) { |
|
| 188 | $sqlBuffer[] = $this->quoteColumnName($column->getName()) . ' ' . $this->getColumnSqlDefinition($column); |
||
| 189 | |||
| 190 | // set column comments, if needed |
||
| 191 | if ($column->getComment()) { |
||
| 192 | $columnsWithComments[] = $column; |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | // set the primary key(s) |
||
| 197 | View Code Duplication | if (isset($options['primary_key'])) { |
|
| 198 | $pkSql = sprintf('CONSTRAINT PK_%s PRIMARY KEY (', substr($table->getName(), 0, 28)); |
||
| 199 | if (is_string($options['primary_key'])) { // handle primary_key => 'id' |
||
| 200 | $pkSql .= $this->quoteColumnName($options['primary_key']); |
||
| 201 | } elseif (is_array($options['primary_key'])) { // handle primary_key => array('tag_id', 'resource_id') |
||
| 202 | $pkSql .= implode(',', array_map([$this, 'quoteColumnName'], $options['primary_key'])); |
||
| 203 | } |
||
| 204 | $pkSql .= ')'; |
||
| 205 | $sqlBuffer[] = $pkSql; |
||
| 206 | } |
||
| 207 | |||
| 208 | // set the foreign keys |
||
| 209 | $foreignKeys = $table->getForeignKeys(); |
||
| 210 | foreach ($foreignKeys as $key => $foreignKey) { |
||
| 211 | $sqlBuffer[] = $this->getForeignKeySqlDefinition($foreignKey, $table->getName(), $key); |
||
| 212 | } |
||
| 213 | |||
| 214 | $sql .= implode(', ', $sqlBuffer); |
||
| 215 | $sql .= ')'; |
||
| 216 | |||
| 217 | $this->execute($sql); |
||
| 218 | // process column comments |
||
| 219 | foreach ($columnsWithComments as $key => $column) { |
||
| 220 | $sql = $this->getColumnCommentSqlDefinition($column, $table->getName()); |
||
| 221 | $this->execute($sql); |
||
| 222 | } |
||
| 223 | // set the indexes |
||
| 224 | $indexes = $table->getIndexes(); |
||
| 225 | |||
| 226 | if (!empty($indexes)) { |
||
| 227 | foreach ($indexes as $index) { |
||
| 228 | $sql = $this->getIndexSqlDefinition($index, $table->getName()); |
||
| 229 | $this->execute($sql); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | if (!$this->hasSequence($table->getName())) { |
||
| 234 | $sql = "CREATE SEQUENCE SQ_".$table->getName()." MINVALUE 1 MAXVALUE 99999999999999999 INCREMENT BY 1"; |
||
| 235 | $this->execute($sql); |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Verify if the table has a Sequence for primary Key |
||
| 241 | * |
||
| 242 | * @param string $tableName Table name |
||
| 243 | * |
||
| 244 | * @return boolean |
||
| 245 | */ |
||
| 246 | public function hasSequence($tableName) |
||
| 247 | { |
||
| 248 | $sql = sprintf( |
||
| 249 | "SELECT COUNT(*) as COUNT FROM user_sequences WHERE sequence_name = '%s'", |
||
| 250 | strtoupper("SQ_".$tableName) |
||
| 251 | ); |
||
| 252 | $result = $this->fetchRow($sql); |
||
| 253 | return $result['COUNT'] > 0; |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Gets the Oracle Column Comment Defininition for a column object. |
||
| 258 | * |
||
| 259 | * @param \Phinx\Db\Table\Column $column Column |
||
| 260 | * @param string $tableName Table name |
||
| 261 | * |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | protected function getColumnCommentSqlDefinition(Column $column, $tableName) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * {@inheritdoc} |
||
| 278 | */ |
||
| 279 | public function renameTable($tableName, $newTableName) |
||
| 287 | |||
| 288 | public function renameSequence($sequenceName, $newSequenceName) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * {@inheritdoc} |
||
| 295 | */ |
||
| 296 | public function dropTable($tableName) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * {@inheritdoc} |
||
| 304 | */ |
||
| 305 | public function truncateTable($tableName) |
||
| 314 | |||
| 315 | View Code Duplication | public function getColumnComment($tableName, $columnName) |
|
| 316 | { |
||
| 317 | $sql = sprintf( |
||
| 318 | "select COMMENTS from ALL_COL_COMMENTS WHERE COLUMN_NAME = '%s' and TABLE_NAME = '%s'", |
||
| 319 | $columnName, |
||
| 320 | $tableName |
||
| 321 | ); |
||
| 322 | $row = $this->fetchRow($sql); |
||
| 323 | |||
| 324 | if ($row['COMMENTS'] != 'NULL') { |
||
| 325 | return $row['COMMENTS']; |
||
| 330 | |||
| 331 | /** |
||
| 332 | * {@inheritdoc} |
||
| 333 | */ |
||
| 334 | public function getColumns($tableName) |
||
| 371 | |||
| 372 | View Code Duplication | protected function parseDefault($default) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * {@inheritdoc} |
||
| 387 | */ |
||
| 388 | View Code Duplication | public function hasColumn($tableName, $columnName) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * {@inheritdoc} |
||
| 403 | */ |
||
| 404 | View Code Duplication | public function addColumn(Table $table, Column $column) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * {@inheritdoc} |
||
| 418 | */ |
||
| 419 | public function renameColumn($tableName, $columnName, $newColumnName) |
||
| 435 | |||
| 436 | View Code Duplication | protected function renameDefault($tableName, $columnName, $newColumnName) |
|
| 452 | |||
| 453 | /** |
||
| 454 | * {@inheritdoc} |
||
| 455 | */ |
||
| 456 | public function changeColumn($tableName, $columnName, Column $newColumn) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * {@inheritdoc} |
||
| 483 | */ |
||
| 484 | public function dropColumn($tableName, $columnName) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Get an array of indexes from a particular table. |
||
| 497 | * |
||
| 498 | * @param string $tableName Table Name |
||
| 499 | * @return array |
||
| 500 | */ |
||
| 501 | View Code Duplication | public function getIndexes($tableName) |
|
| 517 | |||
| 518 | /** |
||
| 519 | * {@inheritdoc} |
||
| 520 | */ |
||
| 521 | View Code Duplication | public function hasIndex($tableName, $columns) |
|
| 539 | |||
| 540 | /** |
||
| 541 | * {@inheritdoc} |
||
| 542 | */ |
||
| 543 | View Code Duplication | public function hasIndexByName($tableName, $indexName) |
|
| 555 | |||
| 556 | /** |
||
| 557 | * {@inheritdoc} |
||
| 558 | */ |
||
| 559 | public function addIndex(Table $table, Index $index) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * {@inheritdoc} |
||
| 567 | */ |
||
| 568 | View Code Duplication | public function dropIndex($tableName, $columns) |
|
| 591 | |||
| 592 | /** |
||
| 593 | * {@inheritdoc} |
||
| 594 | */ |
||
| 595 | View Code Duplication | public function dropIndexByName($tableName, $indexName) |
|
| 613 | |||
| 614 | /** |
||
| 615 | * {@inheritdoc} |
||
| 616 | */ |
||
| 617 | View Code Duplication | public function hasForeignKey($tableName, $columns, $constraint = null) |
|
| 641 | |||
| 642 | /** |
||
| 643 | * Get an array of foreign keys from a particular table. |
||
| 644 | * |
||
| 645 | * @param string $tableName Table Name |
||
| 646 | * @return array |
||
| 647 | */ |
||
| 648 | View Code Duplication | protected function getForeignKeys($tableName, $type = 'R') |
|
| 673 | |||
| 674 | /** |
||
| 675 | * {@inheritdoc} |
||
| 676 | */ |
||
| 677 | public function addForeignKey(Table $table, ForeignKey $foreignKey) |
||
| 688 | |||
| 689 | /** |
||
| 690 | * {@inheritdoc} |
||
| 691 | */ |
||
| 692 | View Code Duplication | public function dropForeignKey($tableName, $columns, $constraint = null) |
|
| 729 | |||
| 730 | /** |
||
| 731 | * {@inheritdoc} |
||
| 732 | */ |
||
| 733 | public function getSqlType($type, $limit = null) |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Returns Phinx type by SQL type |
||
| 786 | * |
||
| 787 | * @param string $sqlType SQL Type definition |
||
| 788 | * @throws \RuntimeException |
||
| 789 | * @internal param string $sqlType SQL type |
||
| 790 | * @returns string Phinx type |
||
| 791 | */ |
||
| 792 | public function getPhinxType($sqlType, $precision = null) |
||
| 829 | |||
| 830 | /** |
||
| 831 | * {@inheritdoc} |
||
| 832 | */ |
||
| 833 | // TODO |
||
| 834 | View Code Duplication | public function createDatabase($name, $options = []) |
|
| 843 | |||
| 844 | /** |
||
| 845 | * {@inheritdoc} |
||
| 846 | */ |
||
| 847 | public function hasDatabase($name) |
||
| 858 | |||
| 859 | /** |
||
| 860 | * {@inheritdoc} |
||
| 861 | */ |
||
| 862 | // TODO |
||
| 863 | public function dropDatabase($name) |
||
| 873 | |||
| 874 | /** |
||
| 875 | * Get the defintion for a `DEFAULT` statement. |
||
| 876 | * |
||
| 877 | * @param mixed $default |
||
| 878 | * @return string |
||
| 879 | */ |
||
| 880 | protected function getDefaultValueDefinition($default) |
||
| 890 | |||
| 891 | /** |
||
| 892 | * Gets the Oracle Column Definition for a Column object. |
||
| 893 | * |
||
| 894 | * @param \Phinx\Db\Table\Column $column Column |
||
| 895 | * @return string |
||
| 896 | */ |
||
| 897 | protected function getColumnSqlDefinition(Column $column, $create = true, $setNullSql = true) |
||
| 931 | |||
| 932 | /** |
||
| 933 | * Gets the Oracle Index Definition for an Index object. |
||
| 934 | * |
||
| 935 | * @param \Phinx\Db\Table\Index $index Index |
||
| 936 | * @return string |
||
| 937 | */ |
||
| 938 | View Code Duplication | protected function getIndexSqlDefinition(Index $index, $tableName) |
|
| 959 | |||
| 960 | /** |
||
| 961 | * Gets the Oracle Foreign Key Definition for an ForeignKey object. |
||
| 962 | * |
||
| 963 | * @param \Phinx\Db\Table\ForeignKey $foreignKey |
||
| 964 | * @return string |
||
| 965 | */ |
||
| 966 | protected function getForeignKeySqlDefinition(ForeignKey $foreignKey, $tableName) |
||
| 982 | |||
| 983 | /** |
||
| 984 | * {@inheritdoc} |
||
| 985 | */ |
||
| 986 | public function getColumnTypes() |
||
| 990 | |||
| 991 | /** |
||
| 992 | * Records a migration being run. |
||
| 993 | * |
||
| 994 | * @param \Phinx\Migration\MigrationInterface $migration Migration |
||
| 995 | * @param string $direction Direction |
||
| 996 | * @param int $startTime Start Time |
||
| 997 | * @param int $endTime End Time |
||
| 998 | * @return \Phinx\Db\Adapter\AdapterInterface |
||
| 999 | */ |
||
| 1000 | public function migrated(\Phinx\Migration\MigrationInterface $migration, $direction, $startTime, $endTime) |
||
| 1037 | |||
| 1038 | /** |
||
| 1039 | * {@inheritdoc} |
||
| 1040 | */ |
||
| 1041 | public function bulkinsert(Table $table, $rows) |
||
| 1077 | |||
| 1078 | protected function getNextValSequence($sequence) |
||
| 1084 | |||
| 1085 | /** |
||
| 1086 | * {@inheritdoc} |
||
| 1087 | */ |
||
| 1088 | View Code Duplication | public function getVersionLog() |
|
| 1112 | } |
||
| 1113 |
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.