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 Give_Tools_Recount_All_Stats 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_Tools_Recount_All_Stats, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Give_Tools_Recount_All_Stats extends Give_Batch_Export { |
||
24 | |||
25 | /** |
||
26 | * Our export type. Used for export-type specific filters/actions |
||
27 | * |
||
28 | * @since 1.5 |
||
29 | * @var string |
||
30 | */ |
||
31 | public $export_type = ''; |
||
32 | |||
33 | /** |
||
34 | * Allows for a non-form batch processing to be run. |
||
35 | * |
||
36 | * @since 1.5 |
||
37 | * @var bool |
||
38 | */ |
||
39 | public $is_void = true; |
||
40 | |||
41 | /** |
||
42 | * Sets the number of items to pull on each step |
||
43 | * |
||
44 | * @since 1.5 |
||
45 | * @var int |
||
46 | */ |
||
47 | public $per_step = 30; |
||
48 | |||
49 | /** |
||
50 | * Display message on completing recount process |
||
51 | * |
||
52 | * @since 1.8.9 |
||
53 | * @var string |
||
54 | */ |
||
55 | public $message = ''; |
||
56 | |||
57 | /** |
||
58 | * Sets donation form id for recalculation |
||
59 | * |
||
60 | * @since 1.8.9 |
||
61 | * @var int |
||
62 | */ |
||
63 | protected $form_id = 0; |
||
64 | |||
65 | /** |
||
66 | * Is Recount process completed |
||
67 | * |
||
68 | * @since 1.8.9 |
||
69 | * @var bool |
||
70 | */ |
||
71 | public $done = false; |
||
72 | |||
73 | /** |
||
74 | * Constructor. |
||
75 | */ |
||
76 | public function __construct( $_step = 1 ) { |
||
81 | |||
82 | /** |
||
83 | * Get the recount all stats data |
||
84 | * |
||
85 | * @access public |
||
86 | * @since 1.5 |
||
87 | * |
||
88 | * @return bool |
||
89 | */ |
||
90 | public function get_data() { |
||
199 | |||
200 | /** |
||
201 | * Return the calculated completion percentage |
||
202 | * |
||
203 | * @since 1.5 |
||
204 | * @return int |
||
205 | */ |
||
206 | public function get_percentage_complete() { |
||
227 | |||
228 | /** |
||
229 | * Set the properties specific to the payments export |
||
230 | * |
||
231 | * @since 1.5 |
||
232 | * |
||
233 | * @param array $request The Form Data passed into the batch processing |
||
234 | */ |
||
235 | public function set_properties( $request ) { |
||
238 | |||
239 | /** |
||
240 | * Process a step |
||
241 | * |
||
242 | * @since 1.5 |
||
243 | * @return bool |
||
244 | */ |
||
245 | public function process_step() { |
||
270 | |||
271 | /** |
||
272 | * Set headers. |
||
273 | */ |
||
274 | public function headers() { |
||
277 | |||
278 | /** |
||
279 | * Perform the export |
||
280 | * |
||
281 | * @access public |
||
282 | * @since 1.5 |
||
283 | * @return void |
||
284 | */ |
||
285 | public function export() { |
||
292 | |||
293 | /** |
||
294 | * Pre Fetch Data |
||
295 | * |
||
296 | * @access public |
||
297 | * @since 1.5 |
||
298 | */ |
||
299 | public function pre_fetch() { |
||
377 | |||
378 | /** |
||
379 | * Given a key, get the information from the Database Directly |
||
380 | * |
||
381 | * @since 1.5 |
||
382 | * |
||
383 | * @param string $key The option_name |
||
384 | * |
||
385 | * @return mixed Returns the data from the database |
||
386 | */ |
||
387 | View Code Duplication | private function get_stored_data( $key ) { |
|
402 | |||
403 | /** |
||
404 | * Give a key, store the value |
||
405 | * |
||
406 | * @since 1.5 |
||
407 | * |
||
408 | * @param string $key The option_name |
||
409 | * @param mixed $value The value to store |
||
410 | * |
||
411 | * @return void |
||
412 | */ |
||
413 | View Code Duplication | private function store_data( $key, $value ) { |
|
432 | |||
433 | /** |
||
434 | * Delete an option |
||
435 | * |
||
436 | * @since 1.5 |
||
437 | * |
||
438 | * @param string $key The option_name to delete |
||
439 | * |
||
440 | * @return void |
||
441 | */ |
||
442 | private function delete_data( $key ) { |
||
446 | |||
447 | } |
||
448 |