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 AbstractConnection { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Initiate the connection. |
||
| 19 | * |
||
| 20 | * @return bool |
||
| 21 | */ |
||
| 22 | public function connect() { |
||
| 23 | if ($this->connected()) { |
||
| 24 | return true; |
||
| 25 | } |
||
| 26 | |||
| 27 | $host = $this->details['host']; |
||
| 28 | |||
| 29 | if ($this->details['port']) { |
||
| 30 | $host .= ', ' . $this->details['port']; |
||
| 31 | } |
||
| 32 | |||
| 33 | $this->connection = sqlsrv_connect($this->details['host'], array( |
||
| 34 | 'UID' => $this->details['user'], |
||
| 35 | 'PWD' => $this->details['pass'], |
||
| 36 | 'Database' => $this->details['name'] |
||
| 37 | )); |
||
| 38 | |||
| 39 | if ($this->error()) { |
||
| 40 | return false; |
||
| 41 | } |
||
| 42 | |||
| 43 | return $this->connected = true; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Close the connection. |
||
| 48 | */ |
||
| 49 | public function disconnect() { |
||
| 50 | if ($this->connected()) { |
||
| 51 | sqlsrv_close($this->connection); |
||
| 52 | } |
||
| 53 | |||
| 54 | $this->connected = false; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Retrieve the query translator. |
||
| 59 | * |
||
| 60 | * @return Translator |
||
| 61 | */ |
||
| 62 | public function translator() { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Query the database for the last ID generated, if the given query is |
||
| 72 | * an insert query. |
||
| 73 | * |
||
| 74 | * TODO: Use SCOPE_IDENTITY() instead? |
||
| 75 | * |
||
| 76 | * @param string $query |
||
| 77 | * @return int |
||
| 78 | */ |
||
| 79 | protected function queryInsertId($query) { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Query the database for the number of rows affected by the last query. |
||
| 92 | * |
||
| 93 | * @param string $query |
||
| 94 | * @return int |
||
| 95 | */ |
||
| 96 | protected function queryAffected($query) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Query the database. |
||
| 109 | * |
||
| 110 | * TODO: Also simplify this. |
||
| 111 | * |
||
| 112 | * @param Query|string $query |
||
| 113 | * @param array $parameters [optional] |
||
| 114 | * @return Result |
||
| 115 | */ |
||
| 116 | public function query($query, array $parameters = array()) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Retrieve error information regarding the last query or connection |
||
| 188 | * attempt. |
||
| 189 | * |
||
| 190 | * Returns null if there is no error. |
||
| 191 | * |
||
| 192 | * @return Error |
||
| 193 | */ |
||
| 194 | public function error() { |
||
| 203 | } |
||
| 204 |
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.