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 |
||
27 | class OC_Helper { |
||
28 | private static $mimetypeIcons = array(); |
||
29 | private static $mimetypeDetector; |
||
30 | private static $templateManager; |
||
31 | /** @var string[] */ |
||
32 | private static $mimeTypeAlias = array( |
||
33 | 'application/octet-stream' => 'file', // use file icon as fallback |
||
34 | |||
35 | 'application/illustrator' => 'image/vector', |
||
36 | 'application/postscript' => 'image/vector', |
||
37 | 'image/svg+xml' => 'image/vector', |
||
38 | |||
39 | 'application/coreldraw' => 'image', |
||
40 | 'application/x-gimp' => 'image', |
||
41 | 'application/x-photoshop' => 'image', |
||
42 | |||
43 | 'application/x-font-ttf' => 'font', |
||
44 | 'application/font-woff' => 'font', |
||
45 | 'application/vnd.ms-fontobject' => 'font', |
||
46 | |||
47 | 'application/json' => 'text/code', |
||
48 | 'application/x-perl' => 'text/code', |
||
49 | 'application/x-php' => 'text/code', |
||
50 | 'text/x-shellscript' => 'text/code', |
||
51 | 'application/xml' => 'text/html', |
||
52 | 'text/css' => 'text/code', |
||
53 | 'application/x-tex' => 'text', |
||
54 | |||
55 | 'application/x-compressed' => 'package/x-generic', |
||
56 | 'application/x-7z-compressed' => 'package/x-generic', |
||
57 | 'application/x-deb' => 'package/x-generic', |
||
58 | 'application/x-gzip' => 'package/x-generic', |
||
59 | 'application/x-rar-compressed' => 'package/x-generic', |
||
60 | 'application/x-tar' => 'package/x-generic', |
||
61 | 'application/vnd.android.package-archive' => 'package/x-generic', |
||
62 | 'application/zip' => 'package/x-generic', |
||
63 | |||
64 | 'application/msword' => 'x-office/document', |
||
65 | 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'x-office/document', |
||
66 | 'application/vnd.openxmlformats-officedocument.wordprocessingml.template' => 'x-office/document', |
||
67 | 'application/vnd.ms-word.document.macroEnabled.12' => 'x-office/document', |
||
68 | 'application/vnd.ms-word.template.macroEnabled.12' => 'x-office/document', |
||
69 | 'application/vnd.oasis.opendocument.text' => 'x-office/document', |
||
70 | 'application/vnd.oasis.opendocument.text-template' => 'x-office/document', |
||
71 | 'application/vnd.oasis.opendocument.text-web' => 'x-office/document', |
||
72 | 'application/vnd.oasis.opendocument.text-master' => 'x-office/document', |
||
73 | |||
74 | 'application/mspowerpoint' => 'x-office/presentation', |
||
75 | 'application/vnd.ms-powerpoint' => 'x-office/presentation', |
||
76 | 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'x-office/presentation', |
||
77 | 'application/vnd.openxmlformats-officedocument.presentationml.template' => 'x-office/presentation', |
||
78 | 'application/vnd.openxmlformats-officedocument.presentationml.slideshow' => 'x-office/presentation', |
||
79 | 'application/vnd.ms-powerpoint.addin.macroEnabled.12' => 'x-office/presentation', |
||
80 | 'application/vnd.ms-powerpoint.presentation.macroEnabled.12' => 'x-office/presentation', |
||
81 | 'application/vnd.ms-powerpoint.template.macroEnabled.12' => 'x-office/presentation', |
||
82 | 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12' => 'x-office/presentation', |
||
83 | 'application/vnd.oasis.opendocument.presentation' => 'x-office/presentation', |
||
84 | 'application/vnd.oasis.opendocument.presentation-template' => 'x-office/presentation', |
||
85 | |||
86 | 'application/msexcel' => 'x-office/spreadsheet', |
||
87 | 'application/vnd.ms-excel' => 'x-office/spreadsheet', |
||
88 | 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'x-office/spreadsheet', |
||
89 | 'application/vnd.openxmlformats-officedocument.spreadsheetml.template' => 'x-office/spreadsheet', |
||
90 | 'application/vnd.ms-excel.sheet.macroEnabled.12' => 'x-office/spreadsheet', |
||
91 | 'application/vnd.ms-excel.template.macroEnabled.12' => 'x-office/spreadsheet', |
||
92 | 'application/vnd.ms-excel.addin.macroEnabled.12' => 'x-office/spreadsheet', |
||
93 | 'application/vnd.ms-excel.sheet.binary.macroEnabled.12' => 'x-office/spreadsheet', |
||
94 | 'application/vnd.oasis.opendocument.spreadsheet' => 'x-office/spreadsheet', |
||
95 | 'application/vnd.oasis.opendocument.spreadsheet-template' => 'x-office/spreadsheet', |
||
96 | 'text/csv' => 'x-office/spreadsheet', |
||
97 | |||
98 | 'application/msaccess' => 'database', |
||
99 | ); |
||
100 | |||
101 | /** |
||
102 | * Creates an url using a defined route |
||
103 | * @param string $route |
||
104 | * @param array $parameters |
||
105 | * @return |
||
106 | * @internal param array $args with param=>value, will be appended to the returned url |
||
107 | * @return string the url |
||
108 | * |
||
109 | * Returns a url to the given app and file. |
||
110 | */ |
||
111 | public static function linkToRoute($route, $parameters = array()) { |
||
114 | |||
115 | /** |
||
116 | * Creates an url |
||
117 | * @param string $app app |
||
118 | * @param string $file file |
||
119 | * @param array $args array with param=>value, will be appended to the returned url |
||
120 | * The value of $args will be urlencoded |
||
121 | * @return string the url |
||
122 | * |
||
123 | * Returns a url to the given app and file. |
||
124 | */ |
||
125 | public static function linkTo( $app, $file, $args = array() ) { |
||
128 | |||
129 | /** |
||
130 | * @param string $key |
||
131 | * @return string url to the online documentation |
||
132 | */ |
||
133 | public static function linkToDocs($key) { |
||
136 | |||
137 | /** |
||
138 | * Creates an absolute url |
||
139 | * @param string $app app |
||
140 | * @param string $file file |
||
141 | * @param array $args array with param=>value, will be appended to the returned url |
||
142 | * The value of $args will be urlencoded |
||
143 | * @return string the url |
||
144 | * |
||
145 | * Returns a absolute url to the given app and file. |
||
146 | */ |
||
147 | public static function linkToAbsolute($app, $file, $args = array()) { |
||
152 | |||
153 | /** |
||
154 | * Makes an $url absolute |
||
155 | * @param string $url the url |
||
156 | * @return string the absolute url |
||
157 | * |
||
158 | * Returns a absolute url to the given app and file. |
||
159 | */ |
||
160 | public static function makeURLAbsolute($url) { |
||
163 | |||
164 | /** |
||
165 | * Creates an url for remote use |
||
166 | * @param string $service id |
||
167 | * @return string the url |
||
168 | * |
||
169 | * Returns a url to the given service. |
||
170 | */ |
||
171 | public static function linkToRemoteBase($service) { |
||
174 | |||
175 | /** |
||
176 | * Creates an absolute url for remote use |
||
177 | * @param string $service id |
||
178 | * @param bool $add_slash |
||
179 | * @return string the url |
||
180 | * |
||
181 | * Returns a absolute url to the given service. |
||
182 | */ |
||
183 | public static function linkToRemote($service, $add_slash = true) { |
||
189 | |||
190 | /** |
||
191 | * Creates an absolute url for public use |
||
192 | * @param string $service id |
||
193 | * @param bool $add_slash |
||
194 | * @return string the url |
||
195 | * |
||
196 | * Returns a absolute url to the given service. |
||
197 | */ |
||
198 | public static function linkToPublic($service, $add_slash = false) { |
||
206 | |||
207 | /** |
||
208 | * Creates path to an image |
||
209 | * @param string $app app |
||
210 | * @param string $image image name |
||
211 | * @return string the url |
||
212 | * |
||
213 | * Returns the path to the image. |
||
214 | */ |
||
215 | public static function imagePath($app, $image) { |
||
218 | |||
219 | /** |
||
220 | * get path to icon of file type |
||
221 | * @param string $mimetype mimetype |
||
222 | * @return string the url |
||
223 | * |
||
224 | * Returns the path to the image of this file type. |
||
225 | */ |
||
226 | public static function mimetypeIcon($mimetype) { |
||
268 | |||
269 | /** |
||
270 | * get path to preview of file |
||
271 | * @param string $path path |
||
272 | * @return string the url |
||
273 | * |
||
274 | * Returns the path to the preview of the file. |
||
275 | */ |
||
276 | public static function previewIcon($path) { |
||
279 | |||
280 | public static function publicPreviewIcon( $path, $token ) { |
||
283 | |||
284 | /** |
||
285 | * shows whether the user has an avatar |
||
286 | * @param string $user username |
||
287 | * @return bool avatar set or not |
||
288 | **/ |
||
289 | public static function userAvatarSet($user) { |
||
298 | |||
299 | /** |
||
300 | * Make a human file size |
||
301 | * @param int $bytes file size in bytes |
||
302 | * @return string a human readable file size |
||
303 | * |
||
304 | * Makes 2048 to 2 kB. |
||
305 | */ |
||
306 | public static function humanFileSize($bytes) { |
||
333 | |||
334 | /** |
||
335 | * Make a php file size |
||
336 | * @param int $bytes file size in bytes |
||
337 | * @return string a php parseable file size |
||
338 | * |
||
339 | * Makes 2048 to 2k and 2^41 to 2048G |
||
340 | */ |
||
341 | public static function phpFileSize($bytes) { |
||
359 | |||
360 | /** |
||
361 | * Make a computer file size |
||
362 | * @param string $str file size in human readable format |
||
363 | * @return int a file size in bytes |
||
364 | * |
||
365 | * Makes 2kB to 2048. |
||
366 | * |
||
367 | * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418 |
||
368 | */ |
||
369 | public static function computerFileSize($str) { |
||
396 | |||
397 | /** |
||
398 | * Recursive copying of folders |
||
399 | * @param string $src source folder |
||
400 | * @param string $dest target folder |
||
401 | * |
||
402 | */ |
||
403 | static function copyr($src, $dest) { |
||
418 | |||
419 | /** |
||
420 | * Recursive deletion of folders |
||
421 | * @param string $dir path to the folder |
||
422 | * @param bool $deleteSelf if set to false only the content of the folder will be deleted |
||
423 | * @return bool |
||
424 | */ |
||
425 | static function rmdirr($dir, $deleteSelf = true) { |
||
454 | |||
455 | /** |
||
456 | * @return \OC\Files\Type\Detection |
||
457 | */ |
||
458 | static public function getMimetypeDetector() { |
||
465 | |||
466 | /** |
||
467 | * @return \OC\Files\Type\TemplateManager |
||
468 | */ |
||
469 | static public function getFileTemplateManager() { |
||
475 | |||
476 | /** |
||
477 | * Try to guess the mimetype based on filename |
||
478 | * |
||
479 | * @param string $path |
||
480 | * @return string |
||
481 | */ |
||
482 | static public function getFileNameMimeType($path) { |
||
485 | |||
486 | /** |
||
487 | * get the mimetype form a local file |
||
488 | * |
||
489 | * @param string $path |
||
490 | * @return string |
||
491 | * does NOT work for ownClouds filesystem, use OC_FileSystem::getMimeType instead |
||
492 | */ |
||
493 | static function getMimeType($path) { |
||
496 | |||
497 | /** |
||
498 | * Get a secure mimetype that won't expose potential XSS. |
||
499 | * |
||
500 | * @param string $mimeType |
||
501 | * @return string |
||
502 | */ |
||
503 | static function getSecureMimeType($mimeType) { |
||
506 | |||
507 | /** |
||
508 | * get the mimetype form a data string |
||
509 | * |
||
510 | * @param string $data |
||
511 | * @return string |
||
512 | */ |
||
513 | static function getStringMimeType($data) { |
||
516 | |||
517 | /** |
||
518 | * Checks $_REQUEST contains a var for the $s key. If so, returns the html-escaped value of this var; otherwise returns the default value provided by $d. |
||
519 | * @param string $s name of the var to escape, if set. |
||
520 | * @param string $d default value. |
||
521 | * @return string the print-safe value. |
||
522 | * |
||
523 | */ |
||
524 | |||
525 | /** |
||
526 | * detect if a given program is found in the search PATH |
||
527 | * |
||
528 | * @param string $name |
||
529 | * @param bool $path |
||
530 | * @internal param string $program name |
||
531 | * @internal param string $optional search path, defaults to $PATH |
||
532 | * @return bool true if executable program found in path |
||
533 | */ |
||
534 | public static function canExecute($name, $path = false) { |
||
569 | |||
570 | /** |
||
571 | * copy the contents of one stream to another |
||
572 | * |
||
573 | * @param resource $source |
||
574 | * @param resource $target |
||
575 | * @return array the number of bytes copied and result |
||
576 | */ |
||
577 | public static function streamCopy($source, $target) { |
||
602 | |||
603 | /** |
||
604 | * create a temporary file with an unique filename |
||
605 | * |
||
606 | * @param string $postfix |
||
607 | * @return string |
||
608 | * @deprecated Use the TempManager instead |
||
609 | * |
||
610 | * temporary files are automatically cleaned up after the script is finished |
||
611 | */ |
||
612 | public static function tmpFile($postfix = '') { |
||
615 | |||
616 | /** |
||
617 | * create a temporary folder with an unique filename |
||
618 | * |
||
619 | * @return string |
||
620 | * @deprecated Use the TempManager instead |
||
621 | * |
||
622 | * temporary files are automatically cleaned up after the script is finished |
||
623 | */ |
||
624 | public static function tmpFolder() { |
||
627 | |||
628 | /** |
||
629 | * Adds a suffix to the name in case the file exists |
||
630 | * |
||
631 | * @param string $path |
||
632 | * @param string $filename |
||
633 | * @return string |
||
634 | */ |
||
635 | public static function buildNotExistingFileName($path, $filename) { |
||
639 | |||
640 | /** |
||
641 | * Adds a suffix to the name in case the file exists |
||
642 | * |
||
643 | * @param string $path |
||
644 | * @param string $filename |
||
645 | * @return string |
||
646 | */ |
||
647 | public static function buildNotExistingFileNameForView($path, $filename, \OC\Files\View $view) { |
||
685 | |||
686 | /** |
||
687 | * Checks if $sub is a subdirectory of $parent |
||
688 | * |
||
689 | * @param string $sub |
||
690 | * @param string $parent |
||
691 | * @return bool |
||
692 | */ |
||
693 | public static function isSubDirectory($sub, $parent) { |
||
711 | |||
712 | /** |
||
713 | * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
||
714 | * |
||
715 | * @param array $input The array to work on |
||
716 | * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
||
717 | * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
||
718 | * @return array |
||
719 | * |
||
720 | * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
||
721 | * based on http://www.php.net/manual/en/function.array-change-key-case.php#107715 |
||
722 | * |
||
723 | */ |
||
724 | public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
||
732 | |||
733 | /** |
||
734 | * replaces a copy of string delimited by the start and (optionally) length parameters with the string given in replacement. |
||
735 | * |
||
736 | * @param string $string |
||
737 | * @param string $replacement The replacement string. |
||
738 | * @param int $start If start is positive, the replacing will begin at the start'th offset into string. If start is negative, the replacing will begin at the start'th character from the end of string. |
||
739 | * @param int $length Length of the part to be replaced |
||
740 | * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
||
741 | * @internal param string $input The input string. .Opposite to the PHP build-in function does not accept an array. |
||
742 | * @return string |
||
743 | */ |
||
744 | public static function mb_substr_replace($string, $replacement, $start, $length = null, $encoding = 'UTF-8') { |
||
753 | |||
754 | /** |
||
755 | * Replace all occurrences of the search string with the replacement string |
||
756 | * |
||
757 | * @param string $search The value being searched for, otherwise known as the needle. |
||
758 | * @param string $replace The replacement |
||
759 | * @param string $subject The string or array being searched and replaced on, otherwise known as the haystack. |
||
760 | * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
||
761 | * @param int $count If passed, this will be set to the number of replacements performed. |
||
762 | * @return string |
||
763 | * |
||
764 | */ |
||
765 | public static function mb_str_replace($search, $replace, $subject, $encoding = 'UTF-8', &$count = null) { |
||
775 | |||
776 | /** |
||
777 | * performs a search in a nested array |
||
778 | * @param array $haystack the array to be searched |
||
779 | * @param string $needle the search string |
||
780 | * @param string $index optional, only search this key name |
||
781 | * @return mixed the key of the matching field, otherwise false |
||
782 | * |
||
783 | * performs a search in a nested array |
||
784 | * |
||
785 | * taken from http://www.php.net/manual/en/function.array-search.php#97645 |
||
786 | */ |
||
787 | public static function recursiveArraySearch($haystack, $needle, $index = null) { |
||
801 | |||
802 | /** |
||
803 | * Shortens str to maxlen by replacing characters in the middle with '...', eg. |
||
804 | * ellipsis('a very long string with lots of useless info to make a better example', 14) becomes 'a very ...example' |
||
805 | * |
||
806 | * @param string $str the string |
||
807 | * @param string $maxlen the maximum length of the result |
||
808 | * @return string with at most maxlen characters |
||
809 | */ |
||
810 | public static function ellipsis($str, $maxlen) { |
||
817 | |||
818 | /** |
||
819 | * calculates the maximum upload size respecting system settings, free space and user quota |
||
820 | * |
||
821 | * @param string $dir the current folder where the user currently operates |
||
822 | * @param int $freeSpace the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
||
823 | * @return int number of bytes representing |
||
824 | */ |
||
825 | public static function maxUploadFilesize($dir, $freeSpace = null) { |
||
831 | |||
832 | /** |
||
833 | * Calculate free space left within user quota |
||
834 | * |
||
835 | * @param string $dir the current folder where the user currently operates |
||
836 | * @return int number of bytes representing |
||
837 | */ |
||
838 | public static function freeSpace($dir) { |
||
847 | |||
848 | /** |
||
849 | * Calculate PHP upload limit |
||
850 | * |
||
851 | * @return PHP upload file size limit |
||
852 | */ |
||
853 | public static function uploadLimit() { |
||
864 | |||
865 | /** |
||
866 | * Checks if a function is available |
||
867 | * |
||
868 | * @param string $function_name |
||
869 | * @return bool |
||
870 | */ |
||
871 | public static function is_function_enabled($function_name) { |
||
887 | |||
888 | /** |
||
889 | * Try to find a program |
||
890 | * Note: currently windows is not supported |
||
891 | * |
||
892 | * @param string $program |
||
893 | * @return null|string |
||
894 | */ |
||
895 | public static function findBinaryPath($program) { |
||
910 | |||
911 | /** |
||
912 | * Calculate the disc space for the given path |
||
913 | * |
||
914 | * @param string $path |
||
915 | * @param \OCP\Files\FileInfo $rootInfo (optional) |
||
916 | * @return array |
||
917 | */ |
||
918 | public static function getStorageInfo($path, $rootInfo = null) { |
||
968 | |||
969 | /** |
||
970 | * Get storage info including all mount points and quota |
||
971 | * |
||
972 | * @return array |
||
973 | */ |
||
974 | private static function getGlobalStorageInfo() { |
||
999 | |||
1000 | /** |
||
1001 | * Returns whether the config file is set manually to read-only |
||
1002 | * @return bool |
||
1003 | */ |
||
1004 | public static function isReadOnlyConfigEnabled() { |
||
1007 | } |
||
1008 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.