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 |
||
| 31 | abstract class AbstractMapping { |
||
| 32 | /** |
||
| 33 | * @var \OCP\IDBConnection $dbc |
||
| 34 | */ |
||
| 35 | protected $dbc; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * returns the DB table name which holds the mappings |
||
| 39 | * @return string |
||
| 40 | */ |
||
| 41 | abstract protected function getTableName(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param \OCP\IDBConnection $dbc |
||
| 45 | */ |
||
| 46 | public function __construct(\OCP\IDBConnection $dbc) { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * checks whether a provided string represents an existing table col |
||
| 52 | * @param string $col |
||
| 53 | * @return bool |
||
| 54 | */ |
||
| 55 | public function isColNameValid($col) { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Gets the value of one column based on a provided value of another column |
||
| 68 | * @param string $fetchCol |
||
| 69 | * @param string $compareCol |
||
| 70 | * @param string $search |
||
| 71 | * @throws \Exception |
||
| 72 | * @return string|false |
||
| 73 | */ |
||
| 74 | protected function getXbyY($fetchCol, $compareCol, $search) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Performs a DELETE or UPDATE query to the database. |
||
| 96 | * @param \Doctrine\DBAL\Driver\Statement $query |
||
| 97 | * @param array $parameters |
||
| 98 | * @return bool true if at least one row was modified, false otherwise |
||
| 99 | */ |
||
| 100 | protected function modify($query, $parameters) { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Gets the LDAP DN based on the provided name. |
||
| 107 | * Replaces Access::ocname2dn |
||
| 108 | * @param string $name |
||
| 109 | * @return string|false |
||
| 110 | */ |
||
| 111 | public function getDNByName($name) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Updates the DN based on the given UUID |
||
| 117 | * @param string $fdn |
||
| 118 | * @param string $uuid |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | View Code Duplication | public function setDNbyUUID($fdn, $uuid) { |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Updates the UUID based on the given DN |
||
| 133 | * |
||
| 134 | * required by Migration/UUIDFix |
||
| 135 | * |
||
| 136 | * @param $uuid |
||
| 137 | * @param $fdn |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | View Code Duplication | public function setUUIDbyDN($uuid, $fdn) { |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Gets the name based on the provided LDAP DN. |
||
| 152 | * @param string $fdn |
||
| 153 | * @return string|false |
||
| 154 | */ |
||
| 155 | public function getNameByDN($fdn) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Searches mapped names by the giving string in the name column |
||
| 161 | * @param string $search |
||
| 162 | * @param string $prefixMatch |
||
| 163 | * @param string $postfixMatch |
||
| 164 | * @return string[] |
||
| 165 | */ |
||
| 166 | public function getNamesBySearch($search, $prefixMatch = "", $postfixMatch = "") { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Gets the name based on the provided LDAP UUID. |
||
| 185 | * @param string $uuid |
||
| 186 | * @return string|false |
||
| 187 | */ |
||
| 188 | public function getNameByUUID($uuid) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Gets the UUID based on the provided LDAP DN |
||
| 194 | * @param string $dn |
||
| 195 | * @return false|string |
||
| 196 | * @throws \Exception |
||
| 197 | */ |
||
| 198 | public function getUUIDByDN($dn) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * gets a piece of the mapping list |
||
| 204 | * @param int $offset |
||
| 205 | * @param int $limit |
||
| 206 | * @return array |
||
| 207 | */ |
||
| 208 | public function getList($offset = null, $limit = null) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * attempts to map the given entry |
||
| 225 | * @param string $fdn fully distinguished name (from LDAP) |
||
| 226 | * @param string $name |
||
| 227 | * @param string $uuid a unique identifier as used in LDAP |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | public function map($fdn, $name, $uuid) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * removes a mapping based on the owncloud_name of the entry |
||
| 259 | * @param string $name |
||
| 260 | * @return bool |
||
| 261 | */ |
||
| 262 | View Code Duplication | public function unmap($name) { |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Truncate's the mapping table |
||
| 272 | * @return bool |
||
| 273 | */ |
||
| 274 | public function clear() { |
||
| 280 | } |
||
| 281 |
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.