Complex classes like Give_Background_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 Give_Background_Updater, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Give_Background_Updater extends WP_Background_Process { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $action = 'give_db_updater'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Dispatch updater. |
||
| 30 | * |
||
| 31 | * Updater will still run via cron job if this fails for any reason. |
||
| 32 | */ |
||
| 33 | public function dispatch() { |
||
| 40 | |||
| 41 | |||
| 42 | /** |
||
| 43 | * Get all batches. |
||
| 44 | * |
||
| 45 | * @since 2.0 |
||
| 46 | * @access public |
||
| 47 | * @return stdClass |
||
| 48 | */ |
||
| 49 | public function get_all_batch() { |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Is queue empty |
||
| 55 | * |
||
| 56 | * @since 2.0.3 |
||
| 57 | * |
||
| 58 | * @return bool |
||
| 59 | */ |
||
| 60 | public function has_queue() { |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * Lock process |
||
| 67 | * |
||
| 68 | * Lock the process so that multiple instances can't run simultaneously. |
||
| 69 | * Override if applicable, but the duration should be greater than that |
||
| 70 | * defined in the time_exceeded() method. |
||
| 71 | * |
||
| 72 | * |
||
| 73 | * @since 2.0.3 |
||
| 74 | */ |
||
| 75 | protected function lock_process() { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Handle cron healthcheck |
||
| 107 | * |
||
| 108 | * Restart the background process if not already running |
||
| 109 | * and data exists in the queue. |
||
| 110 | */ |
||
| 111 | public function handle_cron_healthcheck() { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Schedule fallback event. |
||
| 129 | */ |
||
| 130 | protected function schedule_event() { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Is queue empty |
||
| 138 | * |
||
| 139 | * @since 2.4.5 |
||
| 140 | * |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | protected function is_queue_empty() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get batch |
||
| 162 | * |
||
| 163 | * @since 2.4.5 |
||
| 164 | * |
||
| 165 | * @return stdClass Return the first batch from the queue |
||
| 166 | */ |
||
| 167 | protected function get_batch() { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Save queue |
||
| 194 | * |
||
| 195 | * @since 2.4.5 |
||
| 196 | * |
||
| 197 | * @return $this |
||
| 198 | */ |
||
| 199 | public function save() { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Update queue |
||
| 211 | * |
||
| 212 | * @since 2.4.5 |
||
| 213 | * |
||
| 214 | * @param string $key Key. |
||
| 215 | * @param array $data Data. |
||
| 216 | * |
||
| 217 | * @return $this |
||
| 218 | */ |
||
| 219 | public function update( $key, $data ) { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Delete queue |
||
| 229 | * |
||
| 230 | * @since 2.4.5 |
||
| 231 | * |
||
| 232 | * @param string $key Key. |
||
| 233 | * |
||
| 234 | * @return $this |
||
| 235 | */ |
||
| 236 | public function delete( $key ) { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Is process running |
||
| 244 | * |
||
| 245 | * @since 2.4.5 |
||
| 246 | * |
||
| 247 | * Check whether the current process is already running |
||
| 248 | * in a background process. |
||
| 249 | */ |
||
| 250 | public function is_process_running() { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Unlock process |
||
| 261 | * |
||
| 262 | * Unlock the process so that other instances can spawn. |
||
| 263 | * |
||
| 264 | * @since 2.4.5 |
||
| 265 | * |
||
| 266 | * @return $this |
||
| 267 | */ |
||
| 268 | protected function unlock_process() { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Task |
||
| 276 | * |
||
| 277 | * Override this method to perform any actions required on each |
||
| 278 | * queue item. Return the modified item for further processing |
||
| 279 | * in the next pass through. Or, return false to remove the |
||
| 280 | * item from the queue. |
||
| 281 | * |
||
| 282 | * @param array $update Update info |
||
| 283 | * |
||
| 284 | * @return mixed |
||
| 285 | */ |
||
| 286 | protected function task( $update ) { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Complete |
||
| 421 | * |
||
| 422 | * Override if applicable, but ensure that the below actions are |
||
| 423 | * performed, or, call parent::complete(). |
||
| 424 | */ |
||
| 425 | public function complete() { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Get memory limit |
||
| 448 | * |
||
| 449 | * @return int |
||
| 450 | */ |
||
| 451 | protected function get_memory_limit() { |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Maybe process queue |
||
| 469 | * |
||
| 470 | * Checks whether data exists within the queue and that |
||
| 471 | * the process is not already running. |
||
| 472 | */ |
||
| 473 | public function maybe_handle() { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Handle |
||
| 496 | * |
||
| 497 | * Pass each queue item to the task handler, while remaining |
||
| 498 | * within server memory and time limit constraints. |
||
| 499 | */ |
||
| 500 | protected function handle() { |
||
| 544 | |||
| 545 | |||
| 546 | /** |
||
| 547 | * Check if backgound upgrade paused or not. |
||
| 548 | * |
||
| 549 | * @since 2.0 |
||
| 550 | * @access public |
||
| 551 | * @return bool |
||
| 552 | */ |
||
| 553 | public function is_paused_process(){ |
||
| 561 | |||
| 562 | |||
| 563 | /** |
||
| 564 | * Get identifier |
||
| 565 | * |
||
| 566 | * @since 2.0 |
||
| 567 | * @access public |
||
| 568 | * @return mixed|string |
||
| 569 | */ |
||
| 570 | public function get_identifier() { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Get cron identifier |
||
| 576 | * |
||
| 577 | * @since 2.0 |
||
| 578 | * @access public |
||
| 579 | * @return mixed|string |
||
| 580 | */ |
||
| 581 | public function get_cron_identifier() { |
||
| 584 | |||
| 585 | |||
| 586 | /** |
||
| 587 | * Flush background update related cache to prevent task to go to stalled state. |
||
| 588 | * |
||
| 589 | * @since 2.0.3 |
||
| 590 | */ |
||
| 591 | public static function flush_cache() { |
||
| 608 | } |
||
| 609 |