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 |
||
| 13 | class SqlServer extends AbstractSqlTranslator { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Resolve the given value as an identifier. |
||
| 17 | * |
||
| 18 | * @param mixed $identifier |
||
| 19 | * @return mixed |
||
| 20 | */ |
||
| 21 | public function resolveIdentifier($identifier) { |
||
| 22 | return $identifier; |
||
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Prepare a LIMIT clause using the given limit and offset. |
||
| 27 | * |
||
| 28 | * @param int $limit [optional] |
||
| 29 | * @param int $offset [optional] |
||
| 30 | * @return string |
||
| 31 | */ |
||
| 32 | protected function prepareLimit($limit = 0, $offset = 0) { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Prepare a SELECT statement using the given columns, table, clauses and |
||
| 45 | * options. |
||
| 46 | * |
||
| 47 | * @param string $table |
||
| 48 | * @param array|string $columns |
||
| 49 | * @param string $joins [optional] |
||
| 50 | * @param string $where [optional] |
||
| 51 | * @param string $order [optional] |
||
| 52 | * @param string $limit [optional] |
||
| 53 | * @param bool $distinct [optional] |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | View Code Duplication | protected function prepareSelect($table, $columns, $joins = null, $where = null, $order = null, $limit = null, $distinct = false) { |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Prepare an UPDATE statement with the given table, data and clauses. |
||
| 66 | * |
||
| 67 | * @param string $table |
||
| 68 | * @param array $data |
||
| 69 | * @param string $where [optional] |
||
| 70 | * @param string $limit [optional] |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | View Code Duplication | protected function prepareUpdate($table, $data, $where = null, $limit = null) { |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Prepare a DELETE statement with the given table and clauses. |
||
| 89 | * |
||
| 90 | * @param string $table |
||
| 91 | * @param string $where [optional] |
||
| 92 | * @param string $limit [optional] |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | View Code Duplication | protected function prepareDelete($table, $where = null, $limit = null) { |
|
| 104 | |||
| 105 | } |
||
| 106 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.