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_Payments_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_Payments_Query, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Give_Payments_Query extends Give_Stats { |
||
27 | |||
28 | /** |
||
29 | * Preserve args |
||
30 | * |
||
31 | * @since 1.8.17 |
||
32 | * @access public |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | public $_args = array(); |
||
37 | |||
38 | /** |
||
39 | * The args to pass to the give_get_payments() query |
||
40 | * |
||
41 | * @since 1.0 |
||
42 | * @access public |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | public $args = array(); |
||
47 | |||
48 | /** |
||
49 | * The payments found based on the criteria set |
||
50 | * |
||
51 | * @since 1.0 |
||
52 | * @access public |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | public $payments = array(); |
||
57 | 52 | ||
58 | /** |
||
59 | 52 | * Default query arguments. |
|
60 | 52 | * |
|
61 | 52 | * Not all of these are valid arguments that can be passed to WP_Query. The ones that are not, are modified before |
|
62 | 52 | * the query is run to convert them to the proper syntax. |
|
63 | 52 | * |
|
64 | 52 | * @since 1.0 |
|
65 | 52 | * @access public |
|
66 | 52 | * |
|
67 | 52 | * @param $args array The array of arguments that can be passed in and used for setting up this payment query. |
|
68 | 52 | */ |
|
69 | 52 | public function __construct( $args = array() ) { |
|
103 | |||
104 | /** |
||
105 | 52 | * Set a query variable. |
|
106 | 52 | * |
|
107 | 52 | * @since 1.0 |
|
108 | * @access public |
||
109 | * |
||
110 | * @param $query_var |
||
111 | * @param $value |
||
112 | */ |
||
113 | public function __set( $query_var, $value ) { |
||
120 | |||
121 | 52 | /** |
|
122 | 52 | * Unset a query variable. |
|
123 | 52 | * |
|
124 | 52 | * @since 1.0 |
|
125 | 52 | * @access public |
|
126 | 52 | * |
|
127 | 52 | * @param $query_var |
|
128 | 52 | */ |
|
129 | 52 | public function __unset( $query_var ) { |
|
132 | |||
133 | /** |
||
134 | * Modify the query/query arguments before we retrieve payments. |
||
135 | * |
||
136 | * @since 1.0 |
||
137 | * @access public |
||
138 | * |
||
139 | * @return void |
||
140 | */ |
||
141 | public function init() { |
||
143 | |||
144 | 52 | ||
145 | /** |
||
146 | 52 | * Set query filter. |
|
147 | * |
||
148 | 52 | * @since 1.8.9 |
|
149 | * @access private |
||
150 | */ |
||
151 | 52 | private function set_filters() { |
|
181 | |||
182 | /** |
||
183 | * Unset query filter. |
||
184 | 52 | * |
|
185 | 52 | * @since 1.8.9 |
|
186 | 52 | * @access private |
|
187 | */ |
||
188 | private function unset_filters() { |
||
200 | |||
201 | |||
202 | /** |
||
203 | * Retrieve payments. |
||
204 | * |
||
205 | * The query can be modified in two ways; either the action before the |
||
206 | * query is run, or the filter on the arguments (existing mainly for backwards |
||
207 | * compatibility). |
||
208 | * |
||
209 | * @since 1.0 |
||
210 | * @access public |
||
211 | * |
||
212 | * @return array |
||
213 | */ |
||
214 | public function get_payments() { |
||
248 | |||
249 | 52 | /** |
|
250 | * Get payments by group |
||
251 | 52 | * |
|
252 | 42 | * @since 1.8.17 |
|
253 | * @access public |
||
254 | * |
||
255 | 52 | * @return array |
|
256 | 42 | */ |
|
257 | 42 | public function get_payment_by_group() { |
|
308 | 52 | ||
309 | /** |
||
310 | * If querying a specific date, add the proper filters. |
||
311 | * |
||
312 | * @since 1.0 |
||
313 | * @access public |
||
314 | * |
||
315 | * @return void |
||
316 | */ |
||
317 | public function date_filter_pre() { |
||
342 | 32 | ||
343 | 32 | /** |
|
344 | * Post Status |
||
345 | 32 | * |
|
346 | * @since 1.0 |
||
347 | * @access public |
||
348 | * |
||
349 | * @return void |
||
350 | */ |
||
351 | public function status() { |
||
359 | |||
360 | 32 | /** |
|
361 | * Current Page |
||
362 | 32 | * |
|
363 | 32 | * @since 1.0 |
|
364 | * @access public |
||
365 | 32 | * |
|
366 | * @return void |
||
367 | 32 | */ |
|
368 | 32 | public function page() { |
|
376 | |||
377 | /** |
||
378 | * Posts Per Page |
||
379 | * |
||
380 | * @since 1.0 |
||
381 | * @access public |
||
382 | * |
||
383 | * @return void |
||
384 | */ |
||
385 | public function per_page() { |
||
399 | |||
400 | /** |
||
401 | * Current Month |
||
402 | * |
||
403 | * @since 1.0 |
||
404 | * @access public |
||
405 | * |
||
406 | * @return void |
||
407 | */ |
||
408 | public function month() { |
||
416 | |||
417 | /** |
||
418 | * Order by |
||
419 | * |
||
420 | * @since 1.0 |
||
421 | * @access public |
||
422 | * |
||
423 | * @return void |
||
424 | */ |
||
425 | public function orderby() { |
||
446 | 52 | ||
447 | 52 | /** |
|
448 | * Custom orderby. |
||
449 | 52 | * Note: currently custom sorting is only used for donation listing page. |
|
450 | * |
||
451 | * @since 1.8 |
||
452 | * @access public |
||
453 | * |
||
454 | * @param string $order |
||
455 | * @param WP_Query $query |
||
456 | * |
||
457 | * @return mixed |
||
458 | */ |
||
459 | public function custom_orderby( $order, $query ) { |
||
478 | |||
479 | 52 | /** |
|
480 | * Specific User |
||
481 | 52 | * |
|
482 | 52 | * @since 1.0 |
|
483 | * @access public |
||
484 | * |
||
485 | * @return void |
||
486 | */ |
||
487 | public function user() { |
||
505 | |||
506 | /** |
||
507 | * Specific donor id |
||
508 | * |
||
509 | * @access public |
||
510 | * @since 1.8.9 |
||
511 | * @return void |
||
512 | */ |
||
513 | public function donor() { |
||
525 | |||
526 | /** |
||
527 | * Search |
||
528 | * |
||
529 | * @since 1.0 |
||
530 | * @access public |
||
531 | * |
||
532 | * @return void |
||
533 | */ |
||
534 | public function search() { |
||
640 | |||
641 | /** |
||
642 | * Payment Mode |
||
643 | * |
||
644 | * @since 1.0 |
||
645 | * @access public |
||
646 | * |
||
647 | * @return void |
||
648 | */ |
||
649 | public function mode() { |
||
663 | |||
664 | /** |
||
665 | * Children |
||
666 | * |
||
667 | * @since 1.0 |
||
668 | * @access public |
||
669 | * |
||
670 | * @return void |
||
671 | */ |
||
672 | public function children() { |
||
678 | |||
679 | /** |
||
680 | * Specific Give Form |
||
681 | * |
||
682 | * @since 1.0 |
||
683 | * @access public |
||
684 | * |
||
685 | * @return void |
||
686 | */ |
||
687 | View Code Duplication | public function give_forms() { |
|
712 | |||
713 | /** |
||
714 | * Specific Gateway |
||
715 | * |
||
716 | * @since 1.8.17 |
||
717 | * @access public |
||
718 | * |
||
719 | * @return void |
||
720 | */ |
||
721 | View Code Duplication | public function gateway_filter() { |
|
746 | |||
747 | |||
748 | /** |
||
749 | * Get sql query |
||
750 | * |
||
751 | * Note: Internal purpose only. We are developing on this fn. |
||
752 | * |
||
753 | * @since 1.8.18 |
||
754 | * @access public |
||
755 | * @global $wpdb |
||
756 | * |
||
757 | * @return string |
||
758 | */ |
||
759 | private function get_sql() { |
||
843 | |||
844 | } |
||
845 |