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 |
||
| 12 | class Database |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var Connection |
||
| 16 | */ |
||
| 17 | private $conn; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private $name; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var ClientInterface |
||
| 26 | */ |
||
| 27 | private $client; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param string $name The database name. |
||
| 31 | * @param Connection $conn The current connection. |
||
| 32 | * @param ClientInterface $client The client |
||
| 33 | */ |
||
| 34 | 19 | public function __construct($name, Connection $conn, ClientInterface $client) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * Gets the current connection. |
||
| 43 | * |
||
| 44 | * @return Connection |
||
| 45 | */ |
||
| 46 | 1 | public function getConnection() |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Find a document by a id. |
||
| 53 | * |
||
| 54 | * @param string $id |
||
| 55 | * |
||
| 56 | * @throws Exception If the document doesn't exists. |
||
| 57 | * |
||
| 58 | * @return mixed |
||
| 59 | */ |
||
| 60 | 2 | View Code Duplication | public function find($id) |
| 71 | |||
| 72 | /** |
||
| 73 | * Find all documents from the database. |
||
| 74 | * |
||
| 75 | * @param int|null $limit |
||
| 76 | * @param string|null $startKey |
||
| 77 | * |
||
| 78 | * @return mixed |
||
| 79 | */ |
||
| 80 | 1 | public function findAll($limit = null, $startKey = null) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Find a documents by a id. |
||
| 99 | * |
||
| 100 | * @param array $ids |
||
| 101 | * @param null|int $limit |
||
| 102 | * @param null|int $offset |
||
| 103 | * |
||
| 104 | * @return array|null |
||
| 105 | */ |
||
| 106 | 1 | public function findDocuments(array $ids, $limit = null, $offset = null) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Insert a new document. |
||
| 127 | * |
||
| 128 | * @param array $doc |
||
| 129 | * |
||
| 130 | * @deprecated To update a document use Database::update, it will be removed in version 2 |
||
| 131 | * |
||
| 132 | * @throws Exception |
||
| 133 | */ |
||
| 134 | public function insert(array &$doc) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Updates a document. |
||
| 163 | * |
||
| 164 | * @param string $id The id from the document |
||
| 165 | * @param array $doc A reference from the document |
||
| 166 | * |
||
| 167 | * @throws Exception |
||
| 168 | */ |
||
| 169 | 2 | public function update($id, array &$doc) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Deletes a document. |
||
| 190 | * |
||
| 191 | * @param string $id |
||
| 192 | * @param string $rev |
||
| 193 | * |
||
| 194 | * @throws \RuntimeException |
||
| 195 | * |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | 2 | View Code Duplication | public function delete($id, $rev) |
| 208 | |||
| 209 | /** |
||
| 210 | * Creates a batch updater. |
||
| 211 | * |
||
| 212 | * @return Util\BatchUpdater |
||
| 213 | */ |
||
| 214 | 1 | public function createBatchUpdater() |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Return the database informations. |
||
| 221 | * |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | 1 | public function getInfo() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Return informations about the last changes from the database. |
||
| 233 | * |
||
| 234 | * @throws \RuntimeException If the request was not successfull |
||
| 235 | * |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | 2 | public function getChanges() |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Return the database name. |
||
| 251 | * |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | 2 | public function getName() |
|
| 258 | } |
||
| 259 |