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 Utility 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 Utility, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace XoopsModules\Smartobject; | ||
| 10 | class Utility | ||
| 11 | { | ||
| 12 | use Common\VersionChecks; //checkVerXoops, checkVerPhp Traits | ||
| 13 | |||
| 14 | use Common\ServerStats; // getServerStats Trait | ||
| 15 | |||
| 16 | use Common\FilesManagement; // Files Management Trait | ||
| 17 | |||
| 18 | //--------------- Custom module methods ----------------------------- | ||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | /** | ||
| 23 | * @param $cssfile | ||
| 24 | * @return string | ||
| 25 | */ | ||
| 26 | public static function getCssLink($cssfile) | ||
| 32 | |||
| 33 | /** | ||
| 34 | * @return string | ||
| 35 | */ | ||
| 36 | public static function getPageBeforeForm() | ||
| 42 | |||
| 43 | /** | ||
| 44 | * Checks if a user is admin of $module | ||
| 45 | * | ||
| 46 | * @param bool $module | ||
| 47 | * @return bool: true if user is admin | ||
|  | |||
| 48 | */ | ||
| 49 | public static function userIsAdmin($module = false) | ||
| 75 | |||
| 76 | /** | ||
| 77 | * @return bool | ||
| 78 | */ | ||
| 79 | public static function isXoops22() | ||
| 89 | |||
| 90 | /** | ||
| 91 | * @param bool $withLink | ||
| 92 | * @param bool $forBreadCrumb | ||
| 93 | * @param bool $moduleName | ||
| 94 | * @return string | ||
| 95 | */ | ||
| 96 | public static function getModuleName($withLink = true, $forBreadCrumb = false, $moduleName = false) | ||
| 129 | |||
| 130 | /** | ||
| 131 | * @param bool $moduleName | ||
| 132 | * @return string | ||
| 133 | */ | ||
| 134 | public static function getModuleNameForSEO($moduleName = false) | ||
| 145 | |||
| 146 | /** | ||
| 147 | * @param bool $moduleName | ||
| 148 | * @return bool | ||
| 149 | */ | ||
| 150 | public static function getModuleModeSEO($moduleName = false) | ||
| 157 | |||
| 158 | /** | ||
| 159 | * @param bool $moduleName | ||
| 160 | * @return bool | ||
| 161 | */ | ||
| 162 | public static function getModuleIncludeIdSEO($moduleName = false) | ||
| 169 | |||
| 170 | /** | ||
| 171 | * @param $key | ||
| 172 | * @return string | ||
| 173 | */ | ||
| 174 | public static function getEnv($key) | ||
| 181 | |||
| 182 | public static function getXoopsCpHeader() | ||
| 212 | |||
| 213 | /** | ||
| 214 | * Detemines if a table exists in the current db | ||
| 215 | * | ||
| 216 | * @param string $table the table name (without XOOPS prefix) | ||
| 217 | * @return bool True if table exists, false if not | ||
| 218 | * | ||
| 219 | * @access public | ||
| 220 | * @author xhelp development team | ||
| 221 | */ | ||
| 222 | public static function isTable($table) | ||
| 240 | |||
| 241 | /** | ||
| 242 | * Gets a value from a key in the xhelp_meta table | ||
| 243 | * | ||
| 244 | * @param string $key | ||
| 245 | * @param bool $moduleName | ||
| 246 | * @return string $value | ||
| 247 | * | ||
| 248 | * @access public | ||
| 249 | * @author xhelp development team | ||
| 250 | */ | ||
| 251 | public static function getMeta($key, $moduleName = false) | ||
| 267 | |||
| 268 | /** | ||
| 269 | * @return bool | ||
| 270 | */ | ||
| 271 | public static function getCurrentModuleName() | ||
| 280 | |||
| 281 | /** | ||
| 282 | * Sets a value for a key in the xhelp_meta table | ||
| 283 | * | ||
| 284 | * @param string $key | ||
| 285 | * @param string $value | ||
| 286 | * @param bool $moduleName | ||
| 287 | * @return bool TRUE if success, FALSE if failure | ||
| 288 | * | ||
| 289 | * @access public | ||
| 290 | * @author xhelp development team | ||
| 291 | */ | ||
| 292 | public static function setMeta($key, $value, $moduleName = false) | ||
| 311 | |||
| 312 | // Thanks to Mithrandir:-) | ||
| 313 | /** | ||
| 314 | * @param $str | ||
| 315 | * @param $start | ||
| 316 | * @param $length | ||
| 317 | * @param string $trimmarker | ||
| 318 | * @return string | ||
| 319 | */ | ||
| 320 | public static function getSubstr($str, $start, $length, $trimmarker = '...') | ||
| 337 | |||
| 338 | /** | ||
| 339 | * @param $key | ||
| 340 | * @param bool $moduleName | ||
| 341 | * @param string $default | ||
| 342 | * @return null|string | ||
| 343 | */ | ||
| 344 | public static function getConfig($key, $moduleName = false, $default = 'default_is_undefined') | ||
| 360 | |||
| 361 | /** | ||
| 362 | * Copy a file, or a folder and its contents | ||
| 363 | * | ||
| 364 | * @author Aidan Lister <[email protected]> | ||
| 365 | * @param string $source The source | ||
| 366 | * @param string $dest The destination | ||
| 367 | * @return bool Returns true on success, false on failure | ||
| 368 | */ | ||
| 369 | public static function copyr($source, $dest) | ||
| 398 | |||
| 399 | /** | ||
| 400 | * Thanks to the NewBB2 Development Team | ||
| 401 | * @param $target | ||
| 402 | * @return bool | ||
| 403 | */ | ||
| 404 | public static function mkdirAsAdmin($target) | ||
| 427 | |||
| 428 | /** | ||
| 429 | * Thanks to the NewBB2 Development Team | ||
| 430 | * @param $target | ||
| 431 | * @param int $mode | ||
| 432 | * @return bool | ||
| 433 | */ | ||
| 434 | public static function chmodAsAdmin($target, $mode = 0777) | ||
| 438 | |||
| 439 | /** | ||
| 440 | * @param $src | ||
| 441 | * @param $maxWidth | ||
| 442 | * @param $maxHeight | ||
| 443 | * @return array | ||
| 444 | */ | ||
| 445 | public static function imageResize($src, $maxWidth, $maxHeight) | ||
| 473 | |||
| 474 | /** | ||
| 475 | * @param bool $moduleName | ||
| 476 | * @return mixed | ||
| 477 | */ | ||
| 478 | public static function getModuleInfo($moduleName = false) | ||
| 509 | |||
| 510 | /** | ||
| 511 | * @param bool $moduleName | ||
| 512 | * @return bool | ||
| 513 | */ | ||
| 514 | public static function getModuleConfig($moduleName = false) | ||
| 551 | |||
| 552 | /** | ||
| 553 | * @param $dirname | ||
| 554 | * @return bool | ||
| 555 | */ | ||
| 556 | public static function deleteFile($dirname) | ||
| 563 | |||
| 564 | /** | ||
| 565 | * @param array $errors | ||
| 566 | * @return string | ||
| 567 | */ | ||
| 568 | public static function formatErrors($errors = []) | ||
| 577 | |||
| 578 | /** | ||
| 579 | * getLinkedUnameFromId() | ||
| 580 | * | ||
| 581 | * @param integer $userid Userid of poster etc | ||
| 582 | * @param integer $name : 0 Use Usenamer 1 Use realname | ||
| 583 | * @param array $users | ||
| 584 | * @param bool $withContact | ||
| 585 | * @return string | ||
| 586 | */ | ||
| 587 | public static function getLinkedUnameFromId($userid = 0, $name = 0, $users = [], $withContact = false) | ||
| 630 | |||
| 631 | /** | ||
| 632 | * @param int $currentoption | ||
| 633 | * @param string $breadcrumb | ||
| 634 | * @param bool $submenus | ||
| 635 | * @param int $currentsub | ||
| 636 | */ | ||
| 637 | public static function getAdminMenu($currentoption = 0, $breadcrumb = '', $submenus = false, $currentsub = -1) | ||
| 663 | |||
| 664 | /** | ||
| 665 | * @param string $id | ||
| 666 | * @param string $title | ||
| 667 | * @param string $dsc | ||
| 668 | */ | ||
| 669 | View Code Duplication | public static function getCollapsableBar($id = '', $title = '', $dsc = '') | |
| 679 | |||
| 680 | /** | ||
| 681 | * @param string $id | ||
| 682 | * @param string $title | ||
| 683 | * @param string $dsc | ||
| 684 | */ | ||
| 685 | View Code Duplication | public static function getAjaxCollapsableBar($id = '', $title = '', $dsc = '') | |
| 697 | |||
| 698 | /** | ||
| 699 | * Ajax testing...... | ||
| 700 | * @param $name | ||
| 701 | */ | ||
| 702 | /* | ||
| 703 | public static function getCollapsableBar($id = '', $title = '', $dsc='') | ||
| 704 |      { | ||
| 705 | |||
| 706 | global $xoopsModule; | ||
| 707 |      //echo "<h3 style=\"color: #2F5376; font-weight: bold; font-size: 14px; margin: 6px 0 0 0; \"><a href='javascript:;' onclick=\"toggle('" . $id . "'); toggleIcon('" . $id . "_icon')\";>"; | ||
| 708 | |||
| 709 | ?> | ||
| 710 |      <h3 class="smart_collapsable_title"><a href="javascript:Effect.Combo('<?php echo $id ?>');"><?php echo $title ?></a></h3> | ||
| 711 | <?php | ||
| 712 | |||
| 713 | echo "<img id='" . $id . "_icon' src=" . SMARTOBJECT_URL . "assets/images/close12.gif alt=''></a> " . $title . "</h3>"; | ||
| 714 | echo "<div id='" . $id . "'>"; | ||
| 715 |      if ($dsc != '') { | ||
| 716 | echo "<span style=\"color: #567; margin: 3px 0 12px 0; font-size: small; display: block; \">" . $dsc . "</span>"; | ||
| 717 | } | ||
| 718 | } | ||
| 719 | */ | ||
| 720 | public static function opencloseCollapsable($name) | ||
| 745 | |||
| 746 | /** | ||
| 747 | * @param $name | ||
| 748 | */ | ||
| 749 | public static function closeCollapsable($name) | ||
| 755 | |||
| 756 | /** | ||
| 757 | * @param $name | ||
| 758 | * @param $value | ||
| 759 | * @param int $time | ||
| 760 | */ | ||
| 761 | public static function setCookieVar($name, $value, $time = 0) | ||
| 769 | |||
| 770 | /** | ||
| 771 | * @param $name | ||
| 772 | * @param string $default | ||
| 773 | * @return string | ||
| 774 | */ | ||
| 775 | public static function getCookieVar($name, $default = '') | ||
| 784 | |||
| 785 | /** | ||
| 786 | * @return array | ||
| 787 | */ | ||
| 788 | public static function getCurrentUrls() | ||
| 810 | |||
| 811 | /** | ||
| 812 | * @return mixed | ||
| 813 | */ | ||
| 814 | public static function getCurrentPage() | ||
| 820 | |||
| 821 | /** | ||
| 822 | * Create a title for the short_url field of an article | ||
| 823 | * | ||
| 824 | * @credit psylove | ||
| 825 | * | ||
| 826 | * @var string $title title of the article | ||
| 827 | * @var string $withExt do we add an html extension or not | ||
| 828 | * @return string sort_url for the article | ||
| 829 | */ | ||
| 830 | /** | ||
| 831 | * Moved in SmartMetaGenClass | ||
| 832 | */ | ||
| 833 | /* | ||
| 834 | public static function smart_seo_title($title='', $withExt=true) | ||
| 835 |      { | ||
| 836 | // Transformation de la chaine en minuscule | ||
| 837 | // Codage de la chaine afin d'éviter les erreurs 500 en cas de caractères imprévus | ||
| 838 | $title = rawurlencode(strtolower($title)); | ||
| 839 | |||
| 840 | // Transformation des ponctuations | ||
| 841 |      //                 Tab     Space      !        "        #        %        &        '        (        )        ,        /       :        ;        <        =        >        ?        @        [        \        ]        ^        {        |        }        ~       . | ||
| 842 |      $pattern = array("/%09/", "/%20/", "/%21/", "/%22/", "/%23/", "/%25/", "/%26/", "/%27/", "/%28/", "/%29/", "/%2C/", "/%2F/", "/%3A/", "/%3B/", "/%3C/", "/%3D/", "/%3E/", "/%3F/", "/%40/", "/%5B/", "/%5C/", "/%5D/", "/%5E/", "/%7B/", "/%7C/", "/%7D/", "/%7E/", "/\./"); | ||
| 843 | $rep_pat = array( "-" , "-" , "" , "" , "" , "-100" , "" , "-" , "" , "" , "" , "-" , "" , "" , "" , "-" , "" , "" , "-at-" , "" , "-" , "" , "-" , "" , "-" , "" , "-" , "" ); | ||
| 844 | $title = preg_replace($pattern, $rep_pat, $title); | ||
| 845 | |||
| 846 | // Transformation des caractères accentués | ||
| 847 | $pattern = array( | ||
| 848 | '/%B0/', // ° | ||
| 849 | '/%E8/', // è | ||
| 850 | '/%E9/', // é | ||
| 851 | '/%EA/', // ê | ||
| 852 | '/%EB/', // ë | ||
| 853 | '/%E7/', // ç | ||
| 854 | '/%E0/', // à | ||
| 855 | '/%E2/', // â | ||
| 856 | '/%E4/', // ä | ||
| 857 | '/%EE/', // î | ||
| 858 | '/%EF/', // ï | ||
| 859 | '/%F9/', // ù | ||
| 860 | '/%FC/', // ü | ||
| 861 | '/%FB/', // û | ||
| 862 | '/%F4/', // ô | ||
| 863 | '/%F6/', // ö | ||
| 864 | ); | ||
| 865 | $rep_pat = array( "e" , "e" , "e" , "e" , "c" , "a" , "a" , "a" , "i" , "i" , "u" , "u" , "u" , "o" , "o" ); | ||
| 866 | $title = preg_replace($pattern, $rep_pat, $title); | ||
| 867 | |||
| 868 |      if (count($title) > 0) { | ||
| 869 |      if ($withExt) { | ||
| 870 | $title .= '.html'; | ||
| 871 | } | ||
| 872 | |||
| 873 | return $title; | ||
| 874 | } else | ||
| 875 | |||
| 876 | return ''; | ||
| 877 | } | ||
| 878 | */ | ||
| 879 | public static function getModFooter() | ||
| 899 | |||
| 900 | public static function getXoopsCpFooter() | ||
| 905 | |||
| 906 | /** | ||
| 907 | * @param $text | ||
| 908 | * @return mixed | ||
| 909 | */ | ||
| 910 | public static function sanitizeForCommonTags($text) | ||
| 918 | |||
| 919 | /** | ||
| 920 | * @param $src | ||
| 921 | */ | ||
| 922 | public static function addScript($src) | ||
| 926 | |||
| 927 | /** | ||
| 928 | * @param $src | ||
| 929 | */ | ||
| 930 | public static function addStyle($src) | ||
| 937 | |||
| 938 | public static function addAdminAjaxSupport() | ||
| 944 | |||
| 945 | /** | ||
| 946 | * @param $text | ||
| 947 | * @return mixed | ||
| 948 | */ | ||
| 949 | public static function sanitizeForSmartpopupLink($text) | ||
| 957 | |||
| 958 | /** | ||
| 959 | * Finds the width and height of an image (can also be a flash file) | ||
| 960 | * | ||
| 961 | * @credit phppp | ||
| 962 | * | ||
| 963 | * @var string $url path of the image file | ||
| 964 | * @var string $width reference to the width | ||
| 965 | * @var string $height reference to the height | ||
| 966 | * @return bool false if impossible to find dimension | ||
| 967 | */ | ||
| 968 | public static function getImageSize($url, & $width, & $height) | ||
| 990 | |||
| 991 | /** | ||
| 992 | * Convert characters to decimal values | ||
| 993 | * | ||
| 994 | * @author eric.wallet at yahoo.fr | ||
| 995 | * @link http://ca.php.net/manual/en/function.htmlentities.php#69913 | ||
| 996 | * @param $str | ||
| 997 | * @return mixed | ||
| 998 | */ | ||
| 999 | public static function getHtmlnumericentities($str) | ||
| 1006 | |||
| 1007 | /** | ||
| 1008 | * @param $name | ||
| 1009 | * @param bool $optional | ||
| 1010 | * @return mixed | ||
| 1011 | */ | ||
| 1012 | public static function getCoreHandler($name, $optional = false) | ||
| 1033 | |||
| 1034 | /** | ||
| 1035 | * @param $matches | ||
| 1036 | * @return string | ||
| 1037 | */ | ||
| 1038 | public static function sanitizeAdsenses_callback($matches) | ||
| 1050 | |||
| 1051 | /** | ||
| 1052 | * @param $text | ||
| 1053 | * @return mixed | ||
| 1054 | */ | ||
| 1055 | View Code Duplication | public static function sanitizeAdsenses($text) | |
| 1065 | |||
| 1066 | /** | ||
| 1067 | * @param $matches | ||
| 1068 | * @return string | ||
| 1069 | */ | ||
| 1070 | public static function sanitizeCustomtags_callback($matches) | ||
| 1082 | |||
| 1083 | /** | ||
| 1084 | * @param $text | ||
| 1085 | * @return mixed | ||
| 1086 | */ | ||
| 1087 | View Code Duplication | public static function sanitizeCustomtags($text) | |
| 1097 | |||
| 1098 | /** | ||
| 1099 | * @param $module | ||
| 1100 | * @param $file | ||
| 1101 | */ | ||
| 1102 | public static function loadLanguageFile($module, $file) | ||
| 1114 | |||
| 1115 | public static function loadCommonLanguageFile() | ||
| 1119 | |||
| 1120 | /** | ||
| 1121 | * @param $text | ||
| 1122 | * @param bool $keyword | ||
| 1123 | * @return mixed|string | ||
| 1124 | */ | ||
| 1125 | public static function purifyText($text, $keyword = false) | ||
| 1157 | |||
| 1158 | /** | ||
| 1159 | * @param $document | ||
| 1160 | * @return mixed | ||
| 1161 | */ | ||
| 1162 | public static function getHtml2text($document) | ||
| 1210 | |||
| 1211 | /** | ||
| 1212 | * @author pillepop2003 at yahoo dot de | ||
| 1213 | * | ||
| 1214 | * Use this snippet to extract any float out of a string. You can choose how a single dot is treated with the (bool) 'single_dot_as_decimal' directive. | ||
| 1215 | * This function should be able to cover almost all floats that appear in an european environment. | ||
| 1216 | * @param $str | ||
| 1217 | * @param bool $set | ||
| 1218 | * @return float|int | ||
| 1219 | */ | ||
| 1220 | public static function getFloat($str, $set = false) | ||
| 1249 | |||
| 1250 | /** | ||
| 1251 | * @param $var | ||
| 1252 | * @param bool $currencyObj | ||
| 1253 | * @return float|int|mixed|string | ||
| 1254 | */ | ||
| 1255 | public static function getCurrency($var, $currencyObj = false) | ||
| 1278 | |||
| 1279 | /** | ||
| 1280 | * @param $var | ||
| 1281 | * @return float|int|mixed|string | ||
| 1282 | */ | ||
| 1283 | public static function float($var) | ||
| 1287 | |||
| 1288 | /** | ||
| 1289 | * @param bool $moduleName | ||
| 1290 | * @return string | ||
| 1291 | */ | ||
| 1292 | public static function getModuleAdminLink($moduleName = false) | ||
| 1305 | |||
| 1306 | /** | ||
| 1307 | * @return array|bool | ||
| 1308 | */ | ||
| 1309 | public static function getEditors() | ||
| 1325 | |||
| 1326 | /** | ||
| 1327 | * @param $moduleName | ||
| 1328 | * @param $items | ||
| 1329 | * @return array | ||
| 1330 | */ | ||
| 1331 | public static function getTablesArray($moduleName, $items) | ||
| 1341 | |||
| 1342 | |||
| 1343 | } | ||
| 1344 | 
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.