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 the query is run to convert them to the proper syntax. |
|
62 | 52 | * |
|
63 | 52 | * @since 1.0 |
|
64 | 52 | * @access public |
|
65 | 52 | * |
|
66 | 52 | * @param $args array The array of arguments that can be passed in and used for setting up this payment query. |
|
67 | 52 | */ |
|
68 | 52 | public function __construct( $args = array() ) { |
|
97 | 52 | ||
98 | /** |
||
99 | * Set a query variable. |
||
100 | * |
||
101 | * @since 1.0 |
||
102 | * @access public |
||
103 | * |
||
104 | * @param $query_var |
||
105 | 52 | * @param $value |
|
106 | 52 | */ |
|
107 | 52 | public function __set( $query_var, $value ) { |
|
114 | |||
115 | /** |
||
116 | 52 | * Unset a query variable. |
|
117 | * |
||
118 | 52 | * @since 1.0 |
|
119 | 52 | * @access public |
|
120 | * |
||
121 | 52 | * @param $query_var |
|
122 | 52 | */ |
|
123 | 52 | public function __unset( $query_var ) { |
|
126 | 52 | ||
127 | 52 | /** |
|
128 | 52 | * Modify the query/query arguments before we retrieve payments. |
|
129 | 52 | * |
|
130 | 52 | * @since 1.0 |
|
131 | 52 | * @access public |
|
132 | * |
||
133 | * @return void |
||
134 | */ |
||
135 | public function init() { |
||
137 | |||
138 | |||
139 | /** |
||
140 | * Set query filter. |
||
141 | * |
||
142 | * @since 1.8.9 |
||
143 | * @access private |
||
144 | 52 | */ |
|
145 | private function set_filters() { |
||
166 | |||
167 | /** |
||
168 | * Unset query filter. |
||
169 | * |
||
170 | * @since 1.8.9 |
||
171 | * @access private |
||
172 | */ |
||
173 | private function unset_filters() { |
||
177 | |||
178 | |||
179 | /** |
||
180 | * Retrieve payments. |
||
181 | * |
||
182 | * The query can be modified in two ways; either the action before the |
||
183 | * query is run, or the filter on the arguments (existing mainly for backwards |
||
184 | 52 | * compatibility). |
|
185 | 52 | * |
|
186 | 52 | * @since 1.0 |
|
187 | * @access public |
||
188 | * |
||
189 | * @return array |
||
190 | */ |
||
191 | public function get_payments() { |
||
243 | |||
244 | /** |
||
245 | * If querying a specific date, add the proper filters. |
||
246 | * |
||
247 | * @since 1.0 |
||
248 | * @access public |
||
249 | 52 | * |
|
250 | * @return void |
||
251 | 52 | */ |
|
252 | 42 | public function date_filter_pre() { |
|
261 | 52 | ||
262 | 52 | /** |
|
263 | * If querying a specific date, remove filters after the query has been run |
||
264 | * to avoid affecting future queries. |
||
265 | * |
||
266 | * @since 1.0 |
||
267 | * @access public |
||
268 | * |
||
269 | * @return void |
||
270 | */ |
||
271 | 52 | public function date_filter_post() { |
|
278 | |||
279 | /** |
||
280 | * Post Status |
||
281 | * |
||
282 | * @since 1.0 |
||
283 | * @access public |
||
284 | * |
||
285 | * @return void |
||
286 | */ |
||
287 | 52 | public function status() { |
|
295 | 52 | ||
296 | 52 | /** |
|
297 | 52 | * Current Page |
|
298 | * |
||
299 | * @since 1.0 |
||
300 | * @access public |
||
301 | * |
||
302 | * @return void |
||
303 | */ |
||
304 | public function page() { |
||
312 | |||
313 | /** |
||
314 | * Posts Per Page |
||
315 | * |
||
316 | * @since 1.0 |
||
317 | * @access public |
||
318 | * |
||
319 | * @return void |
||
320 | */ |
||
321 | public function per_page() { |
||
335 | |||
336 | 32 | /** |
|
337 | * Current Month |
||
338 | 32 | * |
|
339 | * @since 1.0 |
||
340 | * @access public |
||
341 | * |
||
342 | 32 | * @return void |
|
343 | 32 | */ |
|
344 | public function month() { |
||
352 | |||
353 | /** |
||
354 | * Order by |
||
355 | * |
||
356 | * @since 1.0 |
||
357 | * @access public |
||
358 | 32 | * |
|
359 | * @return void |
||
360 | 32 | */ |
|
361 | public function orderby() { |
||
382 | |||
383 | /** |
||
384 | * Custom orderby. |
||
385 | * Note: currently custom sorting is only used for donation listing page. |
||
386 | * |
||
387 | * @since 1.8 |
||
388 | * @access public |
||
389 | * |
||
390 | * @param string $order |
||
391 | * @param WP_Query $query |
||
392 | * |
||
393 | * @return mixed |
||
394 | */ |
||
395 | public function custom_orderby( $order, $query ) { |
||
414 | |||
415 | /** |
||
416 | * Specific User |
||
417 | * |
||
418 | * @since 1.0 |
||
419 | * @access public |
||
420 | * |
||
421 | * @return void |
||
422 | */ |
||
423 | public function user() { |
||
441 | |||
442 | /** |
||
443 | * Specific donor id |
||
444 | * |
||
445 | 52 | * @access public |
|
446 | 52 | * @since 1.8.9 |
|
447 | 52 | * @return void |
|
448 | */ |
||
449 | 52 | public function donor() { |
|
461 | |||
462 | /** |
||
463 | * Search |
||
464 | * |
||
465 | 52 | * @since 1.0 |
|
466 | 52 | * @access public |
|
467 | 52 | * |
|
468 | 52 | * @return void |
|
469 | 52 | */ |
|
470 | 52 | public function search() { |
|
576 | |||
577 | /** |
||
578 | * Payment Mode |
||
579 | * |
||
580 | * @since 1.0 |
||
581 | * @access public |
||
582 | * |
||
583 | * @return void |
||
584 | */ |
||
585 | public function mode() { |
||
599 | |||
600 | /** |
||
601 | * Children |
||
602 | * |
||
603 | * @since 1.0 |
||
604 | * @access public |
||
605 | * |
||
606 | * @return void |
||
607 | */ |
||
608 | public function children() { |
||
614 | |||
615 | /** |
||
616 | * Specific Give Form |
||
617 | * |
||
618 | * @since 1.0 |
||
619 | * @access public |
||
620 | * |
||
621 | * @return void |
||
622 | */ |
||
623 | View Code Duplication | public function give_forms() { |
|
648 | |||
649 | /** |
||
650 | * Specific Gateway |
||
651 | * |
||
652 | * @since 1.8.17 |
||
653 | * @access public |
||
654 | * |
||
655 | * @return void |
||
656 | */ |
||
657 | View Code Duplication | public function gateway_filter() { |
|
682 | |||
683 | } |
||
684 |