Complex classes like plugins 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 plugins, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | trait plugins { |
||
22 | /** |
||
23 | * @param \cs\Request $Request |
||
24 | * |
||
25 | * @throws ExitException |
||
26 | */ |
||
27 | static function admin_plugins_get ($Request) { |
||
28 | if ($Request->route_path(3)) { |
||
29 | $route_path = $Request->route_path; |
||
30 | switch ($route_path[3]) { |
||
31 | /** |
||
32 | * Get dependent packages for plugin |
||
33 | */ |
||
34 | case 'dependent_packages': |
||
35 | static::get_dependent_packages_for_plugin($route_path[2]); |
||
36 | return; |
||
37 | /** |
||
38 | * Get dependencies for plugin |
||
39 | */ |
||
40 | case 'dependencies': |
||
41 | static::get_dependencies_for_plugin($route_path[2]); |
||
42 | return; |
||
43 | /** |
||
44 | * Get dependencies for plugin during update |
||
45 | */ |
||
46 | case 'update_dependencies': |
||
47 | static::get_update_dependencies_for_plugin($route_path[2]); |
||
48 | return; |
||
49 | default: |
||
50 | throw new ExitException(400); |
||
51 | } |
||
52 | } |
||
53 | /** |
||
54 | * Get array of plugins in extended form |
||
55 | */ |
||
56 | static::get_plugins_list(); |
||
57 | } |
||
58 | /** |
||
59 | * @param string $plugin |
||
60 | * |
||
61 | * @throws ExitException |
||
62 | */ |
||
63 | protected static function get_dependent_packages_for_plugin ($plugin) { |
||
72 | /** |
||
73 | * @param string $plugin |
||
74 | * |
||
75 | * @throws ExitException |
||
76 | */ |
||
77 | protected static function get_dependencies_for_plugin ($plugin) { |
||
87 | /** |
||
88 | * @param string $plugin |
||
89 | * |
||
90 | * @throws ExitException |
||
91 | */ |
||
92 | protected static function get_update_dependencies_for_plugin ($plugin) { |
||
142 | /** |
||
143 | * @param array $plugin |
||
144 | * @param string $feature |
||
145 | */ |
||
146 | protected static function check_plugin_feature_availability (&$plugin, $feature) { |
||
158 | /** |
||
159 | * Disable plugin |
||
160 | * |
||
161 | * Provides next events: |
||
162 | * admin/System/components/plugins/enable/before |
||
163 | * ['name' => plugin_name] |
||
164 | * |
||
165 | * admin/System/components/plugins/enable/after |
||
166 | * ['name' => plugin_name] |
||
167 | * |
||
168 | * @param \cs\Request $Request |
||
169 | * |
||
170 | * @throws ExitException |
||
171 | */ |
||
172 | static function admin_plugins_enable ($Request) { |
||
209 | /** |
||
210 | * Disable plugin |
||
211 | * |
||
212 | * Provides next events: |
||
213 | * admin/System/components/plugins/disable/before |
||
214 | * ['name' => plugin_name] |
||
215 | * |
||
216 | * admin/System/components/plugins/disable/after |
||
217 | * ['name' => plugin_name] |
||
218 | * |
||
219 | * @param \cs\Request $Request |
||
220 | * |
||
221 | * @throws ExitException |
||
222 | */ |
||
223 | static function admin_plugins_disable ($Request) { |
||
251 | /** |
||
252 | * Extract uploaded plugin |
||
253 | * |
||
254 | * @throws ExitException |
||
255 | */ |
||
256 | static function admin_plugins_extract () { |
||
275 | /** |
||
276 | * Update plugin |
||
277 | * |
||
278 | * Provides next events: |
||
279 | * admin/System/components/plugins/update/before |
||
280 | * ['name' => plugin_name] |
||
281 | * |
||
282 | * admin/System/components/plugins/update/after |
||
283 | * ['name' => plugin_name] |
||
284 | * |
||
285 | * @param \cs\Request $Request |
||
286 | * |
||
287 | * @throws ExitException |
||
288 | */ |
||
289 | static function admin_plugins_update ($Request) { |
||
348 | /** |
||
349 | * Delete plugin completely |
||
350 | * |
||
351 | * @param \cs\Request $Request |
||
352 | * |
||
353 | * @throws ExitException |
||
354 | */ |
||
355 | static function admin_plugins_delete ($Request) { |
||
369 | } |
||
370 |