Complex classes like ModuleAdmin 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 ModuleAdmin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class ModuleAdmin |
||
| 19 | { |
||
| 20 | private $_itemButton = array(); |
||
| 21 | private $_itemInfoBox = array(); |
||
| 22 | private $_itemInfoBoxLine = array(); |
||
| 23 | private $_itemConfigBoxLine = array(); |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var XoopsModule |
||
| 27 | */ |
||
| 28 | private $_obj; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Constructor |
||
| 32 | */ |
||
| 33 | public function __construct() |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @return array |
||
| 45 | */ |
||
| 46 | public function getInfo() |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Return the Module Admin class version number |
||
| 61 | * return string version |
||
| 62 | **/ |
||
| 63 | public function getVersion() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Return the Module Admin class release date |
||
| 76 | * return string version |
||
| 77 | **/ |
||
| 78 | public function getReleaseDate() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Return the available methods for the class |
||
| 91 | * |
||
| 92 | * @return array methods supported by this class |
||
| 93 | */ |
||
| 94 | public function getClassMethods() |
||
| 100 | |||
| 101 | //****************************************************************************************************************** |
||
| 102 | // loadLanguage |
||
| 103 | //****************************************************************************************************************** |
||
| 104 | // Loaf the language file. |
||
| 105 | //****************************************************************************************************************** |
||
| 106 | /** |
||
| 107 | * @return bool|mixed |
||
| 108 | */ |
||
| 109 | public function loadLanguage() |
||
| 121 | //****************************************************************************************************************** |
||
| 122 | // renderMenuIndex |
||
| 123 | //****************************************************************************************************************** |
||
| 124 | // Creating a menu icon in the index |
||
| 125 | //****************************************************************************************************************** |
||
| 126 | /** |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | public function renderMenuIndex() |
||
| 130 | { |
||
| 131 | $path = XOOPS_URL . '/modules/' . $this->_obj->getVar('dirname') . '/'; |
||
| 132 | $pathsystem = XOOPS_URL . '/modules/system/'; |
||
| 133 | $this->_obj->loadAdminMenu(); |
||
| 134 | $ret = "<div class=\"rmmenuicon\">\n"; |
||
| 135 | foreach (array_keys($this->_obj->adminmenu) as $i) { |
||
| 136 | if ($this->_obj->adminmenu[$i]['link'] !== 'admin/index.php') { |
||
| 137 | $ret .= "<a href=\"../" . $this->_obj->adminmenu[$i]['link'] . "\" title=\"" . (isset($this->_obj->adminmenu[$i]['desc']) ? $this->_obj->adminmenu[$i]['desc'] : '') . "\">"; |
||
| 138 | //$ret .= "<img src=\"" . $path . $this->_obj->adminmenu[$i]['icon']. "\" alt=\"" . $this->_obj->adminmenu[$i]['title'] . "\" />"; |
||
| 139 | //mb for direct URL access to icons in modules Admin |
||
| 140 | $ret .= "<img src=\"" . (filter_var($this->_obj->adminmenu[$i]['icon'], FILTER_VALIDATE_URL) ? $this->_obj->adminmenu[$i]['icon'] : $path . $this->_obj->adminmenu[$i]['icon']) . "\" alt=\"" . $this->_obj->adminmenu[$i]['title'] . "\" />"; |
||
| 141 | |||
| 142 | $ret .= '<span>' . $this->_obj->adminmenu[$i]['title'] . '</span>'; |
||
| 143 | $ret .= '</a>'; |
||
| 144 | } |
||
| 145 | } |
||
| 146 | if ($this->_obj->getInfo('help')) { |
||
| 147 | if (substr(XOOPS_VERSION, 0, 9) >= 'XOOPS 2.5') { |
||
| 148 | $ret .= "<a href=\"" . $pathsystem . 'help.php?mid=' . $this->_obj->getVar('mid', 's') . '&' . $this->_obj->getInfo('help') . "\" title=\"" . _AM_SYSTEM_HELP . "\">"; |
||
| 149 | $ret .= "<img width=\"32px\" src=\"" . XOOPS_URL . "/Frameworks/moduleclasses/icons/32/help.png\" alt=\"" . _AM_SYSTEM_HELP . "\" /> "; |
||
| 150 | $ret .= '<span>' . _AM_SYSTEM_HELP . '</span>'; |
||
| 151 | $ret .= '</a>'; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | $ret .= "</div>\n<div style=\"clear: both;\"></div>\n"; |
||
| 155 | |||
| 156 | return $ret; |
||
| 157 | } |
||
| 158 | //****************************************************************************************************************** |
||
| 159 | // renderButton |
||
| 160 | //****************************************************************************************************************** |
||
| 161 | // Creating button |
||
| 162 | //****************************************************************************************************************** |
||
| 163 | /** |
||
| 164 | * @param string $position |
||
| 165 | * @param string $delimeter |
||
| 166 | * |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | public function renderButton($position = 'right', $delimeter = ' ') |
||
| 170 | { |
||
| 171 | $path = XOOPS_URL . '/Frameworks/moduleclasses/icons/32/'; |
||
| 172 | switch ($position) { |
||
| 173 | default: |
||
| 174 | case 'right': |
||
| 175 | $ret = "<div class=\"floatright\">\n"; |
||
| 176 | break; |
||
| 177 | |||
| 178 | case 'left': |
||
| 179 | $ret = "<div class=\"floatleft\">\n"; |
||
| 180 | break; |
||
| 181 | |||
| 182 | case 'center': |
||
| 183 | $ret = "<div class=\"aligncenter\">\n"; |
||
| 184 | } |
||
| 185 | $ret .= "<div class=\"xo-buttons\">\n"; |
||
| 186 | foreach (array_keys($this->_itemButton) as $i) { |
||
| 187 | $ret .= "<a class='ui-corner-all tooltip' href='" . $this->_itemButton[$i]['link'] . "' title='" . $this->_itemButton[$i]['title'] . "' " . $this->_itemButton[$i]['extra'] . '>'; |
||
| 188 | $ret .= "<img src='" |
||
| 189 | //. $path . $this -> _itemButton[$i]['icon'] |
||
| 190 | //mb for direct URL access to icons in modules Admin |
||
| 191 | . (filter_var($this->_itemButton[$i]['icon'], FILTER_VALIDATE_URL) ? $this->_itemButton[$i]['icon'] : $path . $this->_itemButton[$i]['icon']) . "' title='" . $this->_itemButton[$i]['title'] . "' alt='" . $this->_itemButton[$i]['title'] . "' />" . $this->_itemButton[$i]['title']; |
||
| 192 | $ret .= "</a>\n"; |
||
| 193 | $ret .= $delimeter; |
||
| 194 | } |
||
| 195 | $ret .= "</div>\n</div>\n"; |
||
| 196 | $ret .= '<br> <br><br>'; |
||
| 197 | |||
| 198 | return $ret; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param $title |
||
| 203 | * @param $link |
||
| 204 | * @param string $icon |
||
| 205 | * @param string $extra |
||
| 206 | * |
||
| 207 | * @return bool |
||
| 208 | */ |
||
| 209 | public function addItemButton($title, $link, $icon = 'add', $extra = '') |
||
| 220 | //****************************************************************************************************************** |
||
| 221 | // addConfigBoxLine |
||
| 222 | //****************************************************************************************************************** |
||
| 223 | // $value: value |
||
| 224 | // $type: type of config: 1- "default": Just a line with value. |
||
| 225 | // 2- "folder": check if this is an folder. |
||
| 226 | // 3- "chmod": check if this is the good chmod. |
||
| 227 | // For this type ("chmod"), the value is an array: array(path, chmod) |
||
| 228 | //****************************************************************************************************************** |
||
| 229 | /** |
||
| 230 | * @param string $value |
||
| 231 | * @param string $type |
||
| 232 | * |
||
| 233 | * @return bool |
||
| 234 | */ |
||
| 235 | public function addConfigBoxLine($value = '', $type = 'default') |
||
| 236 | { |
||
| 237 | $line = ''; |
||
| 238 | $path = XOOPS_URL . '/Frameworks/moduleclasses/icons/16/'; |
||
| 239 | switch ($type) { |
||
| 240 | default: |
||
| 241 | case 'default': |
||
| 242 | $line .= '<span>' . $value . '</span>'; |
||
| 243 | break; |
||
| 244 | |||
| 245 | case 'folder': |
||
| 246 | if (!is_dir($value)) { |
||
| 247 | $line .= "<span style='color : red; font-weight : bold;'>"; |
||
| 248 | $line .= "<img src='" . $path . "0.png' >"; |
||
| 249 | $line .= sprintf(_AM_MODULEADMIN_CONFIG_FOLDERKO, $value); |
||
| 250 | $line .= "</span>\n"; |
||
| 251 | } else { |
||
| 252 | $line .= "<span style='color : green;'>"; |
||
| 253 | $line .= "<img src='" . $path . "1.png' >"; |
||
| 254 | $line .= sprintf(_AM_MODULEADMIN_CONFIG_FOLDEROK, $value); |
||
| 255 | $line .= "</span>\n"; |
||
| 256 | } |
||
| 257 | break; |
||
| 258 | |||
| 259 | case 'chmod': |
||
| 260 | if (is_dir($value[0])) { |
||
| 261 | if (substr(decoct(fileperms($value[0])), 2) != $value[1]) { |
||
| 262 | $line .= "<span style='color : red; font-weight : bold;'>"; |
||
| 263 | $line .= "<img src='" . $path . "0.png' >"; |
||
| 264 | $line .= sprintf(_AM_MODULEADMIN_CONFIG_CHMOD, $value[0], $value[1], substr(decoct(fileperms($value[0])), 2)); |
||
| 265 | $line .= "</span>\n"; |
||
| 266 | } else { |
||
| 267 | $line .= "<span style='color : green;'>"; |
||
| 268 | $line .= "<img src='" . $path . "1.png' >"; |
||
| 269 | $line .= sprintf(_AM_MODULEADMIN_CONFIG_CHMOD, $value[0], $value[1], substr(decoct(fileperms($value[0])), 2)); |
||
| 270 | $line .= "</span>\n"; |
||
| 271 | } |
||
| 272 | } |
||
| 273 | break; |
||
| 274 | } |
||
| 275 | $this->_itemConfigBoxLine[] = $line; |
||
| 276 | |||
| 277 | return true; |
||
| 278 | } |
||
| 279 | //****************************************************************************************************************** |
||
| 280 | // renderIndex |
||
| 281 | //****************************************************************************************************************** |
||
| 282 | // Creating an index |
||
| 283 | //****************************************************************************************************************** |
||
| 284 | /** |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function renderIndex() |
||
| 288 | { |
||
| 289 | $ret = "<table id='xo-modadmin-index'>\n<tr>\n"; |
||
| 290 | $ret .= "<td width=\"40%\">\n"; |
||
| 291 | $ret .= $this->renderMenuIndex(); |
||
| 292 | $ret .= "</td>\n"; |
||
| 293 | $ret .= "<td width=\"60%\">\n"; |
||
| 294 | $ret .= $this->renderInfoBox(); |
||
| 295 | $ret .= "</td>\n"; |
||
| 296 | $ret .= "</tr>\n"; |
||
| 297 | // If you use a config label |
||
| 298 | if ($this->_obj->getInfo('min_php') || $this->_obj->getInfo('min_xoops') || !empty($this->_itemConfigBoxLine)) { |
||
| 299 | $ret .= "<tr>\n"; |
||
| 300 | $ret .= "<td colspan=\"2\">\n"; |
||
| 301 | $ret .= "<fieldset><legend class=\"label\">"; |
||
| 302 | $ret .= _AM_MODULEADMIN_CONFIG; |
||
| 303 | $ret .= "</legend>\n"; |
||
| 304 | |||
| 305 | // php version |
||
| 306 | $path = XOOPS_URL . '/Frameworks/moduleclasses/icons/16/'; |
||
| 307 | if ($this->_obj->getInfo('min_php')) { |
||
| 308 | if (version_compare(phpversion(), $this->_obj->getInfo('min_php'), '<')) { |
||
| 309 | $ret .= "<span style='color : red; font-weight : bold;'><img src='" . $path . "0.png' >" . sprintf(_AM_MODULEADMIN_CONFIG_PHP, $this->_obj->getInfo('min_php'), phpversion()) . "</span>\n"; |
||
| 310 | } else { |
||
| 311 | $ret .= "<span style='color : green;'><img src='" . $path . "1.png' >" . sprintf(_AM_MODULEADMIN_CONFIG_PHP, $this->_obj->getInfo('min_php'), phpversion()) . "</span>\n"; |
||
| 312 | } |
||
| 313 | $ret .= '<br>'; |
||
| 314 | } |
||
| 315 | |||
| 316 | // Database version |
||
| 317 | $path = XOOPS_URL . '/Frameworks/moduleclasses/icons/16/'; |
||
| 318 | $dbarray = $this->_obj->getInfo('min_db'); |
||
| 319 | |||
| 320 | if ($dbarray[XOOPS_DB_TYPE]) { |
||
| 321 | // changes from redheadedrod to use connector specific version info |
||
| 322 | switch (XOOPS_DB_TYPE) { |
||
| 323 | // server should be the same in both cases |
||
| 324 | case 'mysql': |
||
| 325 | case 'mysqli': |
||
| 326 | global $xoopsDB; |
||
| 327 | $dbCurrentVersion = $xoopsDB->getServerVersion(); |
||
| 328 | break; |
||
| 329 | //case "pdo": |
||
| 330 | // global $xoopsDB; |
||
| 331 | // $dbCurrentVersion = $xoopsDB->getAttribute(PDO::ATTR_SERVER_VERSION); |
||
| 332 | // break; |
||
| 333 | default: // don't really support anything other than mysql |
||
| 334 | $dbCurrentVersion = '0'; |
||
| 335 | break; |
||
| 336 | } |
||
| 337 | $currentVerParts = explode('.', (string)$dbCurrentVersion); |
||
| 338 | $iCurrentVerParts = array_map('intval', $currentVerParts); |
||
| 339 | $dbRequiredVersion = $dbarray[XOOPS_DB_TYPE]; |
||
| 340 | $reqVerParts = explode('.', (string)$dbRequiredVersion); |
||
| 341 | $iReqVerParts = array_map('intval', $reqVerParts); |
||
| 342 | $icount = $j = count($iReqVerParts); |
||
| 343 | $reqVer = $curVer = 0; |
||
| 344 | for ($i = 0; $i < $icount; ++$i) { |
||
| 345 | $j--; |
||
| 346 | $reqVer += $iReqVerParts[$i] * pow(10, $j); |
||
| 347 | if (isset($iCurrentVerParts[$i])) { |
||
| 348 | $curVer += $iCurrentVerParts[$i] * pow(10, $j); |
||
| 349 | } else { |
||
| 350 | $curVer *= pow(10, $j); |
||
| 351 | } |
||
| 352 | } |
||
| 353 | if ($reqVer > $curVer) { |
||
| 354 | $ret .= "<span style='color : red; font-weight : bold;'><img src='" . $path . "0.png' >" . sprintf(XOOPS_DB_TYPE . ' ' . _AM_MODULEADMIN_CONFIG_DB, $dbRequiredVersion, $dbCurrentVersion) . "</span><br>\n"; |
||
| 355 | } else { |
||
| 356 | $ret .= "<span style='color : green;'><img src='" . $path . "1.png' >" . sprintf(strtoupper(XOOPS_DB_TYPE) . ' ' . _AM_MODULEADMIN_CONFIG_DB, $dbRequiredVersion, $dbCurrentVersion) . "</span><br>\n"; |
||
| 357 | } |
||
| 358 | } |
||
| 359 | |||
| 360 | // xoops version |
||
| 361 | if ($this->_obj->getInfo('min_xoops')) { |
||
| 362 | if (substr(XOOPS_VERSION, 6, strlen(XOOPS_VERSION) - 6) < $this->_obj->getInfo('min_xoops')) { |
||
| 363 | $ret .= "<span style='color : red; font-weight : bold;'><img src='" . $path . "0.png' >" . sprintf(_AM_MODULEADMIN_CONFIG_XOOPS, $this->_obj->getInfo('min_xoops'), substr(XOOPS_VERSION, 6, strlen(XOOPS_VERSION) - 6)) . "</span>\n"; |
||
| 364 | } else { |
||
| 365 | $ret .= "<span style='color : green;'><img src='" . $path . "1.png' >" . sprintf(_AM_MODULEADMIN_CONFIG_XOOPS, $this->_obj->getInfo('min_xoops'), substr(XOOPS_VERSION, 6, strlen(XOOPS_VERSION) - 6)) . "</span>\n"; |
||
| 366 | } |
||
| 367 | $ret .= '<br>'; |
||
| 368 | } |
||
| 369 | |||
| 370 | // ModuleAdmin version |
||
| 371 | if ($this->_obj->getInfo('min_admin')) { |
||
| 372 | if ($this->getVersion() < $this->_obj->getInfo('min_admin')) { |
||
| 373 | $ret .= "<span style='color : red; font-weight : bold;'><img src='" . $path . "0.png' >" . sprintf(_AM_MODULEADMIN_CONFIG_ADMIN, $this->_obj->getInfo('min_admin'), $this->getVersion()) . "</span>\n"; |
||
| 374 | } else { |
||
| 375 | $ret .= "<span style='color : green;'><img src='" . $path . "1.png' >" . sprintf(_AM_MODULEADMIN_CONFIG_ADMIN, $this->_obj->getInfo('min_admin'), $this->getVersion()) . "</span>\n"; |
||
| 376 | } |
||
| 377 | $ret .= '<br>'; |
||
| 378 | } |
||
| 379 | if (!empty($this->_itemConfigBoxLine)) { |
||
| 380 | foreach (array_keys($this->_itemConfigBoxLine) as $i) { |
||
| 381 | $ret .= $this->_itemConfigBoxLine[$i]; |
||
| 382 | $ret .= '<br>'; |
||
| 383 | } |
||
| 384 | } |
||
| 385 | $ret .= "</fieldset>\n"; |
||
| 386 | $ret .= "</td>\n"; |
||
| 387 | $ret .= "</tr>\n"; |
||
| 388 | } |
||
| 389 | $ret .= "</table>\n"; |
||
| 390 | |||
| 391 | return $ret; |
||
| 392 | } |
||
| 393 | //****************************************************************************************************************** |
||
| 394 | // addInfoBox |
||
| 395 | //****************************************************************************************************************** |
||
| 396 | // $title: title of an InfoBox |
||
| 397 | //****************************************************************************************************************** |
||
| 398 | /** |
||
| 399 | * @param $title |
||
| 400 | * |
||
| 401 | * @return bool |
||
| 402 | */ |
||
| 403 | public function addInfoBox($title) |
||
| 411 | //****************************************************************************************************************** |
||
| 412 | // addInfoBoxLine |
||
| 413 | //****************************************************************************************************************** |
||
| 414 | // $label: title of InfoBox Line |
||
| 415 | // $text: |
||
| 416 | // $type: type of config: 1- "default": Just a line with value. |
||
| 417 | // 2- "information": check if this is an folder. |
||
| 418 | // 3- "chmod": check if this is the good chmod. |
||
| 419 | // For this type ("chmod"), the value is an array: array(path, chmod) |
||
| 420 | //****************************************************************************************************************** |
||
| 421 | /** |
||
| 422 | * @param $label |
||
| 423 | * @param $text |
||
| 424 | * @param string $value |
||
| 425 | * @param string $color |
||
| 426 | * @param string $type |
||
| 427 | * |
||
| 428 | * @return bool |
||
| 429 | */ |
||
| 430 | public function addInfoBoxLine($label, $text, $value = '', $color = 'inherit', $type = 'default') |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @return string |
||
| 453 | */ |
||
| 454 | public function renderInfoBox() |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Create HTML text to display on Admin About page |
||
| 476 | * |
||
| 477 | * @param string $business the PAYPAL business email or Merchant Account ID |
||
| 478 | * @param bool $logo_xoops true to display XOOPS logo and link on page |
||
| 479 | * |
||
| 480 | * @return string HTML to display |
||
| 481 | */ |
||
| 482 | public function renderAbout($business = '', $logo_xoops = true) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * @param string $menu |
||
| 580 | * |
||
| 581 | * @return string |
||
| 582 | */ |
||
| 583 | public function addNavigation($menu = '') |
||
| 605 | } |
||
| 606 |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state