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 SqlServerAdapter 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 SqlServerAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | class SqlServerAdapter extends PdoAdapter implements AdapterInterface |
||
42 | { |
||
43 | protected $schema = 'dbo'; |
||
44 | |||
45 | protected $signedColumnTypes = ['integer' => true, 'biginteger' => true, 'float' => true, 'decimal' => true]; |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | public function connect() |
||
51 | { |
||
52 | if ($this->connection === null) { |
||
53 | if (!class_exists('PDO') || !in_array('sqlsrv', \PDO::getAvailableDrivers(), true)) { |
||
54 | // try our connection via freetds (Mac/Linux) |
||
55 | $this->connectDblib(); |
||
56 | |||
57 | return; |
||
58 | } |
||
59 | |||
60 | $db = null; |
||
61 | $options = $this->getOptions(); |
||
62 | |||
63 | // if port is specified use it, otherwise use the SqlServer default |
||
64 | View Code Duplication | if (empty($options['port'])) { |
|
|
|||
65 | $dsn = 'sqlsrv:server=' . $options['host'] . ';database=' . $options['name']; |
||
66 | } else { |
||
67 | $dsn = 'sqlsrv:server=' . $options['host'] . ',' . $options['port'] . ';database=' . $options['name']; |
||
68 | } |
||
69 | $dsn .= ';MultipleActiveResultSets=false'; |
||
70 | |||
71 | $driverOptions = [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]; |
||
72 | |||
73 | // charset support |
||
74 | if (isset($options['charset'])) { |
||
75 | $driverOptions[\PDO::SQLSRV_ATTR_ENCODING] = $options['charset']; |
||
76 | } |
||
77 | |||
78 | // support arbitrary \PDO::SQLSRV_ATTR_* driver options and pass them to PDO |
||
79 | // http://php.net/manual/en/ref.pdo-sqlsrv.php#pdo-sqlsrv.constants |
||
80 | View Code Duplication | foreach ($options as $key => $option) { |
|
81 | if (strpos($key, 'sqlsrv_attr_') === 0) { |
||
82 | $driverOptions[constant('\PDO::' . strtoupper($key))] = $option; |
||
83 | } |
||
84 | } |
||
85 | |||
86 | try { |
||
87 | $db = new \PDO($dsn, $options['user'], $options['pass'], $driverOptions); |
||
88 | } catch (\PDOException $exception) { |
||
89 | throw new \InvalidArgumentException(sprintf( |
||
90 | 'There was a problem connecting to the database: %s', |
||
91 | $exception->getMessage() |
||
92 | )); |
||
93 | } |
||
94 | |||
95 | $this->setConnection($db); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Connect to MSSQL using dblib/freetds. |
||
101 | * |
||
102 | * The "sqlsrv" driver is not available on Unix machines. |
||
103 | * |
||
104 | * @throws \InvalidArgumentException |
||
105 | */ |
||
106 | protected function connectDblib() |
||
107 | { |
||
108 | if (!class_exists('PDO') || !in_array('dblib', \PDO::getAvailableDrivers(), true)) { |
||
109 | // @codeCoverageIgnoreStart |
||
110 | throw new \RuntimeException('You need to enable the PDO_Dblib extension for Phinx to run properly.'); |
||
111 | // @codeCoverageIgnoreEnd |
||
112 | } |
||
113 | |||
114 | $options = $this->getOptions(); |
||
115 | |||
116 | // if port is specified use it, otherwise use the SqlServer default |
||
117 | View Code Duplication | if (empty($options['port'])) { |
|
118 | $dsn = 'dblib:host=' . $options['host'] . ';dbname=' . $options['name']; |
||
119 | } else { |
||
120 | $dsn = 'dblib:host=' . $options['host'] . ':' . $options['port'] . ';dbname=' . $options['name']; |
||
121 | } |
||
122 | |||
123 | $driverOptions = [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]; |
||
124 | |||
125 | try { |
||
126 | $db = new \PDO($dsn, $options['user'], $options['pass'], $driverOptions); |
||
127 | } catch (\PDOException $exception) { |
||
128 | throw new \InvalidArgumentException(sprintf( |
||
129 | 'There was a problem connecting to the database: %s', |
||
130 | $exception->getMessage() |
||
131 | )); |
||
132 | } |
||
133 | |||
134 | $this->setConnection($db); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | public function disconnect() |
||
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | public function hasTransactions() |
||
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | public function beginTransaction() |
||
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | public function commitTransaction() |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | public function rollbackTransaction() |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | public function quoteTableName($tableName) |
||
184 | |||
185 | /** |
||
186 | * {@inheritdoc} |
||
187 | */ |
||
188 | public function quoteColumnName($columnName) |
||
192 | |||
193 | /** |
||
194 | * {@inheritdoc} |
||
195 | */ |
||
196 | public function hasTable($tableName) |
||
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | */ |
||
206 | public function createTable(Table $table) |
||
207 | { |
||
208 | $options = $table->getOptions(); |
||
209 | |||
210 | // Add the default primary key |
||
211 | $columns = $table->getPendingColumns(); |
||
212 | View Code Duplication | if (!isset($options['id']) || (isset($options['id']) && $options['id'] === true)) { |
|
213 | $column = new Column(); |
||
214 | $column->setName('id') |
||
215 | ->setType('integer') |
||
216 | ->setIdentity(true); |
||
217 | |||
218 | array_unshift($columns, $column); |
||
219 | $options['primary_key'] = 'id'; |
||
220 | } elseif (isset($options['id']) && is_string($options['id'])) { |
||
221 | // Handle id => "field_name" to support AUTO_INCREMENT |
||
222 | $column = new Column(); |
||
223 | $column->setName($options['id']) |
||
224 | ->setType('integer') |
||
225 | ->setIdentity(true); |
||
226 | |||
227 | array_unshift($columns, $column); |
||
228 | $options['primary_key'] = $options['id']; |
||
229 | } |
||
230 | |||
231 | $sql = 'CREATE TABLE '; |
||
232 | $sql .= $this->quoteTableName($table->getName()) . ' ('; |
||
233 | $sqlBuffer = []; |
||
234 | $columnsWithComments = []; |
||
235 | View Code Duplication | foreach ($columns as $column) { |
|
236 | $sqlBuffer[] = $this->quoteColumnName($column->getName()) . ' ' . $this->getColumnSqlDefinition($column); |
||
237 | |||
238 | // set column comments, if needed |
||
239 | if ($column->getComment()) { |
||
240 | $columnsWithComments[] = $column; |
||
241 | } |
||
242 | } |
||
243 | |||
244 | // set the primary key(s) |
||
245 | if (isset($options['primary_key'])) { |
||
246 | $pkSql = sprintf('CONSTRAINT PK_%s PRIMARY KEY (', $table->getName()); |
||
247 | if (is_string($options['primary_key'])) { // handle primary_key => 'id' |
||
248 | $pkSql .= $this->quoteColumnName($options['primary_key']); |
||
249 | } elseif (is_array($options['primary_key'])) { // handle primary_key => array('tag_id', 'resource_id') |
||
250 | $pkSql .= implode(',', array_map([$this, 'quoteColumnName'], $options['primary_key'])); |
||
251 | } |
||
252 | $pkSql .= ')'; |
||
253 | $sqlBuffer[] = $pkSql; |
||
254 | } |
||
255 | |||
256 | // set the foreign keys |
||
257 | $foreignKeys = $table->getForeignKeys(); |
||
258 | foreach ($foreignKeys as $foreignKey) { |
||
259 | $sqlBuffer[] = $this->getForeignKeySqlDefinition($foreignKey, $table->getName()); |
||
260 | } |
||
261 | |||
262 | $sql .= implode(', ', $sqlBuffer); |
||
263 | $sql .= ');'; |
||
264 | |||
265 | // process column comments |
||
266 | foreach ($columnsWithComments as $column) { |
||
267 | $sql .= $this->getColumnCommentSqlDefinition($column, $table->getName()); |
||
268 | } |
||
269 | |||
270 | // set the indexes |
||
271 | $indexes = $table->getIndexes(); |
||
272 | foreach ($indexes as $index) { |
||
273 | $sql .= $this->getIndexSqlDefinition($index, $table->getName()); |
||
274 | } |
||
275 | |||
276 | // execute the sql |
||
277 | $this->execute($sql); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Gets the SqlServer Column Comment Defininition for a column object. |
||
282 | * |
||
283 | * @param \Phinx\Db\Table\Column $column Column |
||
284 | * @param string $tableName Table name |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | protected function getColumnCommentSqlDefinition(Column $column, $tableName) |
||
305 | |||
306 | /** |
||
307 | * {@inheritdoc} |
||
308 | */ |
||
309 | public function renameTable($tableName, $newTableName) |
||
313 | |||
314 | /** |
||
315 | * {@inheritdoc} |
||
316 | */ |
||
317 | public function dropTable($tableName) |
||
321 | |||
322 | /** |
||
323 | * {@inheritdoc} |
||
324 | */ |
||
325 | public function truncateTable($tableName) |
||
326 | { |
||
327 | $sql = sprintf( |
||
328 | 'TRUNCATE TABLE %s', |
||
329 | $this->quoteTableName($tableName) |
||
330 | ); |
||
331 | |||
332 | $this->execute($sql); |
||
333 | } |
||
334 | |||
335 | public function getColumnComment($tableName, $columnName) |
||
356 | |||
357 | /** |
||
358 | * {@inheritdoc} |
||
359 | */ |
||
360 | public function getColumns($tableName) |
||
394 | |||
395 | protected function parseDefault($default) |
||
407 | |||
408 | /** |
||
409 | * {@inheritdoc} |
||
410 | */ |
||
411 | View Code Duplication | public function hasColumn($tableName, $columnName) |
|
424 | |||
425 | /** |
||
426 | * {@inheritdoc} |
||
427 | */ |
||
428 | View Code Duplication | public function addColumn(Table $table, Column $column) |
|
439 | |||
440 | /** |
||
441 | * {@inheritdoc} |
||
442 | */ |
||
443 | public function renameColumn($tableName, $columnName, $newColumnName) |
||
458 | |||
459 | protected function renameDefault($tableName, $columnName, $newColumnName) |
||
475 | |||
476 | public function changeDefault($tableName, Column $newColumn) |
||
499 | |||
500 | /** |
||
501 | * {@inheritdoc} |
||
502 | */ |
||
503 | public function changeColumn($tableName, $columnName, Column $newColumn) |
||
533 | |||
534 | /** |
||
535 | * {@inheritdoc} |
||
536 | */ |
||
537 | public function dropColumn($tableName, $columnName) |
||
549 | |||
550 | protected function dropDefaultConstraint($tableName, $columnName) |
||
560 | |||
561 | protected function getDefaultConstraint($tableName, $columnName) |
||
589 | |||
590 | protected function getIndexColums($tableId, $indexId) |
||
606 | |||
607 | /** |
||
608 | * Get an array of indexes from a particular table. |
||
609 | * |
||
610 | * @param string $tableName Table Name |
||
611 | * @return array |
||
612 | */ |
||
613 | public function getIndexes($tableName) |
||
630 | |||
631 | /** |
||
632 | * {@inheritdoc} |
||
633 | */ |
||
634 | View Code Duplication | public function hasIndex($tableName, $columns) |
|
653 | |||
654 | /** |
||
655 | * {@inheritdoc} |
||
656 | */ |
||
657 | View Code Duplication | public function hasIndexByName($tableName, $indexName) |
|
669 | |||
670 | /** |
||
671 | * {@inheritdoc} |
||
672 | */ |
||
673 | public function addIndex(Table $table, Index $index) |
||
678 | |||
679 | /** |
||
680 | * {@inheritdoc} |
||
681 | */ |
||
682 | View Code Duplication | public function dropIndex($tableName, $columns) |
|
706 | |||
707 | /** |
||
708 | * {@inheritdoc} |
||
709 | */ |
||
710 | View Code Duplication | public function dropIndexByName($tableName, $indexName) |
|
728 | |||
729 | /** |
||
730 | * {@inheritdoc} |
||
731 | */ |
||
732 | View Code Duplication | public function hasForeignKey($tableName, $columns, $constraint = null) |
|
755 | |||
756 | /** |
||
757 | * Get an array of foreign keys from a particular table. |
||
758 | * |
||
759 | * @param string $tableName Table Name |
||
760 | * @return array |
||
761 | */ |
||
762 | View Code Duplication | protected function getForeignKeys($tableName) |
|
788 | |||
789 | /** |
||
790 | * {@inheritdoc} |
||
791 | */ |
||
792 | View Code Duplication | public function addForeignKey(Table $table, ForeignKey $foreignKey) |
|
802 | |||
803 | /** |
||
804 | * {@inheritdoc} |
||
805 | */ |
||
806 | View Code Duplication | public function dropForeignKey($tableName, $columns, $constraint = null) |
|
845 | |||
846 | /** |
||
847 | * {@inheritdoc} |
||
848 | */ |
||
849 | public function getSqlType($type, $limit = null) |
||
894 | |||
895 | /** |
||
896 | * Returns Phinx type by SQL type |
||
897 | * |
||
898 | * @param string $sqlType SQL Type definition |
||
899 | * @throws \RuntimeException |
||
900 | * @internal param string $sqlType SQL type |
||
901 | * @returns string Phinx type |
||
902 | */ |
||
903 | public function getPhinxType($sqlType) |
||
948 | |||
949 | /** |
||
950 | * {@inheritdoc} |
||
951 | */ |
||
952 | public function createDatabase($name, $options = []) |
||
961 | |||
962 | /** |
||
963 | * {@inheritdoc} |
||
964 | */ |
||
965 | public function hasDatabase($name) |
||
976 | |||
977 | /** |
||
978 | * {@inheritdoc} |
||
979 | */ |
||
980 | public function dropDatabase($name) |
||
990 | |||
991 | /** |
||
992 | * Gets the SqlServer Column Definition for a Column object. |
||
993 | * |
||
994 | * @param \Phinx\Db\Table\Column $column Column |
||
995 | * @return string |
||
996 | */ |
||
997 | protected function getColumnSqlDefinition(Column $column, $create = true) |
||
1038 | |||
1039 | /** |
||
1040 | * Gets the SqlServer Index Definition for an Index object. |
||
1041 | * |
||
1042 | * @param \Phinx\Db\Table\Index $index Index |
||
1043 | * @return string |
||
1044 | */ |
||
1045 | protected function getIndexSqlDefinition(Index $index, $tableName) |
||
1066 | |||
1067 | /** |
||
1068 | * Gets the SqlServer Foreign Key Definition for an ForeignKey object. |
||
1069 | * |
||
1070 | * @param \Phinx\Db\Table\ForeignKey $foreignKey |
||
1071 | * @return string |
||
1072 | */ |
||
1073 | View Code Duplication | protected function getForeignKeySqlDefinition(ForeignKey $foreignKey, $tableName) |
|
1088 | |||
1089 | /** |
||
1090 | * {@inheritdoc} |
||
1091 | */ |
||
1092 | public function getColumnTypes() |
||
1096 | |||
1097 | /** |
||
1098 | * Records a migration being run. |
||
1099 | * |
||
1100 | * @param \Phinx\Migration\MigrationInterface $migration Migration |
||
1101 | * @param string $direction Direction |
||
1102 | * @param int $startTime Start Time |
||
1103 | * @param int $endTime End Time |
||
1104 | * @return \Phinx\Db\Adapter\AdapterInterface |
||
1105 | */ |
||
1106 | public function migrated(\Phinx\Migration\MigrationInterface $migration, $direction, $startTime, $endTime) |
||
1113 | } |
||
1114 |
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.