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 |
||
| 41 | class AppConfig implements IAppConfig { |
||
| 42 | |||
| 43 | /** @var array[] */ |
||
| 44 | protected $sensitiveValues = [ |
||
| 45 | 'user_ldap' => [ |
||
| 46 | 'ldap_agent_password', |
||
| 47 | ], |
||
| 48 | ]; |
||
| 49 | |||
| 50 | /** @var \OCP\IDBConnection */ |
||
| 51 | protected $conn; |
||
| 52 | |||
| 53 | /** @var array[] */ |
||
| 54 | private $cache = []; |
||
| 55 | |||
| 56 | /** @var bool */ |
||
| 57 | private $configLoaded = false; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param IDBConnection $conn |
||
| 61 | */ |
||
| 62 | public function __construct(IDBConnection $conn) { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param string $app |
||
| 69 | * @return array |
||
| 70 | */ |
||
| 71 | private function getAppValues($app) { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Get all apps using the config |
||
| 83 | * |
||
| 84 | * @return array an array of app ids |
||
| 85 | * |
||
| 86 | * This function returns a list of all apps that have at least one |
||
| 87 | * entry in the appconfig table. |
||
| 88 | */ |
||
| 89 | public function getApps() { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get the available keys for an app |
||
| 97 | * |
||
| 98 | * @param string $app the app we are looking for |
||
| 99 | * @return array an array of key names |
||
| 100 | * @deprecated 8.0.0 use method getAppKeys of \OCP\IConfig |
||
| 101 | * |
||
| 102 | * This function gets all keys of an app. Please note that the values are |
||
| 103 | * not returned. |
||
| 104 | */ |
||
| 105 | public function getKeys($app) { |
||
| 114 | |||
| 115 | public function getSortedKeys($data) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Gets the config value |
||
| 123 | * |
||
| 124 | * @param string $app app |
||
| 125 | * @param string $key key |
||
| 126 | * @param string $default = null, default value if the key does not exist |
||
| 127 | * @return string the value or $default |
||
| 128 | * @deprecated 8.0.0 use method getAppValue of \OCP\IConfig |
||
| 129 | * |
||
| 130 | * This function gets a value from the appconfig table. If the key does |
||
| 131 | * not exist the default value will be returned |
||
| 132 | */ |
||
| 133 | public function getValue($app, $key, $default = null) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * check if a key is set in the appconfig |
||
| 145 | * |
||
| 146 | * @param string $app |
||
| 147 | * @param string $key |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | public function hasKey($app, $key) { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Sets a value. If the key did not exist before it will be created. |
||
| 158 | * |
||
| 159 | * @param string $app app |
||
| 160 | * @param string $key key |
||
| 161 | * @param string|float|int $value value |
||
| 162 | * @return bool True if the value was inserted or updated, false if the value was the same |
||
| 163 | * @deprecated 8.0.0 use method setAppValue of \OCP\IConfig |
||
| 164 | */ |
||
| 165 | public function setValue($app, $key, $value) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Deletes a key |
||
| 215 | * |
||
| 216 | * @param string $app app |
||
| 217 | * @param string $key key |
||
| 218 | * @return boolean |
||
| 219 | * @deprecated 8.0.0 use method deleteAppValue of \OCP\IConfig |
||
| 220 | */ |
||
| 221 | public function deleteKey($app, $key) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Remove app from appconfig |
||
| 238 | * |
||
| 239 | * @param string $app app |
||
| 240 | * @return boolean |
||
| 241 | * @deprecated 8.0.0 use method deleteAppValue of \OCP\IConfig |
||
| 242 | * |
||
| 243 | * Removes all keys in appconfig belonging to the app. |
||
| 244 | */ |
||
| 245 | View Code Duplication | public function deleteApp($app) { |
|
| 257 | |||
| 258 | /** |
||
| 259 | * get multiple values, either the app or key can be used as wildcard by setting it to false |
||
| 260 | * |
||
| 261 | * @param string|false $app |
||
| 262 | * @param string|false $key |
||
| 263 | * @return array|false |
||
| 264 | */ |
||
| 265 | public function getValues($app, $key) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * get all values of the app or and filters out sensitive data |
||
| 285 | * |
||
| 286 | * @param string $app |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | public function getFilteredValues($app) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Load all the app config values |
||
| 303 | */ |
||
| 304 | protected function loadConfigValues() { |
||
| 329 | } |
||
| 330 |
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.