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:
Complex classes like OC_Mount_Config often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use OC_Mount_Config, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | class OC_Mount_Config { |
||
|
|
|||
| 49 | // TODO: make this class non-static and give it a proper namespace |
||
| 50 | |||
| 51 | const MOUNT_TYPE_GLOBAL = 'global'; |
||
| 52 | const MOUNT_TYPE_GROUP = 'group'; |
||
| 53 | const MOUNT_TYPE_USER = 'user'; |
||
| 54 | const MOUNT_TYPE_PERSONAL = 'personal'; |
||
| 55 | |||
| 56 | // whether to skip backend test (for unit tests, as this static class is not mockable) |
||
| 57 | public static $skipTest = false; |
||
| 58 | |||
| 59 | /** @var Application */ |
||
| 60 | public static $app; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param string $class |
||
| 64 | * @param array $definition |
||
| 65 | * @return bool |
||
| 66 | * @deprecated 8.2.0 use \OCA\Files_External\Service\BackendService::registerBackend() |
||
| 67 | */ |
||
| 68 | public static function registerBackend($class, $definition) { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Returns the mount points for the given user. |
||
| 79 | * The mount point is relative to the data directory. |
||
| 80 | * |
||
| 81 | * @param string $uid user |
||
| 82 | * @return array of mount point string as key, mountpoint config as value |
||
| 83 | * |
||
| 84 | * @deprecated 8.2.0 use UserGlobalStoragesService::getStorages() and UserStoragesService::getStorages() |
||
| 85 | */ |
||
| 86 | public static function getAbsoluteMountPoints($uid) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Get the system mount points |
||
| 123 | * |
||
| 124 | * @return array |
||
| 125 | * |
||
| 126 | * @deprecated 8.2.0 use GlobalStoragesService::getStorages() |
||
| 127 | */ |
||
| 128 | View Code Duplication | public static function getSystemMountPoints() { |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Get the personal mount points of the current user |
||
| 141 | * |
||
| 142 | * @return array |
||
| 143 | * |
||
| 144 | * @deprecated 8.2.0 use UserStoragesService::getStorages() |
||
| 145 | */ |
||
| 146 | View Code Duplication | public static function getPersonalMountPoints() { |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Convert a StorageConfig to the legacy mountPoints array format |
||
| 159 | * There's a lot of extra information in here, to satisfy all of the legacy functions |
||
| 160 | * |
||
| 161 | * @param StorageConfig $storage |
||
| 162 | * @param bool $isPersonal |
||
| 163 | * @return array |
||
| 164 | */ |
||
| 165 | private static function prepareMountPointEntry(StorageConfig $storage, $isPersonal) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * fill in the correct values for $user |
||
| 192 | * |
||
| 193 | * @param string $user user value |
||
| 194 | * @param string|array $input |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | public static function setUserVars($user, $input) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Test connecting using the given backend configuration |
||
| 214 | * |
||
| 215 | * @param string $class backend class name |
||
| 216 | * @param array $options backend configuration options |
||
| 217 | * @param boolean $isPersonal |
||
| 218 | * @return int see self::STATUS_* |
||
| 219 | * @throws Exception |
||
| 220 | */ |
||
| 221 | public static function getBackendStatus($class, $options, $isPersonal, $testOnly = true) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Read the mount points in the config file into an array |
||
| 253 | * |
||
| 254 | * @param string|null $user If not null, personal for $user, otherwise system |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | public static function readData($user = null) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Get backend dependency message |
||
| 276 | * TODO: move into AppFramework along with templates |
||
| 277 | * |
||
| 278 | * @param Backend[] $backends |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | public static function dependencyMessage($backends) { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Returns a dependency missing message |
||
| 308 | * |
||
| 309 | * @param \OCP\IL10N $l |
||
| 310 | * @param string $module |
||
| 311 | * @param string $backend |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | private static function getSingleDependencyMessage(\OCP\IL10N $l, $module, $backend) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Encrypt passwords in the given config options |
||
| 327 | * |
||
| 328 | * @param array $options mount options |
||
| 329 | * @return array updated options |
||
| 330 | */ |
||
| 331 | View Code Duplication | public static function encryptPasswords($options) { |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Decrypt passwords in the given config options |
||
| 343 | * |
||
| 344 | * @param array $options mount options |
||
| 345 | * @return array updated options |
||
| 346 | */ |
||
| 347 | View Code Duplication | public static function decryptPasswords($options) { |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Encrypt a single password |
||
| 358 | * |
||
| 359 | * @param string $password plain text password |
||
| 360 | * @return string encrypted password |
||
| 361 | */ |
||
| 362 | private static function encryptPassword($password) { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Decrypts a single password |
||
| 371 | * |
||
| 372 | * @param string $encryptedPassword encrypted password |
||
| 373 | * @return string plain text password |
||
| 374 | */ |
||
| 375 | private static function decryptPassword($encryptedPassword) { |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Returns the encryption cipher |
||
| 386 | * |
||
| 387 | * @return AES |
||
| 388 | */ |
||
| 389 | private static function getCipher() { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Computes a hash based on the given configuration. |
||
| 397 | * This is mostly used to find out whether configurations |
||
| 398 | * are the same. |
||
| 399 | * |
||
| 400 | * @param array $config |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | public static function makeConfigHash($config) { |
||
| 416 | } |
||
| 417 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.