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 |
||
| 29 | abstract class AbstractMapping { |
||
| 30 | /** |
||
| 31 | * @var \OCP\IDBConnection $dbc |
||
| 32 | */ |
||
| 33 | protected $dbc; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * returns the DB table name which holds the mappings |
||
| 37 | * @return string |
||
| 38 | */ |
||
| 39 | abstract protected function getTableName(); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param \OCP\IDBConnection $dbc |
||
| 43 | */ |
||
| 44 | 22 | public function __construct(\OCP\IDBConnection $dbc) { |
|
| 47 | |||
| 48 | /** |
||
| 49 | * checks whether a provided string represents an existing table col |
||
| 50 | * @param string $col |
||
| 51 | * @return bool |
||
| 52 | */ |
||
| 53 | 8 | public function isColNameValid($col) { |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Gets the value of one column based on a provided value of another column |
||
| 66 | * @param string $fetchCol |
||
| 67 | * @param string $compareCol |
||
| 68 | * @param string $search |
||
| 69 | * @throws \Exception |
||
| 70 | * @return string|false |
||
| 71 | */ |
||
| 72 | 6 | protected function getXbyY($fetchCol, $compareCol, $search) { |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Performs a DELETE or UPDATE query to the database. |
||
| 94 | * @param \Doctrine\DBAL\Driver\Statement $query |
||
| 95 | * @param array $parameters |
||
| 96 | * @return bool true if at least one row was modified, false otherwise |
||
| 97 | */ |
||
| 98 | 4 | protected function modify($query, $parameters) { |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Gets the LDAP DN based on the provided name. |
||
| 105 | * Replaces Access::ocname2dn |
||
| 106 | * @param string $name |
||
| 107 | * @return string|false |
||
| 108 | */ |
||
| 109 | 4 | public function getDNByName($name) { |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Updates the DN based on the given UUID |
||
| 115 | * @param string $fdn |
||
| 116 | * @param string $uuid |
||
| 117 | * @return bool |
||
| 118 | */ |
||
| 119 | 2 | View Code Duplication | public function setDNbyUUID($fdn, $uuid) { |
| 128 | |||
| 129 | /** |
||
| 130 | * Gets the name based on the provided LDAP DN. |
||
| 131 | * @param string $fdn |
||
| 132 | * @return string|false |
||
| 133 | */ |
||
| 134 | 4 | public function getNameByDN($fdn) { |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Searches mapped names by the giving string in the name column |
||
| 140 | * @param string $search |
||
| 141 | * @return string[] |
||
| 142 | */ |
||
| 143 | 2 | public function getNamesBySearch($search) { |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Gets the name based on the provided LDAP UUID. |
||
| 162 | * @param string $uuid |
||
| 163 | * @return string|false |
||
| 164 | */ |
||
| 165 | 4 | public function getNameByUUID($uuid) { |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Gets the UUID based on the provided LDAP DN |
||
| 171 | * @param string $dn |
||
| 172 | * @return false|string |
||
| 173 | * @throws \Exception |
||
| 174 | */ |
||
| 175 | public function getUUIDByDN($dn) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * gets a piece of the mapping list |
||
| 181 | * @param int $offset |
||
| 182 | * @param int $limit |
||
| 183 | * @return array |
||
| 184 | */ |
||
| 185 | 2 | public function getList($offset = null, $limit = null) { |
|
| 199 | |||
| 200 | /** |
||
| 201 | * attempts to map the given entry |
||
| 202 | * @param string $fdn fully distinguished name (from LDAP) |
||
| 203 | * @param string $name |
||
| 204 | * @param string $uuid a unique identifier as used in LDAP |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | 14 | public function map($fdn, $name, $uuid) { |
|
| 222 | |||
| 223 | /** |
||
| 224 | * removes a mapping based on the owncloud_name of the entry |
||
| 225 | * @param string $name |
||
| 226 | * @return bool |
||
| 227 | */ |
||
| 228 | 2 | View Code Duplication | public function unmap($name) { |
| 235 | |||
| 236 | /** |
||
| 237 | * Truncate's the mapping table |
||
| 238 | * @return bool |
||
| 239 | */ |
||
| 240 | 14 | public function clear() { |
|
| 246 | } |
||
| 247 |