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 Connection |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var ClientInterface |
||
| 19 | */ |
||
| 20 | private $client; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var EventManager |
||
| 24 | */ |
||
| 25 | private $eventManager; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param ClientInterface $client |
||
| 29 | * @param EventManager $dispatcher |
||
| 30 | */ |
||
| 31 | 34 | public function __construct(ClientInterface $client, EventManager $dispatcher = null) |
|
| 36 | |||
| 37 | /** |
||
| 38 | * Return the event dispatcher. |
||
| 39 | * |
||
| 40 | * @return EventManager |
||
| 41 | */ |
||
| 42 | 1 | public function getEventManager() |
|
| 46 | |||
| 47 | /** |
||
| 48 | * Get the couchdb version. |
||
| 49 | * |
||
| 50 | * @return string |
||
| 51 | */ |
||
| 52 | 1 | View Code Duplication | public function version() |
| 59 | |||
| 60 | /** |
||
| 61 | * Show all databases. |
||
| 62 | * |
||
| 63 | * @return array |
||
| 64 | */ |
||
| 65 | 1 | View Code Duplication | public function listDatabases() |
| 72 | |||
| 73 | /** |
||
| 74 | * Drop a database. |
||
| 75 | * |
||
| 76 | * @param string $name |
||
| 77 | * |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | 2 | public function dropDatabase($name) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Select a database. |
||
| 108 | * |
||
| 109 | * @param string $name |
||
| 110 | * |
||
| 111 | * @throws Exception If the database doesn't exists. |
||
| 112 | * |
||
| 113 | * @return Database |
||
| 114 | */ |
||
| 115 | 2 | View Code Duplication | public function selectDatabase($name) |
| 125 | |||
| 126 | /** |
||
| 127 | * Check if a database exists. |
||
| 128 | * |
||
| 129 | * @param string $name The database name |
||
| 130 | * |
||
| 131 | * @return bool |
||
| 132 | */ |
||
| 133 | 2 | public function hasDatabase($name) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Creates a new database. |
||
| 145 | * |
||
| 146 | * @param string $name The database name |
||
| 147 | * |
||
| 148 | * @throws Exception If the database could not be created. |
||
| 149 | * |
||
| 150 | * @return Database |
||
| 151 | */ |
||
| 152 | 4 | public function createDatabase($name) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Gets the database. |
||
| 193 | * |
||
| 194 | * @param string $name The database name |
||
| 195 | * |
||
| 196 | * @return Database |
||
| 197 | * |
||
| 198 | * @deprecated This method will be removed in version 2. |
||
| 199 | * |
||
| 200 | * @codeCoverageIgnore |
||
| 201 | */ |
||
| 202 | public function __get($name) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Drop a database. |
||
| 209 | * |
||
| 210 | * @param string $name The databas ename |
||
| 211 | * |
||
| 212 | * @return bool |
||
| 213 | * |
||
| 214 | * @deprecated This method will be removed in version 2. |
||
| 215 | * |
||
| 216 | * @codeCoverageIgnore |
||
| 217 | */ |
||
| 218 | public function __unset($name) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Check if the database exist. |
||
| 225 | * |
||
| 226 | * @param string $name The database name |
||
| 227 | * |
||
| 228 | * @return bool |
||
| 229 | * |
||
| 230 | * @deprecated This method will be removed in version 2. |
||
| 231 | * |
||
| 232 | * @codeCoverageIgnore |
||
| 233 | */ |
||
| 234 | public function __isset($name) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Wraps the database to a object. |
||
| 241 | * |
||
| 242 | * @param string $name The database name |
||
| 243 | * |
||
| 244 | * @return Database |
||
| 245 | */ |
||
| 246 | 2 | private function wrapDatabase($name) |
|
| 250 | } |
||
| 251 |