Complex classes like Updater 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 Updater, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Updater { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * API Url. |
||
| 18 | * |
||
| 19 | * @access private |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private $api_url = ''; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * API data. |
||
| 26 | * |
||
| 27 | * @access private |
||
| 28 | * @var array|null |
||
| 29 | */ |
||
| 30 | private $api_data = array(); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Name. |
||
| 34 | * |
||
| 35 | * @access private |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | private $name = ''; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Slug. |
||
| 42 | * |
||
| 43 | * @access private |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | private $slug = ''; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Version. |
||
| 50 | * |
||
| 51 | * @access private |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private $version = ''; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Constructor. |
||
| 58 | * |
||
| 59 | * @since 3.0.0 |
||
| 60 | * |
||
| 61 | * @param string $_api_url The URL pointing to the custom API endpoint. |
||
| 62 | * @param string $_plugin_file Path to the plugin file. |
||
| 63 | * @param array $_api_data Optional data to send with API calls. |
||
| 64 | */ |
||
| 65 | public function __construct( $_api_url, $_plugin_file, $_api_data = null ) { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Hook into WordPress update process. |
||
| 80 | * |
||
| 81 | * @since 3.0.0 |
||
| 82 | * |
||
| 83 | * @return void |
||
| 84 | */ |
||
| 85 | public function init() { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Check for Updates at the defined API endpoint and modify the update array. |
||
| 94 | * |
||
| 95 | * This function dives into the update API just when WordPress creates its update array, |
||
| 96 | * then adds a custom API call and injects the custom plugin data retrieved from the API. |
||
| 97 | * It is reassembled from parts of the native WordPress plugin update code. |
||
| 98 | * See wp-includes/update.php line 121 for the original wp_update_plugins() function. |
||
| 99 | * |
||
| 100 | * @since 3.0.0 |
||
| 101 | * |
||
| 102 | * @param array $_transient_data Update array build by WordPress. |
||
| 103 | * |
||
| 104 | * @return array|\stdClass Modified update array with custom plugin data. |
||
| 105 | */ |
||
| 106 | public function check_update( $_transient_data ) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Show update notification row. |
||
| 139 | * |
||
| 140 | * Needed for multisite subsites, because WordPress won't tell otherwise. |
||
| 141 | * |
||
| 142 | * @since 3.0.0 |
||
| 143 | * |
||
| 144 | * @param string $file |
||
| 145 | * @param array $plugin |
||
| 146 | */ |
||
| 147 | public function show_update_notification( $file, $plugin ) { |
||
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * Updates information on the "View version x.x details" page with custom data. |
||
| 232 | * |
||
| 233 | * @since 3.0.0 |
||
| 234 | * |
||
| 235 | * @param mixed $_data |
||
| 236 | * @param string $_action |
||
| 237 | * @param object $_args |
||
| 238 | * |
||
| 239 | * @return object |
||
| 240 | */ |
||
| 241 | public function plugins_api_filter( $_data, $_action = '', $_args = null ) { |
||
| 268 | |||
| 269 | |||
| 270 | /** |
||
| 271 | * Disable SSL verification in order to prevent download update failures |
||
| 272 | * |
||
| 273 | * @since 3.0.0 |
||
| 274 | * |
||
| 275 | * @param array $args |
||
| 276 | * @param string $url |
||
| 277 | * @return object|array $array |
||
| 278 | */ |
||
| 279 | public function http_request_args( $args, $url ) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Calls the API and, if successful, returns the object delivered by the API. |
||
| 289 | * |
||
| 290 | * @since 3.0.0 |
||
| 291 | * |
||
| 292 | * @param string $_action The requested action. |
||
| 293 | * @param array $_data Parameters for the API action. |
||
| 294 | * @return false|object |
||
| 295 | */ |
||
| 296 | private function api_request( $_action, $_data ) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Show changelog. |
||
| 341 | * |
||
| 342 | * @since 3.0.0 |
||
| 343 | */ |
||
| 344 | public function show_changelog() { |
||
| 370 | |||
| 371 | } |
||
| 372 |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state