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_Helper 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_Helper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | class OC_Helper { |
||
| 50 | private static $templateManager; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Creates an absolute url for public use |
||
| 54 | * @param string $service id |
||
| 55 | * @param bool $add_slash |
||
| 56 | * @return string the url |
||
| 57 | * |
||
| 58 | * Returns a absolute url to the given service. |
||
| 59 | */ |
||
| 60 | public static function linkToPublic($service, $add_slash = false) { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Make a human file size |
||
| 71 | * @param int $bytes file size in bytes |
||
| 72 | * @return string a human readable file size |
||
| 73 | * |
||
| 74 | * Makes 2048 to 2 kB. |
||
| 75 | */ |
||
| 76 | public static function humanFileSize($bytes) { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Make a php file size |
||
| 106 | * @param int $bytes file size in bytes |
||
| 107 | * @return string a php parseable file size |
||
| 108 | * |
||
| 109 | * Makes 2048 to 2k and 2^41 to 2048G |
||
| 110 | */ |
||
| 111 | public static function phpFileSize($bytes) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Make a computer file size |
||
| 132 | * @param string $str file size in human readable format |
||
| 133 | * @return float a file size in bytes |
||
| 134 | * |
||
| 135 | * Makes 2kB to 2048. |
||
| 136 | * |
||
| 137 | * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418 |
||
| 138 | */ |
||
| 139 | public static function computerFileSize($str) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Recursive copying of folders |
||
| 174 | * @param string $src source folder |
||
| 175 | * @param string $dest target folder |
||
| 176 | * |
||
| 177 | */ |
||
| 178 | static function copyr($src, $dest) { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Recursive deletion of folders |
||
| 196 | * @param string $dir path to the folder |
||
| 197 | * @param bool $deleteSelf if set to false only the content of the folder will be deleted |
||
| 198 | * @return bool |
||
| 199 | */ |
||
| 200 | static function rmdirr($dir, $deleteSelf = true) { |
||
| 201 | if (is_dir($dir)) { |
||
| 202 | $files = new RecursiveIteratorIterator( |
||
| 203 | new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), |
||
| 204 | RecursiveIteratorIterator::CHILD_FIRST |
||
| 205 | ); |
||
| 206 | |||
| 207 | foreach ($files as $fileInfo) { |
||
| 208 | /** @var SplFileInfo $fileInfo */ |
||
| 209 | if ($fileInfo->isDir()) { |
||
| 210 | rmdir($fileInfo->getRealPath()); |
||
| 211 | } else { |
||
| 212 | unlink($fileInfo->getRealPath()); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | if ($deleteSelf) { |
||
| 216 | rmdir($dir); |
||
| 217 | } |
||
| 218 | } elseif (file_exists($dir)) { |
||
| 219 | if ($deleteSelf) { |
||
| 220 | unlink($dir); |
||
| 221 | } |
||
| 222 | } |
||
| 223 | if (!$deleteSelf) { |
||
| 224 | return true; |
||
| 225 | } |
||
| 226 | |||
| 227 | return !file_exists($dir); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @return \OC\Files\Type\TemplateManager |
||
| 232 | */ |
||
| 233 | static public function getFileTemplateManager() { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * detect if a given program is found in the search PATH |
||
| 242 | * |
||
| 243 | * @param string $name |
||
| 244 | * @param bool $path |
||
| 245 | * @internal param string $program name |
||
| 246 | * @internal param string $optional search path, defaults to $PATH |
||
| 247 | * @return bool true if executable program found in path |
||
| 248 | */ |
||
| 249 | public static function canExecute($name, $path = false) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * copy the contents of one stream to another |
||
| 287 | * |
||
| 288 | * @param resource $source |
||
| 289 | * @param resource $target |
||
| 290 | * @return array the number of bytes copied and result |
||
| 291 | */ |
||
| 292 | public static function streamCopy($source, $target) { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Adds a suffix to the name in case the file exists |
||
| 320 | * |
||
| 321 | * @param string $path |
||
| 322 | * @param string $filename |
||
| 323 | * @return string |
||
| 324 | */ |
||
| 325 | public static function buildNotExistingFileName($path, $filename) { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Adds a suffix to the name in case the file exists |
||
| 332 | * |
||
| 333 | * @param string $path |
||
| 334 | * @param string $filename |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | public static function buildNotExistingFileNameForView($path, $filename, \OC\Files\View $view) { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Checks if $sub is a subdirectory of $parent |
||
| 379 | * |
||
| 380 | * @param string $sub |
||
| 381 | * @param string $parent |
||
| 382 | * @return bool |
||
| 383 | */ |
||
| 384 | public static function isSubDirectory($sub, $parent) { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
||
| 405 | * |
||
| 406 | * @param array $input The array to work on |
||
| 407 | * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
||
| 408 | * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
||
| 409 | * @return array |
||
| 410 | * |
||
| 411 | * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
||
| 412 | * based on http://www.php.net/manual/en/function.array-change-key-case.php#107715 |
||
| 413 | * |
||
| 414 | */ |
||
| 415 | public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * performs a search in a nested array |
||
| 426 | * @param array $haystack the array to be searched |
||
| 427 | * @param string $needle the search string |
||
| 428 | * @param string $index optional, only search this key name |
||
| 429 | * @return mixed the key of the matching field, otherwise false |
||
| 430 | * |
||
| 431 | * performs a search in a nested array |
||
| 432 | * |
||
| 433 | * taken from http://www.php.net/manual/en/function.array-search.php#97645 |
||
| 434 | */ |
||
| 435 | public static function recursiveArraySearch($haystack, $needle, $index = null) { |
||
| 449 | |||
| 450 | /** |
||
| 451 | * calculates the maximum upload size respecting system settings, free space and user quota |
||
| 452 | * |
||
| 453 | * @param string $dir the current folder where the user currently operates |
||
| 454 | * @param int $freeSpace the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
||
| 455 | * @return int number of bytes representing |
||
| 456 | */ |
||
| 457 | public static function maxUploadFilesize($dir, $freeSpace = null) { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Calculate free space left within user quota |
||
| 466 | * |
||
| 467 | * @param string $dir the current folder where the user currently operates |
||
| 468 | * @return int number of bytes representing |
||
| 469 | */ |
||
| 470 | public static function freeSpace($dir) { |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Calculate PHP upload limit |
||
| 482 | * |
||
| 483 | * @return int PHP upload file size limit |
||
| 484 | */ |
||
| 485 | public static function uploadLimit() { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Checks if a function is available |
||
| 500 | * |
||
| 501 | * @param string $function_name |
||
| 502 | * @return bool |
||
| 503 | */ |
||
| 504 | public static function is_function_enabled($function_name) { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Try to find a program |
||
| 524 | * Note: currently windows is not supported |
||
| 525 | * |
||
| 526 | * @param string $program |
||
| 527 | * @return null|string |
||
| 528 | */ |
||
| 529 | public static function findBinaryPath($program) { |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Calculate the disc space for the given path |
||
| 560 | * |
||
| 561 | * @param string $path |
||
| 562 | * @param \OCP\Files\FileInfo $rootInfo (optional) |
||
| 563 | * @return array |
||
| 564 | * @throws \OCP\Files\NotFoundException |
||
| 565 | */ |
||
| 566 | public static function getStorageInfo($path, $rootInfo = null) { |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Get storage info including all mount points and quota |
||
| 636 | * |
||
| 637 | * @return array |
||
| 638 | */ |
||
| 639 | private static function getGlobalStorageInfo() { |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Returns whether the config file is set manually to read-only |
||
| 667 | * @return bool |
||
| 668 | */ |
||
| 669 | public static function isReadOnlyConfigEnabled() { |
||
| 672 | } |
||
| 673 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.