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 ) { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Handler for the upgrader success finishes. |
||
| 106 | * |
||
| 107 | * @access public |
||
| 108 | * |
||
| 109 | * @param \WP_Upgrader $upgrader Upgrader instance. |
||
| 110 | * @param array $details Array of bulk item update data. |
||
| 111 | */ |
||
| 112 | public function on_upgrader_completion( $upgrader, $details ) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Retrieve the plugin information by a plugin slug. |
||
| 195 | * |
||
| 196 | * @access private |
||
| 197 | * |
||
| 198 | * @param string $slug Plugin slug. |
||
| 199 | * @return array Plugin information. |
||
| 200 | */ |
||
| 201 | private function get_plugin_info( $slug ) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Retrieve upgrade errors. |
||
| 212 | * |
||
| 213 | * @access private |
||
| 214 | * |
||
| 215 | * @param \Automatic_Upgrader_Skin|\WP_Upgrader_Skin $skin The upgrader skin being used. |
||
| 216 | * @return array|boolean Error on error, false otherwise. |
||
| 217 | */ |
||
| 218 | private function get_errors( $skin ) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Handle plugin edit in the administration. |
||
| 251 | * |
||
| 252 | * @access public |
||
| 253 | * |
||
| 254 | * @todo The `admin_action_update` hook is called only for logged in users, but maybe implement nonce verification? |
||
| 255 | */ |
||
| 256 | public function check_plugin_edit() { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Handle plugin ajax edit in the administration. |
||
| 283 | * |
||
| 284 | * @access public |
||
| 285 | * |
||
| 286 | * @todo Update this method to use WP_Filesystem instead of fopen/fclose. |
||
| 287 | */ |
||
| 288 | public function plugin_edit_ajax() { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Handle plugin deletion. |
||
| 350 | * |
||
| 351 | * @access public |
||
| 352 | * |
||
| 353 | * @param string $plugin_path Path to the plugin main file. |
||
| 354 | */ |
||
| 355 | public function delete_plugin( $plugin_path ) { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Invoked after plugin deletion. |
||
| 377 | * |
||
| 378 | * @access public |
||
| 379 | * |
||
| 380 | * @param string $plugin_path Path to the plugin main file. |
||
| 381 | * @param boolean $is_deleted Whether the plugin was deleted successfully. |
||
| 382 | */ |
||
| 383 | public function deleted_plugin( $plugin_path, $is_deleted ) { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Expand the plugins within a hook before they are serialized and sent to the server. |
||
| 390 | * |
||
| 391 | * @access public |
||
| 392 | * |
||
| 393 | * @param array $args The hook parameters. |
||
| 394 | * @return array $args The expanded hook parameters. |
||
| 395 | */ |
||
| 396 | public function expand_plugin_data( $args ) { |
||
| 416 | } |
||
| 417 |