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:
| 1 | <?php |
||
| 15 | class SqlServer extends AbstractSqlTranslator |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Translate a query that reads records. |
||
| 19 | * |
||
| 20 | * @param Storage\Query $query |
||
| 21 | * @return Database\Query |
||
| 22 | */ |
||
| 23 | protected function translateRead(Storage\Query $query) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Resolve the given value as an identifier. |
||
| 39 | * |
||
| 40 | * @param mixed $identifier |
||
| 41 | * @return mixed |
||
| 42 | */ |
||
| 43 | protected function resolveIdentifier($identifier) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Prepare a LIMIT clause using the given limit and offset. |
||
| 50 | * |
||
| 51 | * @param int $limit [optional] |
||
| 52 | * @param int $offset [optional] |
||
| 53 | * @return string |
||
| 54 | */ |
||
| 55 | protected function prepareLimit($limit = 0, $offset = 0) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Prepare a SELECT statement using the given columns, table, clauses and |
||
| 68 | * options. |
||
| 69 | * |
||
| 70 | * @param string $table |
||
| 71 | * @param array|string $columns |
||
| 72 | * @param string $joins [optional] |
||
| 73 | * @param string $where [optional] |
||
| 74 | * @param string $order [optional] |
||
| 75 | * @param string $limit [optional] |
||
| 76 | * @param string $groupings [optional] |
||
| 77 | * @param string $having [optional] |
||
| 78 | * @param bool $distinct [optional] |
||
| 79 | * @return string |
||
| 80 | */ |
||
| 81 | View Code Duplication | protected function prepareSelect( |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Prepare the column selection for an ANSI offset select statement. |
||
| 101 | * |
||
| 102 | * Cheers Microsoft. |
||
| 103 | * |
||
| 104 | * @param string $columns |
||
| 105 | * @param array|string $order |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | protected function prepareAnsiOffsetSelectColumns($columns, $order) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Prepare an ANSI offset select statement. |
||
| 122 | * |
||
| 123 | * Cheers Microsoft. |
||
| 124 | * |
||
| 125 | * @param Storage\Query $query |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | protected function prepareAnsiOffsetSelect(Storage\Query $query) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Prepare an UPDATE statement with the given table, data and clauses. |
||
| 172 | * |
||
| 173 | * @param string $table |
||
| 174 | * @param array $data |
||
| 175 | * @param string $where [optional] |
||
| 176 | * @param string $limit [optional] |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | View Code Duplication | protected function prepareUpdate($table, $data, $where = null, $limit = null) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Prepare a DELETE statement with the given table and clauses. |
||
| 196 | * |
||
| 197 | * @param string $table |
||
| 198 | * @param string $where [optional] |
||
| 199 | * @param string $limit [optional] |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | View Code Duplication | protected function prepareDelete($table, $where = null, $limit = null) |
|
| 212 | } |
||
| 213 |
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.