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 Util 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 Util, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class Util { |
||
| 39 | const HEADER_START = 'HBEGIN'; |
||
| 40 | const HEADER_END = 'HEND'; |
||
| 41 | const HEADER_PADDING_CHAR = '-'; |
||
| 42 | |||
| 43 | const HEADER_ENCRYPTION_MODULE_KEY = 'oc_encryption_module'; |
||
| 44 | |||
| 45 | const ID = 'OC_DEFAULT_MODULE'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * block size will always be 8192 for a PHP stream |
||
| 49 | * @see https://bugs.php.net/bug.php?id=21641 |
||
| 50 | * @var integer |
||
| 51 | */ |
||
| 52 | protected $headerSize = 8192; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * block size will always be 8192 for a PHP stream |
||
| 56 | * @see https://bugs.php.net/bug.php?id=21641 |
||
| 57 | * @var integer |
||
| 58 | */ |
||
| 59 | protected $blockSize = 8192; |
||
| 60 | |||
| 61 | /** @var View */ |
||
| 62 | protected $rootView; |
||
| 63 | |||
| 64 | /** @var array */ |
||
| 65 | protected $ocHeaderKeys; |
||
| 66 | |||
| 67 | /** @var \OC\User\Manager */ |
||
| 68 | protected $userManager; |
||
| 69 | |||
| 70 | /** @var IConfig */ |
||
| 71 | protected $config; |
||
| 72 | |||
| 73 | /** @var array paths excluded from encryption */ |
||
| 74 | protected $excludedPaths; |
||
| 75 | |||
| 76 | /** @var \OC\Group\Manager $manager */ |
||
| 77 | protected $groupManager; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * |
||
| 81 | * @param View $rootView |
||
| 82 | * @param \OC\User\Manager $userManager |
||
| 83 | * @param \OC\Group\Manager $groupManager |
||
| 84 | * @param IConfig $config |
||
| 85 | */ |
||
| 86 | public function __construct( |
||
| 107 | |||
| 108 | /** |
||
| 109 | * read encryption module ID from header |
||
| 110 | * |
||
| 111 | * @param array $header |
||
| 112 | * @return string |
||
| 113 | * @throws ModuleDoesNotExistsException |
||
| 114 | */ |
||
| 115 | public function getEncryptionModuleId(array $header = null) { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * create header for encrypted file |
||
| 136 | * |
||
| 137 | * @param array $headerData |
||
| 138 | * @param IEncryptionModule $encryptionModule |
||
| 139 | * @return string |
||
| 140 | * @throws EncryptionHeaderToLargeException if header has to many arguments |
||
| 141 | * @throws EncryptionHeaderKeyExistsException if header key is already in use |
||
| 142 | */ |
||
| 143 | public function createHeader(array $headerData, IEncryptionModule $encryptionModule) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * go recursively through a dir and collect all files and sub files. |
||
| 164 | * |
||
| 165 | * @param string $dir relative to the users files folder |
||
| 166 | * @return array with list of files relative to the users files folder |
||
| 167 | */ |
||
| 168 | public function getAllFiles($dir) { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * check if it is a file uploaded by the user stored in data/user/files |
||
| 190 | * or a metadata file |
||
| 191 | * |
||
| 192 | * @param string $path relative to the data/ folder |
||
| 193 | * @return boolean |
||
| 194 | */ |
||
| 195 | public function isFile($path) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * return size of encryption header |
||
| 205 | * |
||
| 206 | * @return integer |
||
| 207 | */ |
||
| 208 | public function getHeaderSize() { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * return size of block read by a PHP stream |
||
| 214 | * |
||
| 215 | * @return integer |
||
| 216 | */ |
||
| 217 | public function getBlockSize() { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * get the owner and the path for the file relative to the owners files folder |
||
| 223 | * |
||
| 224 | * @param string $path |
||
| 225 | * @return array |
||
| 226 | * @throws \BadMethodCallException |
||
| 227 | */ |
||
| 228 | public function getUidAndFilename($path) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Remove .path extension from a file path |
||
| 247 | * @param string $path Path that may identify a .part file |
||
| 248 | * @return string File path without .part extension |
||
| 249 | * @note this is needed for reusing keys |
||
| 250 | */ |
||
| 251 | View Code Duplication | public function stripPartialFileExtension($path) { |
|
| 269 | |||
| 270 | public function getUserWithAccessToMountPoint($users, $groups) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * check if the file is stored on a system wide mount point |
||
| 292 | * @param string $path relative to /data/user with leading '/' |
||
| 293 | * @param string $uid |
||
| 294 | * @return boolean |
||
| 295 | */ |
||
| 296 | public function isSystemWideMountPoint($path, $uid) { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * check if mount point is applicable to user |
||
| 312 | * |
||
| 313 | * @param array $mount contains $mount['applicable']['users'], $mount['applicable']['groups'] |
||
| 314 | * @param string $uid |
||
| 315 | * @return boolean |
||
| 316 | */ |
||
| 317 | private function isMountPointApplicableToUser($mount, $uid) { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * check if it is a path which is excluded by ownCloud from encryption |
||
| 335 | * |
||
| 336 | * @param string $path |
||
| 337 | * @return boolean |
||
| 338 | */ |
||
| 339 | public function isExcluded($path) { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * check if recovery key is enabled for user |
||
| 371 | * |
||
| 372 | * @param string $uid |
||
| 373 | * @return boolean |
||
| 374 | */ |
||
| 375 | public function recoveryEnabled($uid) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * set new key storage root |
||
| 383 | * |
||
| 384 | * @param string $root new key store root relative to the data folder |
||
| 385 | */ |
||
| 386 | public function setKeyStorageRoot($root) { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * get key storage root |
||
| 392 | * |
||
| 393 | * @return string key storage root |
||
| 394 | */ |
||
| 395 | public function getKeyStorageRoot() { |
||
| 398 | } |
||
| 399 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.