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 Admin 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 Admin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Admin |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Set module directory |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | public $tplModule = 'system'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Template call for each render parts |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | public $tplFile = array( |
||
| 40 | 'index' => 'admin_index.tpl', |
||
| 41 | 'about' => 'admin_about.tpl', |
||
| 42 | 'infobox' => 'admin_infobox.tpl', |
||
| 43 | 'bread' => 'admin_breadcrumb.tpl', |
||
| 44 | 'button' => 'admin_buttons.tpl', |
||
| 45 | 'tips' => 'admin_tips.tpl', |
||
| 46 | 'nav' => 'admin_navigation.tpl', |
||
| 47 | ); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Tips to display in admin page |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private $tips = ''; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * List of button |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | private $itemButton = array(); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * List of Info Box |
||
| 65 | * |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | private $itemInfoBox = array(); |
||
| 69 | |||
| 70 | /** |
||
| 71 | * List of line of an Info Box |
||
| 72 | * |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | private $itemConfigBoxLine = array(); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Breadcrumb data |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | private $bread = array(); |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Current module object |
||
| 86 | * |
||
| 87 | * @var \Xoops\Core\Kernel\Handlers\XoopsModule $module |
||
| 88 | */ |
||
| 89 | private $module = null; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Constructor |
||
| 93 | */ |
||
| 94 | public function __construct() |
||
| 95 | { |
||
| 96 | $xoops = \Xoops::getInstance(); |
||
| 97 | $this->module = $xoops->module; |
||
| 98 | $xoops->theme()->addStylesheet('media/xoops/css/moduladmin.css'); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Add breadcrumb menu |
||
| 103 | * |
||
| 104 | * @param string $title title |
||
| 105 | * @param string $link url |
||
| 106 | * @param bool $home is home |
||
| 107 | * |
||
| 108 | * @return void |
||
| 109 | */ |
||
| 110 | public function addBreadcrumbLink($title = '', $link = '', $home = false) |
||
| 111 | { |
||
| 112 | if ($title != '') { |
||
| 113 | $this->bread[] = array( |
||
| 114 | 'link' => $link, 'title' => $title, 'home' => $home |
||
| 115 | ); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Add config line |
||
| 121 | * |
||
| 122 | * @param string $value line value - a string or array of values |
||
| 123 | * @param string $type type of line default, folder, chmod, extension, module |
||
| 124 | * Or, type value for itemConfigBoxLine -- accept, warning, error |
||
| 125 | * |
||
| 126 | * @return bool |
||
| 127 | */ |
||
| 128 | public function addConfigBoxLine($value = '', $type = 'default') |
||
| 129 | { |
||
| 130 | switch ($type) { |
||
| 131 | default: |
||
| 132 | case "default": |
||
| 133 | $this->itemConfigBoxLine[] = array('type' => $type, 'text' => $value); |
||
| 134 | break; |
||
| 135 | |||
| 136 | case "folder": |
||
| 137 | if (!is_dir($value)) { |
||
| 138 | $this->itemConfigBoxLine[] = array( |
||
| 139 | 'type' => 'error', 'text' => sprintf(\XoopsLocale::EF_FOLDER_DOES_NOT_EXIST, $value) |
||
| 140 | ); |
||
| 141 | } else { |
||
| 142 | $this->itemConfigBoxLine[] = array( |
||
| 143 | 'type' => 'accept', 'text' => sprintf(\XoopsLocale::SF_FOLDER_EXISTS, $value) |
||
| 144 | ); |
||
| 145 | } |
||
| 146 | break; |
||
| 147 | |||
| 148 | case "chmod": |
||
| 149 | if (is_dir($value[0])) { |
||
| 150 | if (substr(decoct(fileperms($value[0])), 2) != $value[1]) { |
||
| 151 | $this->itemConfigBoxLine[] = array( |
||
| 152 | 'type' => 'error', |
||
| 153 | 'text' => sprintf( |
||
| 154 | \XoopsLocale::EF_FOLDER_MUST_BE_WITH_CHMOD, |
||
| 155 | $value[0], |
||
| 156 | $value[1], |
||
| 157 | substr(decoct(fileperms($value[0])), 2) |
||
| 158 | ) |
||
| 159 | ); |
||
| 160 | } else { |
||
| 161 | $this->itemConfigBoxLine[] = array( |
||
| 162 | 'type' => 'accept', |
||
| 163 | 'text' => sprintf( |
||
| 164 | \XoopsLocale::EF_FOLDER_MUST_BE_WITH_CHMOD, |
||
| 165 | $value[0], |
||
| 166 | $value[1], |
||
| 167 | substr(decoct(fileperms($value[0])), 2) |
||
| 168 | ) |
||
| 169 | ); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | break; |
||
| 173 | |||
| 174 | View Code Duplication | case "extension": |
|
| 175 | $xoops = \Xoops::getInstance(); |
||
| 176 | if (is_array($value)) { |
||
| 177 | $text = $value[0]; |
||
| 178 | $type = $value[1]; |
||
| 179 | } else { |
||
| 180 | $text = $value; |
||
| 181 | $type = 'error'; |
||
| 182 | } |
||
| 183 | if ($xoops->isActiveModule($text) == false) { |
||
|
|
|||
| 184 | $this->itemConfigBoxLine[] = array( |
||
| 185 | 'type' => $type, 'text' => sprintf(\XoopsLocale::EF_EXTENSION_IS_NOT_INSTALLED, $text) |
||
| 186 | ); |
||
| 187 | } else { |
||
| 188 | $this->itemConfigBoxLine[] = array( |
||
| 189 | 'type' => 'accept', 'text' => sprintf(\XoopsLocale::SF_EXTENSION_IS_INSTALLED, $text) |
||
| 190 | ); |
||
| 191 | } |
||
| 192 | break; |
||
| 193 | |||
| 194 | View Code Duplication | case "module": |
|
| 195 | $xoops = \Xoops::getInstance(); |
||
| 196 | if (is_array($value)) { |
||
| 197 | $text = $value[0]; |
||
| 198 | $type = $value[1]; |
||
| 199 | } else { |
||
| 200 | $text = $value; |
||
| 201 | $type = 'error'; |
||
| 202 | } |
||
| 203 | if ($xoops->isActiveModule($text) == false) { |
||
| 204 | $this->itemConfigBoxLine[] = array( |
||
| 205 | 'type' => $type, 'text' => sprintf(\XoopsLocale::F_MODULE_IS_NOT_INSTALLED, $text) |
||
| 206 | ); |
||
| 207 | } else { |
||
| 208 | $this->itemConfigBoxLine[] = array( |
||
| 209 | 'type' => 'accept', 'text' => sprintf(\XoopsLocale::F_MODULE_IS_INSTALLED, $text) |
||
| 210 | ); |
||
| 211 | } |
||
| 212 | break; |
||
| 213 | |||
| 214 | View Code Duplication | case "service": |
|
| 215 | $xoops = \Xoops::getInstance(); |
||
| 216 | if (is_array($value)) { |
||
| 217 | $text = $value[0]; |
||
| 218 | $type = $value[1]; |
||
| 219 | } else { |
||
| 220 | $text = $value; |
||
| 221 | $type = 'error'; |
||
| 222 | } |
||
| 223 | if ($xoops->service($text)->isAvailable()) { |
||
| 224 | $this->itemConfigBoxLine[] = array( |
||
| 225 | 'type' => 'accept', 'text' => sprintf(\XoopsLocale::SF_SERVICE_IS_INSTALLED, $text) |
||
| 226 | ); |
||
| 227 | } else { |
||
| 228 | $this->itemConfigBoxLine[] = array( |
||
| 229 | 'type' => $type, 'text' => sprintf(\XoopsLocale::EF_SERVICE_IS_NOT_INSTALLED, $text) |
||
| 230 | ); |
||
| 231 | } |
||
| 232 | break; |
||
| 233 | |||
| 234 | } |
||
| 235 | return true; |
||
| 236 | } |
||
| 237 | |||
| 238 | // simplified config aliases |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Add error to config box |
||
| 242 | * |
||
| 243 | * @param string $value the error message |
||
| 244 | * |
||
| 245 | * @return bool |
||
| 246 | */ |
||
| 247 | public function addConfigError($value = '') |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Add accept (OK) message to config box |
||
| 254 | * |
||
| 255 | * @param string $value the OK message |
||
| 256 | * |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | public function addConfigAccept($value = '') |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Add warning to config box |
||
| 266 | * |
||
| 267 | * @param string $value the warning message |
||
| 268 | * |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | public function addConfigWarning($value = '') |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Check for installed module and version and to configuration box |
||
| 278 | * |
||
| 279 | * @param string $moddir module directory name |
||
| 280 | * @param integer $minversion minimum acceptable module version (100 = V1.00) |
||
| 281 | * |
||
| 282 | * @return bool true if requested version of the module is available |
||
| 283 | */ |
||
| 284 | View Code Duplication | public function addConfigModuleVersion($moddir, $minversion) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Add Info box |
||
| 318 | * Template in htdocs/modules/system/templates/admin/admin_infobox.tpl |
||
| 319 | * |
||
| 320 | * @param string $title title |
||
| 321 | * @param string $type type will be used as icon name in title in template |
||
| 322 | * @param string $extra extra added to the div element that surrounds the box |
||
| 323 | * |
||
| 324 | * @return bool |
||
| 325 | */ |
||
| 326 | public function addInfoBox($title, $type = 'default', $extra = '') |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Add line to the info box |
||
| 337 | * |
||
| 338 | * @param string $text title |
||
| 339 | * @param string $type type |
||
| 340 | * @param string $color color |
||
| 341 | * |
||
| 342 | * @return bool |
||
| 343 | */ |
||
| 344 | public function addInfoBoxLine($text = '', $type = 'default', $color = 'inherit') |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Add Item button |
||
| 361 | * |
||
| 362 | * @param string $title title |
||
| 363 | * @param string $link link |
||
| 364 | * @param string $icon icon |
||
| 365 | * @param string $extra extra |
||
| 366 | * |
||
| 367 | * @return bool |
||
| 368 | */ |
||
| 369 | public function addItemButton($title, $link, $icon = 'add', $extra = '') |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Add a tips |
||
| 381 | * |
||
| 382 | * @param string $text text |
||
| 383 | * |
||
| 384 | * @return void |
||
| 385 | */ |
||
| 386 | public function addTips($text = '') |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Construct template path |
||
| 393 | * |
||
| 394 | * @param string $type type |
||
| 395 | * |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | private function getTplPath($type = '') |
||
| 402 | |||
| 403 | /** |
||
| 404 | * renderBreadcrumb |
||
| 405 | * |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | public function renderBreadcrumb() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * displayBreadcrumb |
||
| 417 | * |
||
| 418 | * @return void |
||
| 419 | */ |
||
| 420 | public function displayBreadcrumb() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Render all items buttons |
||
| 427 | * |
||
| 428 | * @param string $position position |
||
| 429 | * @param string $delimiter delimiter |
||
| 430 | * |
||
| 431 | * @return string |
||
| 432 | */ |
||
| 433 | public function renderButton($position = "floatright", $delimiter = " ") |
||
| 442 | |||
| 443 | /** |
||
| 444 | * displayButton |
||
| 445 | * |
||
| 446 | * @param string $position position |
||
| 447 | * @param string $delimiter delimiter |
||
| 448 | * |
||
| 449 | * @return void |
||
| 450 | */ |
||
| 451 | public function displayButton($position = "floatright", $delimiter = " ") |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Render InfoBox |
||
| 458 | * |
||
| 459 | * @return string |
||
| 460 | */ |
||
| 461 | public function renderInfoBox() |
||
| 467 | |||
| 468 | /** |
||
| 469 | * displayInfoBox |
||
| 470 | * |
||
| 471 | * @return void |
||
| 472 | */ |
||
| 473 | public function displayInfoBox() |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Render index page for admin |
||
| 480 | * |
||
| 481 | * @return string |
||
| 482 | */ |
||
| 483 | public function renderIndex() |
||
| 614 | |||
| 615 | /** |
||
| 616 | * displayIndex |
||
| 617 | * |
||
| 618 | * @return void |
||
| 619 | */ |
||
| 620 | public function displayIndex() |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Render navigation to admin page |
||
| 627 | * |
||
| 628 | * @param string $menu current menu |
||
| 629 | * |
||
| 630 | * @return array |
||
| 631 | */ |
||
| 632 | public function renderNavigation($menu = '') |
||
| 653 | |||
| 654 | /** |
||
| 655 | * displayNavigation |
||
| 656 | * |
||
| 657 | * @param string $menu current menu |
||
| 658 | * |
||
| 659 | * @return void |
||
| 660 | */ |
||
| 661 | public function displayNavigation($menu = '') |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Render tips to admin page |
||
| 671 | * |
||
| 672 | * @return string |
||
| 673 | */ |
||
| 674 | public function renderTips() |
||
| 680 | |||
| 681 | /** |
||
| 682 | * displayTips |
||
| 683 | * |
||
| 684 | * @return void |
||
| 685 | */ |
||
| 686 | public function displayTips() |
||
| 690 | |||
| 691 | /** |
||
| 692 | * Render about page |
||
| 693 | * |
||
| 694 | * @param bool $logo_xoops show logo |
||
| 695 | * |
||
| 696 | * @return bool|mixed|string |
||
| 697 | */ |
||
| 698 | public function renderAbout($logo_xoops = true) |
||
| 769 | |||
| 770 | /** |
||
| 771 | * displayAbout |
||
| 772 | * |
||
| 773 | * @param bool $logo_xoops display logo |
||
| 774 | * |
||
| 775 | * @return void |
||
| 776 | */ |
||
| 777 | public function displayAbout($logo_xoops = true) |
||
| 781 | } |
||
| 782 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.