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 |
||
| 37 | class Util { |
||
| 38 | |||
| 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 | /** |
||
| 46 | * block size will always be 8192 for a PHP stream |
||
| 47 | * @see https://bugs.php.net/bug.php?id=21641 |
||
| 48 | * @var integer |
||
| 49 | */ |
||
| 50 | protected $headerSize = 8192; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * block size will always be 8192 for a PHP stream |
||
| 54 | * @see https://bugs.php.net/bug.php?id=21641 |
||
| 55 | * @var integer |
||
| 56 | */ |
||
| 57 | protected $blockSize = 8192; |
||
| 58 | |||
| 59 | /** @var View */ |
||
| 60 | protected $rootView; |
||
| 61 | |||
| 62 | /** @var array */ |
||
| 63 | protected $ocHeaderKeys; |
||
| 64 | |||
| 65 | /** @var \OC\User\Manager */ |
||
| 66 | protected $userManager; |
||
| 67 | |||
| 68 | /** @var IConfig */ |
||
| 69 | protected $config; |
||
| 70 | |||
| 71 | /** @var array paths excluded from encryption */ |
||
| 72 | protected $excludedPaths; |
||
| 73 | |||
| 74 | /** @var \OC\Group\Manager $manager */ |
||
| 75 | protected $groupManager; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * |
||
| 79 | * @param View $rootView |
||
| 80 | * @param \OC\User\Manager $userManager |
||
| 81 | * @param \OC\Group\Manager $groupManager |
||
| 82 | * @param IConfig $config |
||
| 83 | */ |
||
| 84 | public function __construct( |
||
| 102 | |||
| 103 | /** |
||
| 104 | * read encryption module ID from header |
||
| 105 | * |
||
| 106 | * @param array $header |
||
| 107 | * @return string |
||
| 108 | * @throws ModuleDoesNotExistsException |
||
| 109 | */ |
||
| 110 | public function getEncryptionModuleId(array $header = null) { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * create header for encrypted file |
||
| 131 | * |
||
| 132 | * @param array $headerData |
||
| 133 | * @param IEncryptionModule $encryptionModule |
||
| 134 | * @return string |
||
| 135 | * @throws EncryptionHeaderToLargeException if header has to many arguments |
||
| 136 | * @throws EncryptionHeaderKeyExistsException if header key is already in use |
||
| 137 | */ |
||
| 138 | public function createHeader(array $headerData, IEncryptionModule $encryptionModule) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * go recursively through a dir and collect all files and sub files. |
||
| 159 | * |
||
| 160 | * @param string $dir relative to the users files folder |
||
| 161 | * @return array with list of files relative to the users files folder |
||
| 162 | */ |
||
| 163 | public function getAllFiles($dir) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * check if it is a file uploaded by the user stored in data/user/files |
||
| 186 | * or a metadata file |
||
| 187 | * |
||
| 188 | * @param string $path relative to the data/ folder |
||
| 189 | * @return boolean |
||
| 190 | */ |
||
| 191 | public function isFile($path) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * return size of encryption header |
||
| 201 | * |
||
| 202 | * @return integer |
||
| 203 | */ |
||
| 204 | public function getHeaderSize() { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * return size of block read by a PHP stream |
||
| 210 | * |
||
| 211 | * @return integer |
||
| 212 | */ |
||
| 213 | public function getBlockSize() { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * get the owner and the path for the file relative to the owners files folder |
||
| 219 | * |
||
| 220 | * @param string $path |
||
| 221 | * @return array |
||
| 222 | * @throws \BadMethodCallException |
||
| 223 | */ |
||
| 224 | public function getUidAndFilename($path) { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Remove .path extension from a file path |
||
| 245 | * @param string $path Path that may identify a .part file |
||
| 246 | * @return string File path without .part extension |
||
| 247 | * @note this is needed for reusing keys |
||
| 248 | */ |
||
| 249 | public function stripPartialFileExtension($path) { |
||
| 269 | |||
| 270 | public function getUserWithAccessToMountPoint($users, $groups) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * check if the file is stored on a system wide mount point |
||
| 296 | * @param string $path relative to /data/user with leading '/' |
||
| 297 | * @param string $uid |
||
| 298 | * @return boolean |
||
| 299 | */ |
||
| 300 | public function isSystemWideMountPoint($path, $uid) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * check if mount point is applicable to user |
||
| 316 | * |
||
| 317 | * @param array $mount contains $mount['applicable']['users'], $mount['applicable']['groups'] |
||
| 318 | * @param string $uid |
||
| 319 | * @return boolean |
||
| 320 | */ |
||
| 321 | private function isMountPointApplicableToUser($mount, $uid) { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * check if it is a path which is excluded by ownCloud from encryption |
||
| 339 | * |
||
| 340 | * @param string $path |
||
| 341 | * @return boolean |
||
| 342 | */ |
||
| 343 | public function isExcluded($path) { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * check if recovery key is enabled for user |
||
| 377 | * |
||
| 378 | * @param string $uid |
||
| 379 | * @return boolean |
||
| 380 | */ |
||
| 381 | public function recoveryEnabled($uid) { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * set new key storage root |
||
| 389 | * |
||
| 390 | * @param string $root new key store root relative to the data folder |
||
| 391 | */ |
||
| 392 | public function setKeyStorageRoot($root) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * get key storage root |
||
| 398 | * |
||
| 399 | * @return string key storage root |
||
| 400 | */ |
||
| 401 | public function getKeyStorageRoot() { |
||
| 404 | |||
| 405 | } |
||
| 406 |
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.