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 |
||
| 36 | class Util { |
||
| 37 | |||
| 38 | const HEADER_START = 'HBEGIN'; |
||
| 39 | const HEADER_END = 'HEND'; |
||
| 40 | const HEADER_PADDING_CHAR = '-'; |
||
| 41 | |||
| 42 | const HEADER_ENCRYPTION_MODULE_KEY = 'oc_encryption_module'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * block size will always be 8192 for a PHP stream |
||
| 46 | * @see https://bugs.php.net/bug.php?id=21641 |
||
| 47 | * @var integer |
||
| 48 | */ |
||
| 49 | protected $headerSize = 8192; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * block size will always be 8192 for a PHP stream |
||
| 53 | * @see https://bugs.php.net/bug.php?id=21641 |
||
| 54 | * @var integer |
||
| 55 | */ |
||
| 56 | protected $blockSize = 8192; |
||
| 57 | |||
| 58 | /** @var View */ |
||
| 59 | protected $rootView; |
||
| 60 | |||
| 61 | /** @var array */ |
||
| 62 | protected $ocHeaderKeys; |
||
| 63 | |||
| 64 | /** @var \OC\User\Manager */ |
||
| 65 | protected $userManager; |
||
| 66 | |||
| 67 | /** @var IConfig */ |
||
| 68 | protected $config; |
||
| 69 | |||
| 70 | /** @var array paths excluded from encryption */ |
||
| 71 | protected $excludedPaths; |
||
| 72 | |||
| 73 | /** @var \OC\Group\Manager $manager */ |
||
| 74 | protected $groupManager; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * |
||
| 78 | * @param View $rootView |
||
| 79 | * @param \OC\User\Manager $userManager |
||
| 80 | * @param \OC\Group\Manager $groupManager |
||
| 81 | * @param IConfig $config |
||
| 82 | */ |
||
| 83 | public function __construct( |
||
| 100 | 189 | ||
| 101 | 189 | /** |
|
| 102 | * read encryption module ID from header |
||
| 103 | * |
||
| 104 | * @param array $header |
||
| 105 | * @return string |
||
| 106 | * @throws ModuleDoesNotExistsException |
||
| 107 | */ |
||
| 108 | public function getEncryptionModuleId(array $header = null) { |
||
| 126 | 74 | ||
| 127 | /** |
||
| 128 | * create header for encrypted file |
||
| 129 | * |
||
| 130 | * @param array $headerData |
||
| 131 | * @param IEncryptionModule $encryptionModule |
||
| 132 | * @return string |
||
| 133 | * @throws EncryptionHeaderToLargeException if header has to many arguments |
||
| 134 | * @throws EncryptionHeaderKeyExistsException if header key is already in use |
||
| 135 | */ |
||
| 136 | public function createHeader(array $headerData, IEncryptionModule $encryptionModule) { |
||
| 154 | 16 | ||
| 155 | /** |
||
| 156 | * go recursively through a dir and collect all files and sub files. |
||
| 157 | * |
||
| 158 | * @param string $dir relative to the users files folder |
||
| 159 | * @return array with list of files relative to the users files folder |
||
| 160 | */ |
||
| 161 | public function getAllFiles($dir) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * check if it is a file uploaded by the user stored in data/user/files |
||
| 184 | * or a metadata file |
||
| 185 | * |
||
| 186 | * @param string $path relative to the data/ folder |
||
| 187 | * @return boolean |
||
| 188 | */ |
||
| 189 | public function isFile($path) { |
||
| 196 | 5 | ||
| 197 | /** |
||
| 198 | * return size of encryption header |
||
| 199 | * |
||
| 200 | * @return integer |
||
| 201 | */ |
||
| 202 | public function getHeaderSize() { |
||
| 205 | 72 | ||
| 206 | /** |
||
| 207 | * return size of block read by a PHP stream |
||
| 208 | * |
||
| 209 | * @return integer |
||
| 210 | */ |
||
| 211 | public function getBlockSize() { |
||
| 214 | 14 | ||
| 215 | /** |
||
| 216 | * get the owner and the path for the file relative to the owners files folder |
||
| 217 | * |
||
| 218 | * @param string $path |
||
| 219 | * @return array |
||
| 220 | * @throws \BadMethodCallException |
||
| 221 | */ |
||
| 222 | public function getUidAndFilename($path) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Remove .path extension from a file path |
||
| 243 | * @param string $path Path that may identify a .part file |
||
| 244 | * @return string File path without .part extension |
||
| 245 | * @note this is needed for reusing keys |
||
| 246 | */ |
||
| 247 | public function stripPartialFileExtension($path) { |
||
| 267 | |||
| 268 | public function getUserWithAccessToMountPoint($users, $groups) { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * check if the file is stored on a system wide mount point |
||
| 284 | * @param string $path relative to /data/user with leading '/' |
||
| 285 | * @param string $uid |
||
| 286 | * @return boolean |
||
| 287 | */ |
||
| 288 | public function isSystemWideMountPoint($path, $uid) { |
||
| 301 | 5 | ||
| 302 | /** |
||
| 303 | * check if mount point is applicable to user |
||
| 304 | * |
||
| 305 | * @param array $mount contains $mount['applicable']['users'], $mount['applicable']['groups'] |
||
| 306 | * @param string $uid |
||
| 307 | * @return boolean |
||
| 308 | */ |
||
| 309 | private function isMountPointApplicableToUser($mount, $uid) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * check if it is a path which is excluded by ownCloud from encryption |
||
| 327 | * |
||
| 328 | * @param string $path |
||
| 329 | * @return boolean |
||
| 330 | */ |
||
| 331 | public function isExcluded($path) { |
||
| 362 | 7 | ||
| 363 | /** |
||
| 364 | * check if recovery key is enabled for user |
||
| 365 | * |
||
| 366 | * @param string $uid |
||
| 367 | * @return boolean |
||
| 368 | */ |
||
| 369 | public function recoveryEnabled($uid) { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * set new key storage root |
||
| 377 | * |
||
| 378 | * @param string $root new key store root relative to the data folder |
||
| 379 | */ |
||
| 380 | public function setKeyStorageRoot($root) { |
||
| 383 | |||
| 384 | /** |
||
| 385 | * get key storage root |
||
| 386 | * |
||
| 387 | * @return string key storage root |
||
| 388 | */ |
||
| 389 | public function getKeyStorageRoot() { |
||
| 392 | 15 | ||
| 393 | } |
||
| 394 |
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.