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() { |
||
37 | |||
38 | |||
39 | /** |
||
40 | * Get all batches. |
||
41 | * |
||
42 | * @since 2.0 |
||
43 | * @access public |
||
44 | * @return stdClass |
||
45 | */ |
||
46 | public function get_all_batch() { |
||
49 | |||
50 | /** |
||
51 | * Is queue empty |
||
52 | * |
||
53 | * @since 2.0.3 |
||
54 | * |
||
55 | * @return bool |
||
56 | */ |
||
57 | public function has_queue() { |
||
60 | |||
61 | |||
62 | /** |
||
63 | * Lock process |
||
64 | * |
||
65 | * Lock the process so that multiple instances can't run simultaneously. |
||
66 | * Override if applicable, but the duration should be greater than that |
||
67 | * defined in the time_exceeded() method. |
||
68 | * |
||
69 | * |
||
70 | * @since 2.0.3 |
||
71 | */ |
||
72 | protected function lock_process() { |
||
101 | |||
102 | /** |
||
103 | * Handle cron healthcheck |
||
104 | * |
||
105 | * Restart the background process if not already running |
||
106 | * and data exists in the queue. |
||
107 | */ |
||
108 | public function handle_cron_healthcheck() { |
||
123 | |||
124 | /** |
||
125 | * Schedule fallback event. |
||
126 | */ |
||
127 | protected function schedule_event() { |
||
132 | |||
133 | /** |
||
134 | * Task |
||
135 | * |
||
136 | * Override this method to perform any actions required on each |
||
137 | * queue item. Return the modified item for further processing |
||
138 | * in the next pass through. Or, return false to remove the |
||
139 | * item from the queue. |
||
140 | * |
||
141 | * @param array $update Update info |
||
142 | * |
||
143 | * @return mixed |
||
144 | */ |
||
145 | protected function task( $update ) { |
||
273 | |||
274 | /** |
||
275 | * Complete |
||
276 | * |
||
277 | * Override if applicable, but ensure that the below actions are |
||
278 | * performed, or, call parent::complete(). |
||
279 | */ |
||
280 | public function complete() { |
||
300 | |||
301 | /** |
||
302 | * Get memory limit |
||
303 | * |
||
304 | * @return int |
||
305 | */ |
||
306 | protected function get_memory_limit() { |
||
321 | |||
322 | /** |
||
323 | * Maybe process queue |
||
324 | * |
||
325 | * Checks whether data exists within the queue and that |
||
326 | * the process is not already running. |
||
327 | */ |
||
328 | public function maybe_handle() { |
||
348 | |||
349 | |||
350 | /** |
||
351 | * Check if backgound upgrade paused or not. |
||
352 | * |
||
353 | * @since 2.0 |
||
354 | * @access public |
||
355 | * @return bool |
||
356 | */ |
||
357 | public function is_paused_process(){ |
||
365 | |||
366 | |||
367 | /** |
||
368 | * Get identifier |
||
369 | * |
||
370 | * @since 2.0 |
||
371 | * @access public |
||
372 | * @return mixed|string |
||
373 | */ |
||
374 | public function get_identifier() { |
||
377 | |||
378 | /** |
||
379 | * Get cron identifier |
||
380 | * |
||
381 | * @since 2.0 |
||
382 | * @access public |
||
383 | * @return mixed|string |
||
384 | */ |
||
385 | public function get_cron_identifier() { |
||
388 | |||
389 | |||
390 | /** |
||
391 | * Flush background update related cache to prevent task to go to stalled state. |
||
392 | * |
||
393 | * @since 2.0.3 |
||
394 | */ |
||
395 | public static function flush_cache() { |
||
413 | } |
||
414 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.