Complex classes like modules 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 modules, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | trait modules { |
||
| 23 | /** |
||
| 24 | * @param \cs\Request $Request |
||
| 25 | * |
||
| 26 | * @return mixed |
||
| 27 | * |
||
| 28 | * @throws ExitException |
||
| 29 | */ |
||
| 30 | static function admin_modules_get ($Request) { |
||
| 31 | if ($Request->route_path(3)) { |
||
| 32 | $route_path = $Request->route_path; |
||
| 33 | switch ($route_path[3]) { |
||
| 34 | /** |
||
| 35 | * Get dependent packages for module |
||
| 36 | */ |
||
| 37 | case 'dependent_packages': |
||
| 38 | return static::get_dependent_packages_for_module($route_path[2]); |
||
| 39 | /** |
||
| 40 | * Get dependencies for module (packages, databases, storages) |
||
| 41 | */ |
||
| 42 | case 'dependencies': |
||
| 43 | return static::get_dependencies_for_module($route_path[2]); |
||
| 44 | /** |
||
| 45 | * Get dependencies for module during update |
||
| 46 | */ |
||
| 47 | case 'update_dependencies': |
||
| 48 | return static::get_update_dependencies_for_module($route_path[2]); |
||
| 49 | /** |
||
| 50 | * Get mapping of named module's databases to indexes of system databases |
||
| 51 | */ |
||
| 52 | case 'db': |
||
| 53 | return static::get_module_databases($route_path[2]); |
||
| 54 | /** |
||
| 55 | * Get mapping of named module's storages to indexes of system storages |
||
| 56 | */ |
||
| 57 | case 'storage': |
||
| 58 | return static::get_module_storages($route_path[2]); |
||
| 59 | default: |
||
| 60 | throw new ExitException(400); |
||
| 61 | } |
||
| 62 | } elseif ($Request->route_path(2) == 'default') { |
||
| 63 | /** |
||
| 64 | * Get current default module |
||
| 65 | */ |
||
| 66 | return static::get_default_module(); |
||
| 67 | } else { |
||
| 68 | /** |
||
| 69 | * Get array of modules in extended form |
||
| 70 | */ |
||
| 71 | return static::get_modules_list(); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | /** |
||
| 75 | * @param string $module |
||
| 76 | * |
||
| 77 | * @return string[][] |
||
| 78 | * |
||
| 79 | * @throws ExitException |
||
| 80 | */ |
||
| 81 | protected static function get_dependent_packages_for_module ($module) { |
||
| 82 | if (!Config::instance()->module($module)) { |
||
| 83 | throw new ExitException(404); |
||
| 84 | } |
||
| 85 | $meta_file = MODULES."/$module/meta.json"; |
||
| 86 | return file_exists($meta_file) ? Packages_dependencies::get_dependent_packages(file_get_json($meta_file)) : []; |
||
| 87 | } |
||
| 88 | /** |
||
| 89 | * @param string $module |
||
| 90 | * |
||
| 91 | * @return array |
||
| 92 | * |
||
| 93 | * @throws ExitException |
||
| 94 | */ |
||
| 95 | protected static function get_dependencies_for_module ($module) { |
||
| 96 | if (!Config::instance()->module($module)) { |
||
| 97 | throw new ExitException(404); |
||
| 98 | } |
||
| 99 | $meta_file = MODULES."/$module/meta.json"; |
||
| 100 | return file_exists($meta_file) ? Packages_dependencies::get_dependencies(file_get_json($meta_file)) : []; |
||
| 101 | } |
||
| 102 | /** |
||
| 103 | * @param string $module |
||
| 104 | * |
||
| 105 | * @return array |
||
| 106 | * |
||
| 107 | * @throws ExitException |
||
| 108 | */ |
||
| 109 | protected static function get_update_dependencies_for_module ($module) { |
||
| 110 | if (!Config::instance()->module($module)) { |
||
| 111 | throw new ExitException(404); |
||
| 112 | } |
||
| 113 | $tmp_location = TEMP.'/System/admin/'.Session::instance()->get_id().'.phar'; |
||
| 114 | $tmp_dir = "phar://$tmp_location"; |
||
| 115 | if ( |
||
| 116 | !file_exists(MODULES."/$module/meta.json") || |
||
| 117 | !file_exists("$tmp_dir/meta.json") |
||
| 118 | ) { |
||
| 119 | throw new ExitException(400); |
||
| 120 | } |
||
| 121 | $new_meta = file_get_json("$tmp_dir/meta.json"); |
||
| 122 | if (!static::is_same_module($new_meta, $module)) { |
||
| 123 | throw new ExitException(Language::prefix('system_admin_modules_')->this_is_not_module_installer_file, 400); |
||
| 124 | } |
||
| 125 | return Packages_dependencies::get_dependencies($new_meta); |
||
| 126 | } |
||
| 127 | /** |
||
| 128 | * @param array $meta |
||
| 129 | * @param string $module |
||
| 130 | * |
||
| 131 | * @return bool |
||
| 132 | */ |
||
| 133 | protected static function is_same_module ($meta, $module) { |
||
| 136 | /** |
||
| 137 | * @param string $module |
||
| 138 | * |
||
| 139 | * @return array |
||
| 140 | * |
||
| 141 | * @throws ExitException |
||
| 142 | */ |
||
| 143 | protected static function get_module_databases ($module) { |
||
| 144 | $Config = Config::instance(); |
||
| 145 | if (!isset($Config->components['modules'][$module]['db'])) { |
||
| 146 | throw new ExitException(404); |
||
| 147 | } |
||
| 148 | return $Config->components['modules'][$module]['db']; |
||
| 149 | } |
||
| 150 | /** |
||
| 151 | * @param string $module |
||
| 152 | * |
||
| 153 | * @return array |
||
| 154 | * |
||
| 155 | * @throws ExitException |
||
| 156 | */ |
||
| 157 | protected static function get_module_storages ($module) { |
||
| 158 | $Config = Config::instance(); |
||
| 159 | if (!isset($Config->components['modules'][$module]['storage'])) { |
||
| 160 | throw new ExitException(404); |
||
| 161 | } |
||
| 162 | return $Config->components['modules'][$module]['storage']; |
||
| 163 | } |
||
| 164 | protected static function get_modules_list () { |
||
| 165 | $Config = Config::instance(); |
||
| 166 | $modules_list = []; |
||
| 167 | foreach ($Config->components['modules'] as $module_name => &$module_data) { |
||
| 168 | $module = [ |
||
| 169 | 'active' => $module_data['active'], |
||
| 170 | 'name' => $module_name, |
||
| 171 | 'has_user_section' => file_exists_with_extension(MODULES."/$module_name/index", ['php', 'html', 'json']), |
||
| 172 | 'has_admin_section' => file_exists_with_extension(MODULES."/$module_name/admin/index", ['php', 'json']) |
||
| 173 | ]; |
||
| 174 | /** |
||
| 175 | * Check if API available |
||
| 176 | */ |
||
| 177 | static::check_module_feature_availability($module, 'readme', 'api'); |
||
| 178 | /** |
||
| 179 | * Check if readme available |
||
| 180 | */ |
||
| 181 | static::check_module_feature_availability($module, 'readme'); |
||
| 182 | /** |
||
| 183 | * Check if license available |
||
| 184 | */ |
||
| 185 | static::check_module_feature_availability($module, 'license'); |
||
| 186 | if (file_exists(MODULES."/$module_name/meta.json")) { |
||
| 187 | $module['meta'] = file_get_json(MODULES."/$module_name/meta.json"); |
||
| 188 | } |
||
| 189 | $modules_list[] = $module; |
||
| 190 | } |
||
| 191 | return $modules_list; |
||
| 192 | } |
||
| 193 | /** |
||
| 194 | * @param array $module |
||
| 195 | * @param string $feature |
||
| 196 | * @param string $dir |
||
| 197 | */ |
||
| 198 | protected static function check_module_feature_availability (&$module, $feature, $dir = '') { |
||
| 212 | /** |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | protected static function get_default_module () { |
||
| 216 | return Config::instance()->core['default_module']; |
||
| 217 | } |
||
| 218 | /** |
||
| 219 | * @param \cs\Request $Request |
||
| 220 | * |
||
| 221 | * @throws ExitException |
||
| 222 | */ |
||
| 223 | static function admin_modules_put ($Request) { |
||
| 224 | if ($Request->route_path(3)) { |
||
| 225 | $module = $Request->route_path[2]; |
||
| 226 | switch ($Request->route_path[3]) { |
||
| 227 | /** |
||
| 228 | * Set mapping of named module's databases to indexes of system databases |
||
| 229 | */ |
||
| 230 | case 'db': |
||
| 231 | static::set_module_databases($Request, $module); |
||
| 232 | break; |
||
| 233 | /** |
||
| 234 | * Set mapping of named module's storages to indexes of system storages |
||
| 235 | */ |
||
| 236 | case 'storage': |
||
| 237 | static::set_module_storages($Request, $module); |
||
| 238 | break; |
||
| 239 | default: |
||
| 240 | throw new ExitException(400); |
||
| 241 | } |
||
| 242 | } elseif ($Request->route_path(2) == 'default') { |
||
| 243 | /** |
||
| 244 | * Set current default module |
||
| 245 | */ |
||
| 246 | static::set_default_module($Request->data('module')); |
||
| 247 | } else { |
||
| 248 | throw new ExitException(400); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | /** |
||
| 252 | * @param \cs\Request $Request |
||
| 253 | * @param string $module |
||
| 254 | * |
||
| 255 | * @throws ExitException |
||
| 256 | */ |
||
| 257 | protected static function set_module_databases ($Request, $module) { |
||
| 258 | $Config = Config::instance(); |
||
| 259 | $database_config = $Request->data('db'); |
||
| 260 | if (!$database_config || !isset($Config->components['modules'][$module]['db'])) { |
||
| 261 | throw new ExitException(404); |
||
| 262 | } |
||
| 263 | $Config->components['modules'][$module]['db'] = _int($database_config); |
||
| 264 | static::admin_modules_save(); |
||
| 265 | } |
||
| 266 | /** |
||
| 267 | * @param \cs\Request $Request |
||
| 268 | * @param string $module |
||
| 269 | * |
||
| 270 | * @throws ExitException |
||
| 271 | */ |
||
| 272 | protected static function set_module_storages ($Request, $module) { |
||
| 273 | $Config = Config::instance(); |
||
| 274 | $storage_config = $Request->data('storage'); |
||
| 275 | if (!$storage_config || !isset($Config->components['modules'][$module]['storage'])) { |
||
| 276 | throw new ExitException(404); |
||
| 277 | } |
||
| 278 | $Config->components['modules'][$module]['storage'] = _int($storage_config); |
||
| 279 | static::admin_modules_save(); |
||
| 280 | } |
||
| 281 | /** |
||
| 282 | * @param string $module |
||
| 283 | * |
||
| 284 | * @throws ExitException |
||
| 285 | */ |
||
| 286 | protected static function set_default_module ($module) { |
||
| 305 | /** |
||
| 306 | * Enable module |
||
| 307 | * |
||
| 308 | * Provides next events: |
||
| 309 | * admin/System/components/modules/enable/before |
||
| 310 | * ['name' => module_name] |
||
| 311 | * |
||
| 312 | * admin/System/components/modules/enable/after |
||
| 313 | * ['name' => module_name] |
||
| 314 | * |
||
| 315 | * @param \cs\Request $Request |
||
| 316 | * |
||
| 317 | * @throws ExitException |
||
| 318 | */ |
||
| 319 | static function admin_modules_enable ($Request) { |
||
| 320 | $Config = Config::instance(); |
||
| 321 | $module = $Request->route_path(2); |
||
| 322 | if (!$Config->module($module)->disabled()) { |
||
| 323 | throw new ExitException(400); |
||
| 324 | } |
||
| 325 | if (!Event::instance()->fire('admin/System/components/modules/enable/before', ['name' => $module])) { |
||
| 326 | throw new ExitException(500); |
||
| 327 | } |
||
| 328 | $Config->components['modules'][$module]['active'] = Config\Module_Properties::ENABLED; |
||
| 329 | static::admin_modules_save(); |
||
| 330 | Event::instance()->fire('admin/System/components/modules/enable/after', ['name' => $module]); |
||
| 331 | static::admin_modules_cleanup(); |
||
| 332 | } |
||
| 333 | protected static function admin_modules_cleanup () { |
||
| 343 | /** |
||
| 344 | * Disable module |
||
| 345 | * |
||
| 346 | * Provides next events: |
||
| 347 | * admin/System/components/modules/disable/before |
||
| 348 | * ['name' => module_name] |
||
| 349 | * |
||
| 350 | * admin/System/components/modules/disable/after |
||
| 351 | * ['name' => module_name] |
||
| 352 | * |
||
| 353 | * @param \cs\Request $Request |
||
| 354 | * |
||
| 355 | * @throws ExitException |
||
| 356 | */ |
||
| 357 | static function admin_modules_disable ($Request) { |
||
| 369 | /** |
||
| 370 | * @param Config $Config |
||
| 371 | * @param string $module |
||
| 372 | * |
||
| 373 | * @throws ExitException |
||
| 374 | */ |
||
| 375 | protected static function admin_modules_disable_internal ($Config, $module) { |
||
| 384 | /** |
||
| 385 | * Install module |
||
| 386 | * |
||
| 387 | * Provides next events: |
||
| 388 | * admin/System/components/modules/install/before |
||
| 389 | * ['name' => module_name] |
||
| 390 | * |
||
| 391 | * admin/System/components/modules/install/after |
||
| 392 | * ['name' => module_name] |
||
| 393 | * |
||
| 394 | * @param \cs\Request $Request |
||
| 395 | * |
||
| 396 | * @throws ExitException |
||
| 397 | */ |
||
| 398 | static function admin_modules_install ($Request) { |
||
| 422 | /** |
||
| 423 | * Uninstall module |
||
| 424 | * |
||
| 425 | * Provides next events: |
||
| 426 | * admin/System/components/modules/uninstall/before |
||
| 427 | * ['name' => module_name] |
||
| 428 | * |
||
| 429 | * admin/System/components/modules/uninstall/after |
||
| 430 | * ['name' => module_name] |
||
| 431 | * |
||
| 432 | * @param \cs\Request $Request |
||
| 433 | * |
||
| 434 | * @throws ExitException |
||
| 435 | */ |
||
| 436 | static function admin_modules_uninstall ($Request) { |
||
| 459 | /** |
||
| 460 | * @param string $module |
||
| 461 | */ |
||
| 462 | protected static function delete_permissions_for_module ($module) { |
||
| 475 | /** |
||
| 476 | * Extract uploaded module |
||
| 477 | * |
||
| 478 | * @throws ExitException |
||
| 479 | */ |
||
| 480 | static function admin_modules_extract () { |
||
| 503 | /** |
||
| 504 | * Update module (or system if module name is System) |
||
| 505 | * |
||
| 506 | * @param \cs\Request $Request |
||
| 507 | * |
||
| 508 | * @throws ExitException |
||
| 509 | */ |
||
| 510 | static function admin_modules_update ($Request) { |
||
| 534 | /** |
||
| 535 | * Provides next events: |
||
| 536 | * admin/System/components/modules/update/before |
||
| 537 | * ['name' => module_name] |
||
| 538 | * |
||
| 539 | * admin/System/components/modules/update/after |
||
| 540 | * ['name' => module_name] |
||
| 541 | * |
||
| 542 | * @param string $module |
||
| 543 | * @param array $existing_meta |
||
| 544 | * @param array $new_meta |
||
| 545 | * @param string $tmp_location |
||
| 546 | * @param \cs\Request $Request |
||
| 547 | * |
||
| 548 | * @throws ExitException |
||
| 549 | */ |
||
| 550 | protected static function update_module ($module, $existing_meta, $new_meta, $tmp_location, $Request) { |
||
| 580 | /** |
||
| 581 | * Provides next events: |
||
| 582 | * admin/System/components/modules/update_system/before |
||
| 583 | * |
||
| 584 | * admin/System/components/modules/update_system/after |
||
| 585 | * |
||
| 586 | * @param string $module |
||
| 587 | * @param array $existing_meta |
||
| 588 | * @param array $new_meta |
||
| 589 | * @param string $tmp_location |
||
| 590 | * |
||
| 591 | * @throws ExitException |
||
| 592 | */ |
||
| 593 | protected static function update_system ($module, $existing_meta, $new_meta, $tmp_location) { |
||
| 627 | /** |
||
| 628 | * Delete module completely |
||
| 629 | * |
||
| 630 | * @param \cs\Request $Request |
||
| 631 | * |
||
| 632 | * @throws ExitException |
||
| 633 | */ |
||
| 634 | static function admin_modules_delete ($Request) { |
||
| 646 | /** |
||
| 647 | * Update information about present modules |
||
| 648 | * |
||
| 649 | * @throws ExitException |
||
| 650 | */ |
||
| 651 | static function admin_modules_update_list () { |
||
| 687 | /** |
||
| 688 | * Save changes |
||
| 689 | * |
||
| 690 | * @throws ExitException |
||
| 691 | */ |
||
| 692 | protected static function admin_modules_save () { |
||
| 697 | } |
||
| 698 |