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 | * @throws ExitException |
||
27 | */ |
||
28 | static function admin_modules_get ($Request) { |
||
29 | if ($Request->route_path(3)) { |
||
30 | $route_path = $Request->route_path; |
||
31 | switch ($route_path[3]) { |
||
32 | /** |
||
33 | * Get dependent packages for module |
||
34 | */ |
||
35 | case 'dependent_packages': |
||
36 | static::get_dependent_packages_for_module($route_path[2]); |
||
37 | break; |
||
38 | /** |
||
39 | * Get dependencies for module (packages, databases, storages) |
||
40 | */ |
||
41 | case 'dependencies': |
||
42 | static::get_dependencies_for_module($route_path[2]); |
||
43 | break; |
||
44 | /** |
||
45 | * Get dependencies for module during update |
||
46 | */ |
||
47 | case 'update_dependencies': |
||
48 | static::get_update_dependencies_for_module($route_path[2]); |
||
49 | break; |
||
50 | /** |
||
51 | * Get mapping of named module's databases to indexes of system databases |
||
52 | */ |
||
53 | case 'db': |
||
54 | static::get_module_databases($route_path[2]); |
||
55 | break; |
||
56 | /** |
||
57 | * Get mapping of named module's storages to indexes of system storages |
||
58 | */ |
||
59 | case 'storage': |
||
60 | static::get_module_storages($route_path[2]); |
||
61 | break; |
||
62 | default: |
||
63 | throw new ExitException(400); |
||
64 | } |
||
65 | } elseif ($Request->route_path(2) == 'default') { |
||
66 | /** |
||
67 | * Get current default module |
||
68 | */ |
||
69 | static::get_default_module(); |
||
70 | } else { |
||
71 | /** |
||
72 | * Get array of modules in extended form |
||
73 | */ |
||
74 | static::get_modules_list(); |
||
75 | } |
||
76 | } |
||
77 | /** |
||
78 | * @param string $module |
||
79 | * |
||
80 | * @throws ExitException |
||
81 | */ |
||
82 | protected static function get_dependent_packages_for_module ($module) { |
||
91 | /** |
||
92 | * @param string $module |
||
93 | * |
||
94 | * @throws ExitException |
||
95 | */ |
||
96 | protected static function get_dependencies_for_module ($module) { |
||
105 | /** |
||
106 | * @param string $module |
||
107 | * |
||
108 | * @throws ExitException |
||
109 | */ |
||
110 | protected static function get_update_dependencies_for_module ($module) { |
||
130 | /** |
||
131 | * @param array $meta |
||
132 | * @param string $module |
||
133 | * |
||
134 | * @return bool |
||
135 | */ |
||
136 | protected static function is_same_module ($meta, $module) { |
||
139 | /** |
||
140 | * @param string $module |
||
141 | * |
||
142 | * @throws ExitException |
||
143 | */ |
||
144 | protected static function get_module_databases ($module) { |
||
153 | /** |
||
154 | * @param string $module |
||
155 | * |
||
156 | * @throws ExitException |
||
157 | */ |
||
158 | protected static function get_module_storages ($module) { |
||
197 | /** |
||
198 | * @param array $module |
||
199 | * @param string $feature |
||
200 | * @param string $dir |
||
201 | */ |
||
202 | protected static function check_module_feature_availability (&$module, $feature, $dir = '') { |
||
221 | /** |
||
222 | * @param \cs\Request $Request |
||
223 | * |
||
224 | * @throws ExitException |
||
225 | */ |
||
226 | static function admin_modules_put ($Request) { |
||
254 | /** |
||
255 | * @param string $module |
||
256 | * |
||
257 | * @throws ExitException |
||
258 | */ |
||
259 | protected static function set_module_databases ($module) { |
||
267 | /** |
||
268 | * @param string $module |
||
269 | * |
||
270 | * @throws ExitException |
||
271 | */ |
||
272 | protected static function set_module_storages ($module) { |
||
280 | /** |
||
281 | * @param string $module |
||
282 | * |
||
283 | * @throws ExitException |
||
284 | */ |
||
285 | protected static function set_default_module ($module) { |
||
304 | /** |
||
305 | * Enable module |
||
306 | * |
||
307 | * Provides next events: |
||
308 | * admin/System/components/modules/enable/before |
||
309 | * ['name' => module_name] |
||
310 | * |
||
311 | * admin/System/components/modules/enable/after |
||
312 | * ['name' => module_name] |
||
313 | * |
||
314 | * @param \cs\Request $Request |
||
315 | * |
||
316 | * @throws ExitException |
||
317 | */ |
||
318 | static function admin_modules_enable ($Request) { |
||
342 | /** |
||
343 | * Disable module |
||
344 | * |
||
345 | * Provides next events: |
||
346 | * admin/System/components/modules/disable/before |
||
347 | * ['name' => module_name] |
||
348 | * |
||
349 | * admin/System/components/modules/disable/after |
||
350 | * ['name' => module_name] |
||
351 | * |
||
352 | * @param \cs\Request $Request |
||
353 | * |
||
354 | * @throws ExitException |
||
355 | */ |
||
356 | static function admin_modules_disable ($Request) { |
||
374 | /** |
||
375 | * Install module |
||
376 | * |
||
377 | * Provides next events: |
||
378 | * admin/System/components/modules/install/before |
||
379 | * ['name' => module_name] |
||
380 | * |
||
381 | * admin/System/components/modules/install/after |
||
382 | * ['name' => module_name] |
||
383 | * |
||
384 | * @param \cs\Request $Request |
||
385 | * |
||
386 | * @throws ExitException |
||
387 | */ |
||
388 | static function admin_modules_install ($Request) { |
||
410 | /** |
||
411 | * Uninstall module |
||
412 | * |
||
413 | * Provides next events: |
||
414 | * admin/System/components/modules/uninstall/before |
||
415 | * ['name' => module_name] |
||
416 | * |
||
417 | * admin/System/components/modules/uninstall/after |
||
418 | * ['name' => module_name] |
||
419 | * |
||
420 | * @param \cs\Request $Request |
||
421 | * |
||
422 | * @throws ExitException |
||
423 | */ |
||
424 | static function admin_modules_uninstall ($Request) { |
||
447 | /** |
||
448 | * @param string $module |
||
449 | */ |
||
450 | protected static function delete_permissions_for_module ($module) { |
||
463 | /** |
||
464 | * Extract uploaded module |
||
465 | * |
||
466 | * @throws ExitException |
||
467 | */ |
||
468 | static function admin_modules_extract () { |
||
491 | /** |
||
492 | * Update module (or system if module name is System) |
||
493 | * |
||
494 | * @param \cs\Request $Request |
||
495 | * |
||
496 | * @throws ExitException |
||
497 | */ |
||
498 | static function admin_modules_update ($Request) { |
||
522 | /** |
||
523 | * Provides next events: |
||
524 | * admin/System/components/modules/update/before |
||
525 | * ['name' => module_name] |
||
526 | * |
||
527 | * admin/System/components/modules/update/after |
||
528 | * ['name' => module_name] |
||
529 | * |
||
530 | * @param string $module |
||
531 | * @param array $existing_meta |
||
532 | * @param array $new_meta |
||
533 | * @param string $tmp_location |
||
534 | * @param \cs\Request $Request |
||
535 | * |
||
536 | * @throws ExitException |
||
537 | */ |
||
538 | protected static function update_module ($module, $existing_meta, $new_meta, $tmp_location, $Request) { |
||
568 | /** |
||
569 | * Provides next events: |
||
570 | * admin/System/components/modules/update_system/before |
||
571 | * |
||
572 | * admin/System/components/modules/update_system/after |
||
573 | * |
||
574 | * @param string $module |
||
575 | * @param array $existing_meta |
||
576 | * @param array $new_meta |
||
577 | * @param string $tmp_location |
||
578 | * |
||
579 | * @throws ExitException |
||
580 | */ |
||
581 | protected static function update_system ($module, $existing_meta, $new_meta, $tmp_location) { |
||
615 | /** |
||
616 | * Delete module completely |
||
617 | * |
||
618 | * @param \cs\Request $Request |
||
619 | * |
||
620 | * @throws ExitException |
||
621 | */ |
||
622 | static function admin_modules_delete ($Request) { |
||
637 | /** |
||
638 | * Update information about present modules |
||
639 | * |
||
640 | * @throws ExitException |
||
641 | */ |
||
642 | static function admin_modules_update_list () { |
||
678 | /** |
||
679 | * Save changes |
||
680 | * |
||
681 | * @throws ExitException |
||
682 | */ |
||
683 | protected static function admin_modules_save () { |
||
688 | } |
||
689 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.