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_Donors_Query 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_Donors_Query, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Give_Donors_Query { |
||
27 | |||
28 | /** |
||
29 | * The args to pass to the give_get_donors() query |
||
30 | * |
||
31 | * @since 1.8.14 |
||
32 | * @access public |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | public $args = array(); |
||
37 | |||
38 | /** |
||
39 | * The donors found based on the criteria set |
||
40 | * |
||
41 | * @since 1.8.14 |
||
42 | * @access public |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | public $donors = array(); |
||
47 | |||
48 | /** |
||
49 | * The donors found based on the criteria set |
||
50 | * |
||
51 | * @since 1.8.14 |
||
52 | * @access public |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | public $table_name = ''; |
||
57 | |||
58 | /** |
||
59 | * The donors found based on the criteria set |
||
60 | * |
||
61 | * @since 1.8.14 |
||
62 | * @access public |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | public $meta_table_name = ''; |
||
67 | |||
68 | /** |
||
69 | * The donors found based on the criteria set |
||
70 | * |
||
71 | * @since 1.8.14 |
||
72 | * @access public |
||
73 | * |
||
74 | * @var string |
||
75 | */ |
||
76 | public $meta_type = ''; |
||
77 | |||
78 | /** |
||
79 | * Preserve args |
||
80 | * |
||
81 | * @since 2.4.0 |
||
82 | * @access public |
||
83 | * |
||
84 | * @var array |
||
85 | */ |
||
86 | public $_args = array(); |
||
87 | |||
88 | /** |
||
89 | * Default query arguments. |
||
90 | * |
||
91 | * Not all of these are valid arguments that can be passed to WP_Query. The ones that are not, are modified before |
||
92 | * the query is run to convert them to the proper syntax. |
||
93 | * |
||
94 | * @since 1.8.14 |
||
95 | * @access public |
||
96 | * |
||
97 | * @param $args array The array of arguments that can be passed in and used for setting up this payment query. |
||
98 | */ |
||
99 | public function __construct( $args = array() ) { |
||
137 | |||
138 | /** |
||
139 | * Modify the query/query arguments before we retrieve donors. |
||
140 | * |
||
141 | * @since 1.8.14 |
||
142 | * @access public |
||
143 | * |
||
144 | * @return void |
||
145 | */ |
||
146 | public function init() { |
||
148 | |||
149 | |||
150 | /** |
||
151 | * Retrieve donors. |
||
152 | * |
||
153 | * The query can be modified in two ways; either the action before the |
||
154 | * query is run, or the filter on the arguments (existing mainly for backwards |
||
155 | * compatibility). |
||
156 | * |
||
157 | * @since 1.8.14 |
||
158 | * @access public |
||
159 | * |
||
160 | * @global wpdb $wpdb |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | public function get_donors() { |
||
204 | |||
205 | /** |
||
206 | * Get sql query from queried array. |
||
207 | * |
||
208 | * @since 2.0 |
||
209 | * @access public |
||
210 | * |
||
211 | * @global wpdb $wpdb |
||
212 | * @return string |
||
213 | */ |
||
214 | public function get_sql() { |
||
254 | |||
255 | /** |
||
256 | * Set query where clause. |
||
257 | * |
||
258 | * @since 1.8.14 |
||
259 | * @access private |
||
260 | * |
||
261 | * @global wpdb $wpdb |
||
262 | * @return string |
||
263 | */ |
||
264 | private function get_where_query() { |
||
289 | |||
290 | /** |
||
291 | * Set email where clause. |
||
292 | * |
||
293 | * @since 1.8.14 |
||
294 | * @access private |
||
295 | * |
||
296 | * @global wpdb $wpdb |
||
297 | * @return string |
||
298 | */ |
||
299 | private function get_where_email() { |
||
320 | |||
321 | /** |
||
322 | * Set donor where clause. |
||
323 | * |
||
324 | * @since 1.8.14 |
||
325 | * @access private |
||
326 | * |
||
327 | * @global wpdb $wpdb |
||
328 | * @return string |
||
329 | */ |
||
330 | View Code Duplication | private function get_where_donor() { |
|
345 | |||
346 | /** |
||
347 | * Set date where clause. |
||
348 | * |
||
349 | * @since 1.8.14 |
||
350 | * @access private |
||
351 | * |
||
352 | * @global wpdb $wpdb |
||
353 | * @return string |
||
354 | */ |
||
355 | private function get_where_date() { |
||
375 | |||
376 | /** |
||
377 | * Set search where clause. |
||
378 | * |
||
379 | * @since 1.8.14 |
||
380 | * @access private |
||
381 | * |
||
382 | * @global wpdb $wpdb |
||
383 | * @return string |
||
384 | */ |
||
385 | private function get_where_search() { |
||
418 | |||
419 | /** |
||
420 | * Set user where clause. |
||
421 | * |
||
422 | * @since 1.8.14 |
||
423 | * @access private |
||
424 | * |
||
425 | * @global wpdb $wpdb |
||
426 | * @return string |
||
427 | */ |
||
428 | View Code Duplication | private function get_where_user() { |
|
443 | |||
444 | /** |
||
445 | * Set orderby query |
||
446 | * |
||
447 | * @since 1.8.14 |
||
448 | * @access private |
||
449 | * |
||
450 | * @return string |
||
451 | */ |
||
452 | private function get_order_query() { |
||
495 | |||
496 | /** |
||
497 | * Set donation count value where clause. |
||
498 | * @todo: add phpunit test |
||
499 | * |
||
500 | * @since 2.2.0 |
||
501 | * @access private |
||
502 | * |
||
503 | * @global wpdb $wpdb |
||
504 | * @return string |
||
505 | */ |
||
506 | View Code Duplication | private function get_where_donation_count() { |
|
522 | |||
523 | /** |
||
524 | * Set purchase value where clause. |
||
525 | * @todo: add phpunit test |
||
526 | * |
||
527 | * @since 2.1.0 |
||
528 | * @access private |
||
529 | * |
||
530 | * @global wpdb $wpdb |
||
531 | * @return string |
||
532 | */ |
||
533 | View Code Duplication | private function get_where_donation_amount() { |
|
549 | |||
550 | /** |
||
551 | * Set give_forms where clause. |
||
552 | * |
||
553 | * @todo : add phpunit test |
||
554 | * |
||
555 | * @since 2.1.0 |
||
556 | * @access private |
||
557 | * |
||
558 | * @global wpdb $wpdb |
||
559 | * @return string |
||
560 | */ |
||
561 | private function get_where_give_forms() { |
||
603 | |||
604 | /** |
||
605 | * If querying a specific date, add the proper filters. |
||
606 | * Note: This function currently only accept dates with admin defined core date format |
||
607 | * |
||
608 | * @since 2.4.0 |
||
609 | * @access public |
||
610 | * |
||
611 | * @return void |
||
612 | */ |
||
613 | public function date_filter_pre() { |
||
647 | |||
648 | /** |
||
649 | * Update donors meta cache |
||
650 | * |
||
651 | * @since 2.5.0 |
||
652 | * @access private |
||
653 | * |
||
654 | * @param array $donor_ids |
||
655 | */ |
||
656 | public static function update_meta_cache( $donor_ids ){ |
||
664 | |||
665 | /** |
||
666 | * Set a query variable. |
||
667 | * |
||
668 | * @since 2.4.0 |
||
669 | * @access public |
||
670 | * |
||
671 | * @param $query_var |
||
672 | * @param $value |
||
673 | */ |
||
674 | View Code Duplication | public function __set( $query_var, $value ) { |
|
681 | |||
682 | } |
||
683 |