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 adminAdminModel 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 adminAdminModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class adminAdminModel extends admin |
||
| 12 | { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Ftp root path |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | var $pwd; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Buffer for Admin GNB menu |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | var $gnbLangBuffer; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Find XE installed path on sftp |
||
| 28 | */ |
||
| 29 | function getSFTPPath() |
||
| 30 | { |
||
| 31 | $ftp_info = Context::getRequestVars(); |
||
| 32 | |||
| 33 | if(!$ftp_info->ftp_host) |
||
| 34 | { |
||
| 35 | $ftp_info->ftp_host = "127.0.0.1"; |
||
| 36 | } |
||
| 37 | |||
| 38 | if(!$ftp_info->ftp_port || !is_numeric($ftp_info->ftp_port)) |
||
| 39 | { |
||
| 40 | $ftp_info->ftp_port = '22'; |
||
| 41 | } |
||
| 42 | |||
| 43 | $connection = ssh2_connect($ftp_info->ftp_host, $ftp_info->ftp_port); |
||
| 44 | if(!ssh2_auth_password($connection, $ftp_info->ftp_user, $ftp_info->ftp_password)) |
||
| 45 | { |
||
| 46 | return new Object(-1, 'msg_ftp_invalid_auth_info'); |
||
| 47 | } |
||
| 48 | $sftp = ssh2_sftp($connection); |
||
| 49 | |||
| 50 | // create temp file |
||
| 51 | $pin = $_SERVER['REQUEST_TIME']; |
||
| 52 | FileHandler::writeFile('./files/cache/ftp_check', $pin); |
||
| 53 | |||
| 54 | // create path candidate |
||
| 55 | $xe_path = _XE_PATH_; |
||
|
|
|||
| 56 | $path_info = array_reverse(explode('/', _XE_PATH_)); |
||
| 57 | array_pop($path_info); // remove last '/' |
||
| 58 | $path_candidate = array(); |
||
| 59 | |||
| 60 | $temp = ''; |
||
| 61 | foreach($path_info as $path) |
||
| 62 | { |
||
| 63 | $temp = '/' . $path . $temp; |
||
| 64 | $path_candidate[] = $temp; |
||
| 65 | } |
||
| 66 | |||
| 67 | // try |
||
| 68 | foreach($path_candidate as $path) |
||
| 69 | { |
||
| 70 | // upload check file |
||
| 71 | if(!@ssh2_scp_send($connection, FileHandler::getRealPath('./files/cache/ftp_check'), $path . 'ftp_check.html')) |
||
| 72 | { |
||
| 73 | continue; |
||
| 74 | } |
||
| 75 | |||
| 76 | // get check file |
||
| 77 | $result = FileHandler::getRemoteResource(getNotencodedFullUrl() . 'ftp_check.html'); |
||
| 78 | |||
| 79 | // delete temp check file |
||
| 80 | @ssh2_sftp_unlink($sftp, $path . 'ftp_check.html'); |
||
| 81 | |||
| 82 | // found |
||
| 83 | if($result == $pin) |
||
| 84 | { |
||
| 85 | $found_path = $path; |
||
| 86 | break; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | FileHandler::removeFile('./files/cache/ftp_check', $pin); |
||
| 91 | |||
| 92 | if($found_path) |
||
| 93 | { |
||
| 94 | $this->add('found_path', $found_path); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | function getFTPPath() |
||
| 99 | { |
||
| 100 | $ftp_info = Context::getRequestVars(); |
||
| 101 | |||
| 102 | if(!$ftp_info->ftp_host) |
||
| 103 | { |
||
| 104 | $ftp_info->ftp_host = "127.0.0.1"; |
||
| 105 | } |
||
| 106 | |||
| 107 | if(!$ftp_info->ftp_port || !is_numeric($ftp_info->ftp_port)) |
||
| 108 | { |
||
| 109 | $ftp_info->ftp_port = '22'; |
||
| 110 | } |
||
| 111 | |||
| 112 | $connection = ftp_connect($ftp_info->ftp_host, $ftp_info->ftp_port); |
||
| 113 | if(!$connection) |
||
| 114 | { |
||
| 115 | return new Object(-1, sprintf(Context::getLang('msg_ftp_not_connected'), 'host')); |
||
| 116 | } |
||
| 117 | |||
| 118 | $login_result = @ftp_login($connection, $ftp_info->ftp_user, $ftp_info->ftp_password); |
||
| 119 | if(!$login_result) |
||
| 120 | { |
||
| 121 | ftp_close($connection); |
||
| 122 | return new Object(-1, 'msg_ftp_invalid_auth_info'); |
||
| 123 | } |
||
| 124 | |||
| 125 | // create temp file |
||
| 126 | $pin = $_SERVER['REQUEST_TIME']; |
||
| 127 | FileHandler::writeFile('./files/cache/ftp_check', $pin); |
||
| 128 | |||
| 129 | // create path candidate |
||
| 130 | $xe_path = _XE_PATH_; |
||
| 131 | $path_info = array_reverse(explode('/', _XE_PATH_)); |
||
| 132 | array_pop($path_info); // remove last '/' |
||
| 133 | $path_candidate = array(); |
||
| 134 | |||
| 135 | $temp = ''; |
||
| 136 | foreach($path_info as $path) |
||
| 137 | { |
||
| 138 | $temp = '/' . $path . $temp; |
||
| 139 | $path_candidate[] = $temp; |
||
| 140 | } |
||
| 141 | |||
| 142 | // try |
||
| 143 | foreach($path_candidate as $path) |
||
| 144 | { |
||
| 145 | // upload check file |
||
| 146 | if(!ftp_put($connection, $path . 'ftp_check.html', FileHandler::getRealPath('./files/cache/ftp_check'), FTP_BINARY)) |
||
| 147 | { |
||
| 148 | continue; |
||
| 149 | } |
||
| 150 | |||
| 151 | // get check file |
||
| 152 | $result = FileHandler::getRemoteResource(getNotencodedFullUrl() . 'ftp_check.html'); |
||
| 153 | |||
| 154 | // delete temp check file |
||
| 155 | ftp_delete($connection, $path . 'ftp_check.html'); |
||
| 156 | |||
| 157 | // found |
||
| 158 | if($result == $pin) |
||
| 159 | { |
||
| 160 | $found_path = $path; |
||
| 161 | break; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | FileHandler::removeFile('./files/cache/ftp_check', $pin); |
||
| 166 | |||
| 167 | if($found_path) |
||
| 168 | { |
||
| 169 | $this->add('found_path', $found_path); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Find XE installed path on ftp |
||
| 175 | */ |
||
| 176 | function getAdminFTPPath() |
||
| 177 | { |
||
| 178 | Context::loadLang(_XE_PATH_ . 'modules/autoinstall/lang'); |
||
| 179 | @set_time_limit(5); |
||
| 180 | require_once(_XE_PATH_ . 'libs/ftp.class.php'); |
||
| 181 | |||
| 182 | $ftp_info = Context::getRequestVars(); |
||
| 183 | |||
| 184 | if(!$ftp_info->ftp_user || !$ftp_info->ftp_password) |
||
| 185 | { |
||
| 186 | return new Object(1, 'msg_ftp_invalid_auth_info'); |
||
| 187 | } |
||
| 188 | |||
| 189 | if(!$ftp_info->ftp_host) |
||
| 190 | { |
||
| 191 | $ftp_info->ftp_host = '127.0.0.1'; |
||
| 192 | } |
||
| 193 | |||
| 194 | if(!$ftp_info->ftp_port || !is_numeric($ftp_info->ftp_port)) |
||
| 195 | { |
||
| 196 | $ftp_info->ftp_port = '21'; |
||
| 197 | } |
||
| 198 | |||
| 199 | View Code Duplication | if($ftp_info->sftp == 'Y') |
|
| 200 | { |
||
| 201 | if(!function_exists('ssh2_sftp')) |
||
| 202 | { |
||
| 203 | return new Object(-1, 'disable_sftp_support'); |
||
| 204 | } |
||
| 205 | return $this->getSFTPPath(); |
||
| 206 | } |
||
| 207 | |||
| 208 | if($ftp_info->ftp_pasv == 'N') |
||
| 209 | { |
||
| 210 | if(function_exists('ftp_connect')) |
||
| 211 | { |
||
| 212 | return $this->getFTPPath(); |
||
| 213 | } |
||
| 214 | $ftp_info->ftp_pasv = "Y"; |
||
| 215 | } |
||
| 216 | |||
| 217 | $oFTP = new ftp(); |
||
| 218 | if(!$oFTP->ftp_connect($ftp_info->ftp_host, $ftp_info->ftp_port)) |
||
| 219 | { |
||
| 220 | return new Object(1, sprintf(Context::getLang('msg_ftp_not_connected'), 'host')); |
||
| 221 | } |
||
| 222 | |||
| 223 | if(!$oFTP->ftp_login($ftp_info->ftp_user, $ftp_info->ftp_password)) |
||
| 224 | { |
||
| 225 | return new Object(1, 'msg_ftp_invalid_auth_info'); |
||
| 226 | } |
||
| 227 | |||
| 228 | // create temp file |
||
| 229 | $pin = $_SERVER['REQUEST_TIME']; |
||
| 230 | FileHandler::writeFile('./files/cache/ftp_check', $pin); |
||
| 231 | |||
| 232 | // create path candidate |
||
| 233 | $xe_path = _XE_PATH_; |
||
| 234 | $path_info = array_reverse(explode('/', _XE_PATH_)); |
||
| 235 | array_pop($path_info); // remove last '/' |
||
| 236 | $path_candidate = array(); |
||
| 237 | |||
| 238 | $temp = ''; |
||
| 239 | foreach($path_info as $path) |
||
| 240 | { |
||
| 241 | $temp = '/' . $path . $temp; |
||
| 242 | $path_candidate[] = $temp; |
||
| 243 | } |
||
| 244 | |||
| 245 | // try |
||
| 246 | foreach($path_candidate as $path) |
||
| 247 | { |
||
| 248 | // upload check file |
||
| 249 | if(!$oFTP->ftp_put($path . 'ftp_check.html', FileHandler::getRealPath('./files/cache/ftp_check'))) |
||
| 250 | { |
||
| 251 | continue; |
||
| 252 | } |
||
| 253 | |||
| 254 | // get check file |
||
| 255 | $result = FileHandler::getRemoteResource(getNotencodedFullUrl() . 'ftp_check.html'); |
||
| 256 | |||
| 257 | // delete temp check file |
||
| 258 | $oFTP->ftp_delete($path . 'ftp_check.html'); |
||
| 259 | |||
| 260 | // found |
||
| 261 | if($result == $pin) |
||
| 262 | { |
||
| 263 | $found_path = $path; |
||
| 264 | break; |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | FileHandler::removeFile('./files/cache/ftp_check', $pin); |
||
| 269 | |||
| 270 | if($found_path) |
||
| 271 | { |
||
| 272 | $this->add('found_path', $found_path); |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Add file list to Object after sftp connect |
||
| 278 | * @return void|Object |
||
| 279 | */ |
||
| 280 | View Code Duplication | function getSFTPList() |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Add file list to Object after ftp connect |
||
| 319 | * @return void|Object |
||
| 320 | */ |
||
| 321 | function getAdminFTPList() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Parameter arrange for send to XE collect server |
||
| 393 | * @param string $type 'WORKING', 'INSTALL' |
||
| 394 | * @return string |
||
| 395 | */ |
||
| 396 | function getEnv($type = 'WORKING') |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Return theme info list by theme directory list |
||
| 514 | * @return array |
||
| 515 | */ |
||
| 516 | function getThemeList() |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Return theme info |
||
| 535 | * @param string $theme_name |
||
| 536 | * @param array $layout_list |
||
| 537 | * @return object |
||
| 538 | */ |
||
| 539 | function getThemeInfo($theme_name, $layout_list = NULL) |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Return theme module skin list |
||
| 704 | * @return array |
||
| 705 | */ |
||
| 706 | function getModulesSkinList() |
||
| 744 | |||
| 745 | /** |
||
| 746 | * Return admin menu language |
||
| 747 | * @return array |
||
| 748 | */ |
||
| 749 | function getAdminMenuLang() |
||
| 784 | |||
| 785 | /** |
||
| 786 | * Get admin favorite list |
||
| 787 | * @param int $siteSrl if default site, siteSrl is zero |
||
| 788 | * @param bool $isGetModuleInfo |
||
| 789 | * @return object |
||
| 790 | */ |
||
| 791 | function getFavoriteList($siteSrl = 0, $isGetModuleInfo = FALSE) |
||
| 820 | |||
| 821 | /** |
||
| 822 | * Check available insert favorite |
||
| 823 | * @param int $siteSrl if default site, siteSrl is zero |
||
| 824 | * @param string $module |
||
| 825 | * @return object |
||
| 826 | */ |
||
| 827 | function isExistsFavorite($siteSrl, $module) |
||
| 851 | |||
| 852 | /** |
||
| 853 | * Return site list |
||
| 854 | * @return void |
||
| 855 | */ |
||
| 856 | function getSiteAllList() |
||
| 865 | |||
| 866 | /** |
||
| 867 | * Returns a list of all sites that contain modules |
||
| 868 | * For each site domain and site_srl are retrieved |
||
| 869 | * |
||
| 870 | * @return array |
||
| 871 | */ |
||
| 872 | function getAllSitesThatHaveModules($domain = NULL) |
||
| 914 | |||
| 915 | /** |
||
| 916 | * Return site count |
||
| 917 | * @param string $date |
||
| 918 | * @return int |
||
| 919 | */ |
||
| 920 | View Code Duplication | function getSiteCountByDate($date = '') |
|
| 937 | |||
| 938 | function getFaviconUrl($default = true) |
||
| 942 | |||
| 943 | function getMobileIconUrl($default = true) |
||
| 947 | |||
| 948 | function iconUrlCheck($iconname, $default_icon_name, $default) |
||
| 949 | { |
||
| 950 | $site_info = Context::get('site_module_info'); |
||
| 970 | |||
| 971 | } |
||
| 972 | /* End of file admin.admin.model.php */ |
||
| 974 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.