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 |
||
15 | class Plugins extends Module { |
||
16 | /** |
||
17 | * Action handler callable. |
||
18 | * |
||
19 | * @access private |
||
20 | * |
||
21 | * @var callable |
||
22 | */ |
||
23 | private $action_handler; |
||
24 | |||
25 | /** |
||
26 | * Information about plugins we store temporarily. |
||
27 | * |
||
28 | * @access private |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | private $plugin_info = array(); |
||
33 | |||
34 | /** |
||
35 | * List of all plugins in the installation. |
||
36 | * |
||
37 | * @access private |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | private $plugins = array(); |
||
42 | |||
43 | /** |
||
44 | * Sync module name. |
||
45 | * |
||
46 | * @access public |
||
47 | * |
||
48 | * @return string |
||
49 | */ |
||
50 | public function name() { |
||
53 | |||
54 | /** |
||
55 | * Initialize plugins action listeners. |
||
56 | * |
||
57 | * @access public |
||
58 | * |
||
59 | * @param callable $callable Action handler callable. |
||
60 | */ |
||
61 | public function init_listeners( $callable ) { |
||
77 | |||
78 | /** |
||
79 | * Initialize the module in the sender. |
||
80 | * |
||
81 | * @access public |
||
82 | */ |
||
83 | public function init_before_send() { |
||
88 | |||
89 | /** |
||
90 | * Fetch and populate all current plugins before upgrader installation. |
||
91 | * |
||
92 | * @access public |
||
93 | * |
||
94 | * @param bool|WP_Error $response Install response, true if successful, WP_Error if not. |
||
95 | */ |
||
96 | public function populate_plugins( $response ) { |
||
100 | |||
101 | /** |
||
102 | * Handler for the upgrader success finishes. |
||
103 | * |
||
104 | * @access public |
||
105 | * |
||
106 | * @param \WP_Upgrader $upgrader Upgrader instance. |
||
107 | * @param array $details Array of bulk item update data. |
||
108 | */ |
||
109 | public function on_upgrader_completion( $upgrader, $details ) { |
||
189 | |||
190 | /** |
||
191 | * Retrieve the plugin information by a plugin slug. |
||
192 | * |
||
193 | * @access private |
||
194 | * |
||
195 | * @param string $slug Plugin slug. |
||
196 | * @return array Plugin information. |
||
197 | */ |
||
198 | private function get_plugin_info( $slug ) { |
||
206 | |||
207 | /** |
||
208 | * Retrieve upgrade errors. |
||
209 | * |
||
210 | * @access private |
||
211 | * |
||
212 | * @param \Automatic_Upgrader_Skin|\WP_Upgrader_Skin $skin The upgrader skin being used. |
||
213 | * @return array|boolean Error on error, false otherwise. |
||
214 | */ |
||
215 | private function get_errors( $skin ) { |
||
245 | |||
246 | /** |
||
247 | * Handle plugin edit in the administration. |
||
248 | * |
||
249 | * @access public |
||
250 | * |
||
251 | * @todo The `admin_action_update` hook is called only for logged in users, but maybe implement nonce verification? |
||
252 | */ |
||
253 | public function check_plugin_edit() { |
||
277 | |||
278 | /** |
||
279 | * Handle plugin ajax edit in the administration. |
||
280 | * |
||
281 | * @access public |
||
282 | * |
||
283 | * @todo Update this method to use WP_Filesystem instead of fopen/fclose. |
||
284 | */ |
||
285 | public function plugin_edit_ajax() { |
||
344 | |||
345 | /** |
||
346 | * Handle plugin deletion. |
||
347 | * |
||
348 | * @access public |
||
349 | * |
||
350 | * @param string $plugin_path Path to the plugin main file. |
||
351 | */ |
||
352 | public function delete_plugin( $plugin_path ) { |
||
371 | |||
372 | /** |
||
373 | * Invoked after plugin deletion. |
||
374 | * |
||
375 | * @access public |
||
376 | * |
||
377 | * @param string $plugin_path Path to the plugin main file. |
||
378 | * @param boolean $is_deleted Whether the plugin was deleted successfully. |
||
379 | */ |
||
380 | public function deleted_plugin( $plugin_path, $is_deleted ) { |
||
384 | |||
385 | /** |
||
386 | * Expand the plugins within a hook before they are serialized and sent to the server. |
||
387 | * |
||
388 | * @access public |
||
389 | * |
||
390 | * @param array $args The hook parameters. |
||
391 | * @return array $args The expanded hook parameters. |
||
392 | */ |
||
393 | public function expand_plugin_data( $args ) { |
||
413 | } |
||
414 |