Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like WP_Background_Process 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 WP_Background_Process, and based on these observations, apply Extract Interface, too.
1 | <?php // @codingStandardsIgnoreLine. |
||
14 | abstract class WP_Background_Process extends WP_Async_Request { |
||
15 | |||
16 | /** |
||
17 | * Action |
||
18 | * |
||
19 | * (default value: 'background_process') |
||
20 | * |
||
21 | * @var string |
||
22 | * @access protected |
||
23 | */ |
||
24 | protected $action = 'background_process'; |
||
25 | |||
26 | /** |
||
27 | * Start time of current process. |
||
28 | * |
||
29 | * (default value: 0) |
||
30 | * |
||
31 | * @var int |
||
32 | * @access protected |
||
33 | */ |
||
34 | protected $start_time = 0; |
||
35 | |||
36 | /** |
||
37 | * Cron_hook_identifier |
||
38 | * |
||
39 | * @var mixed |
||
40 | * @access protected |
||
41 | */ |
||
42 | protected $cron_hook_identifier; |
||
43 | |||
44 | /** |
||
45 | * Cron_interval_identifier |
||
46 | * |
||
47 | * @var mixed |
||
48 | * @access protected |
||
49 | */ |
||
50 | protected $cron_interval_identifier; |
||
51 | |||
52 | /** |
||
53 | * Initiate new background process |
||
54 | */ |
||
55 | public function __construct() { |
||
64 | |||
65 | /** |
||
66 | * Dispatch |
||
67 | * |
||
68 | * @access public |
||
69 | * @return void |
||
70 | */ |
||
71 | public function dispatch() { |
||
78 | |||
79 | /** |
||
80 | * Push to queue |
||
81 | * |
||
82 | * @param mixed $data Data. |
||
83 | * |
||
84 | * @return $this |
||
85 | */ |
||
86 | public function push_to_queue( $data ) { |
||
91 | |||
92 | /** |
||
93 | * Save queue |
||
94 | * |
||
95 | * @return $this |
||
96 | */ |
||
97 | public function save() { |
||
106 | |||
107 | /** |
||
108 | * Update queue |
||
109 | * |
||
110 | * @param string $key Key. |
||
111 | * @param array $data Data. |
||
112 | * |
||
113 | * @return $this |
||
114 | */ |
||
115 | public function update( $key, $data ) { |
||
122 | |||
123 | /** |
||
124 | * Delete queue |
||
125 | * |
||
126 | * @param string $key Key. |
||
127 | * |
||
128 | * @return $this |
||
129 | */ |
||
130 | public function delete( $key ) { |
||
135 | |||
136 | /** |
||
137 | * Generate key |
||
138 | * |
||
139 | * Generates a unique key based on microtime. Queue items are |
||
140 | * given a unique key so that they can be merged upon save. |
||
141 | * |
||
142 | * @param int $length Length. |
||
143 | * |
||
144 | * @return string |
||
145 | */ |
||
146 | protected function generate_key( $length = 64 ) { |
||
152 | |||
153 | /** |
||
154 | * Maybe process queue |
||
155 | * |
||
156 | * Checks whether data exists within the queue and that |
||
157 | * the process is not already running. |
||
158 | */ |
||
159 | public function maybe_handle() { |
||
179 | |||
180 | /** |
||
181 | * Is queue empty |
||
182 | * |
||
183 | * @return bool |
||
184 | */ |
||
185 | View Code Duplication | protected function is_queue_empty() { |
|
206 | |||
207 | /** |
||
208 | * Is process running |
||
209 | * |
||
210 | * Check whether the current process is already running |
||
211 | * in a background process. |
||
212 | */ |
||
213 | protected function is_process_running() { |
||
221 | |||
222 | /** |
||
223 | * Lock process |
||
224 | * |
||
225 | * Lock the process so that multiple instances can't run simultaneously. |
||
226 | * Override if applicable, but the duration should be greater than that |
||
227 | * defined in the time_exceeded() method. |
||
228 | */ |
||
229 | protected function lock_process() { |
||
237 | |||
238 | /** |
||
239 | * Unlock process |
||
240 | * |
||
241 | * Unlock the process so that other instances can spawn. |
||
242 | * |
||
243 | * @return $this |
||
244 | */ |
||
245 | protected function unlock_process() { |
||
250 | |||
251 | /** |
||
252 | * Get batch |
||
253 | * |
||
254 | * @return stdClass Return the first batch from the queue |
||
255 | */ |
||
256 | View Code Duplication | protected function get_batch() { |
|
287 | |||
288 | /** |
||
289 | * Handle |
||
290 | * |
||
291 | * Pass each queue item to the task handler, while remaining |
||
292 | * within server memory and time limit constraints. |
||
293 | */ |
||
294 | protected function handle() { |
||
334 | |||
335 | /** |
||
336 | * Memory exceeded |
||
337 | * |
||
338 | * Ensures the batch process never exceeds 90% |
||
339 | * of the maximum WordPress memory. |
||
340 | * |
||
341 | * @return bool |
||
342 | */ |
||
343 | protected function memory_exceeded() { |
||
354 | |||
355 | /** |
||
356 | * Get memory limit |
||
357 | * |
||
358 | * @return int |
||
359 | */ |
||
360 | View Code Duplication | protected function get_memory_limit() { |
|
375 | |||
376 | /** |
||
377 | * Time exceeded. |
||
378 | * |
||
379 | * Ensures the batch never exceeds a sensible time limit. |
||
380 | * A timeout limit of 30s is common on shared hosting. |
||
381 | * |
||
382 | * @return bool |
||
383 | */ |
||
384 | protected function time_exceeded() { |
||
394 | |||
395 | /** |
||
396 | * Complete. |
||
397 | * |
||
398 | * Override if applicable, but ensure that the below actions are |
||
399 | * performed, or, call parent::complete(). |
||
400 | */ |
||
401 | protected function complete() { |
||
405 | |||
406 | /** |
||
407 | * Schedule cron healthcheck |
||
408 | * |
||
409 | * @access public |
||
410 | * @param mixed $schedules Schedules. |
||
411 | * @return mixed |
||
412 | */ |
||
413 | View Code Duplication | public function schedule_cron_healthcheck( $schedules ) { |
|
428 | |||
429 | /** |
||
430 | * Handle cron healthcheck |
||
431 | * |
||
432 | * Restart the background process if not already running |
||
433 | * and data exists in the queue. |
||
434 | */ |
||
435 | public function handle_cron_healthcheck() { |
||
451 | |||
452 | /** |
||
453 | * Schedule event |
||
454 | */ |
||
455 | protected function schedule_event() { |
||
460 | |||
461 | /** |
||
462 | * Clear scheduled event |
||
463 | */ |
||
464 | protected function clear_scheduled_event() { |
||
471 | |||
472 | /** |
||
473 | * Cancel Process |
||
474 | * |
||
475 | * Stop processing queue items, clear cronjob and delete batch. |
||
476 | * |
||
477 | */ |
||
478 | public function cancel_process() { |
||
488 | |||
489 | /** |
||
490 | * Task |
||
491 | * |
||
492 | * Override this method to perform any actions required on each |
||
493 | * queue item. Return the modified item for further processing |
||
494 | * in the next pass through. Or, return false to remove the |
||
495 | * item from the queue. |
||
496 | * |
||
497 | * @param mixed $item Queue item to iterate over. |
||
498 | * |
||
499 | * @return mixed |
||
500 | */ |
||
501 | abstract protected function task( $item ); |
||
502 | |||
503 | } |
||
504 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.