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 | |||
| 58 | /** |
||
| 59 | * Default query arguments. |
||
| 60 | * |
||
| 61 | * 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 | * |
||
| 63 | * @since 1.0 |
||
| 64 | * @access public |
||
| 65 | * |
||
| 66 | * @param $args array The array of arguments that can be passed in and used for setting up this payment query. |
||
| 67 | */ |
||
| 68 | public function __construct( $args = array() ) { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Set a query variable. |
||
| 104 | * |
||
| 105 | * @since 1.0 |
||
| 106 | * @access public |
||
| 107 | * |
||
| 108 | * @param $query_var |
||
| 109 | * @param $value |
||
| 110 | */ |
||
| 111 | public function __set( $query_var, $value ) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Unset a query variable. |
||
| 121 | * |
||
| 122 | * @since 1.0 |
||
| 123 | * @access public |
||
| 124 | * |
||
| 125 | * @param $query_var |
||
| 126 | */ |
||
| 127 | public function __unset( $query_var ) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Modify the query/query arguments before we retrieve payments. |
||
| 133 | * |
||
| 134 | * @since 1.0 |
||
| 135 | * @access public |
||
| 136 | * |
||
| 137 | * @return void |
||
| 138 | */ |
||
| 139 | public function init() { |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * Set query filter. |
||
| 145 | * |
||
| 146 | * @since 1.8.9 |
||
| 147 | * @access private |
||
| 148 | */ |
||
| 149 | private function set_filters() { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Unset query filter. |
||
| 173 | * |
||
| 174 | * @since 1.8.9 |
||
| 175 | * @access private |
||
| 176 | */ |
||
| 177 | private function unset_filters() { |
||
| 181 | |||
| 182 | |||
| 183 | /** |
||
| 184 | * Retrieve payments. |
||
| 185 | * |
||
| 186 | * The query can be modified in two ways; either the action before the |
||
| 187 | * query is run, or the filter on the arguments (existing mainly for backwards |
||
| 188 | * compatibility). |
||
| 189 | * |
||
| 190 | * @since 1.0 |
||
| 191 | * @access public |
||
| 192 | * |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | public function get_payments() { |
||
| 247 | |||
| 248 | |||
| 249 | /** |
||
| 250 | * Get payments by group |
||
| 251 | * |
||
| 252 | * @since 1.8.17 |
||
| 253 | * @access public |
||
| 254 | * |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | public function get_payment_by_group() { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * If querying a specific date, add the proper filters. |
||
| 332 | * |
||
| 333 | * @since 1.0 |
||
| 334 | * @access public |
||
| 335 | * |
||
| 336 | * @return void |
||
| 337 | */ |
||
| 338 | public function date_filter_pre() { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * If querying a specific date, remove filters after the query has been run |
||
| 350 | * to avoid affecting future queries. |
||
| 351 | * |
||
| 352 | * @since 1.0 |
||
| 353 | * @access public |
||
| 354 | * |
||
| 355 | * @return void |
||
| 356 | */ |
||
| 357 | public function date_filter_post() { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Post Status |
||
| 367 | * |
||
| 368 | * @since 1.0 |
||
| 369 | * @access public |
||
| 370 | * |
||
| 371 | * @return void |
||
| 372 | */ |
||
| 373 | public function status() { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Current Page |
||
| 384 | * |
||
| 385 | * @since 1.0 |
||
| 386 | * @access public |
||
| 387 | * |
||
| 388 | * @return void |
||
| 389 | */ |
||
| 390 | public function page() { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Posts Per Page |
||
| 401 | * |
||
| 402 | * @since 1.0 |
||
| 403 | * @access public |
||
| 404 | * |
||
| 405 | * @return void |
||
| 406 | */ |
||
| 407 | public function per_page() { |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Current Month |
||
| 424 | * |
||
| 425 | * @since 1.0 |
||
| 426 | * @access public |
||
| 427 | * |
||
| 428 | * @return void |
||
| 429 | */ |
||
| 430 | public function month() { |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Order by |
||
| 441 | * |
||
| 442 | * @since 1.0 |
||
| 443 | * @access public |
||
| 444 | * |
||
| 445 | * @return void |
||
| 446 | */ |
||
| 447 | public function orderby() { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Custom orderby. |
||
| 471 | * Note: currently custom sorting is only used for donation listing page. |
||
| 472 | * |
||
| 473 | * @since 1.8 |
||
| 474 | * @access public |
||
| 475 | * |
||
| 476 | * @param string $order |
||
| 477 | * @param WP_Query $query |
||
| 478 | * |
||
| 479 | * @return mixed |
||
| 480 | */ |
||
| 481 | public function custom_orderby( $order, $query ) { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Specific User |
||
| 500 | * |
||
| 501 | * @since 1.0 |
||
| 502 | * @access public |
||
| 503 | * |
||
| 504 | * @return void |
||
| 505 | */ |
||
| 506 | public function user() { |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Specific donor id |
||
| 527 | * |
||
| 528 | * @access public |
||
| 529 | * @since 1.8.9 |
||
| 530 | * @return void |
||
| 531 | */ |
||
| 532 | public function donor() { |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Search |
||
| 547 | * |
||
| 548 | * @since 1.0 |
||
| 549 | * @access public |
||
| 550 | * |
||
| 551 | * @return void |
||
| 552 | */ |
||
| 553 | public function search() { |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Payment Mode |
||
| 662 | * |
||
| 663 | * @since 1.0 |
||
| 664 | * @access public |
||
| 665 | * |
||
| 666 | * @return void |
||
| 667 | */ |
||
| 668 | public function mode() { |
||
| 682 | |||
| 683 | /** |
||
| 684 | * Children |
||
| 685 | * |
||
| 686 | * @since 1.0 |
||
| 687 | * @access public |
||
| 688 | * |
||
| 689 | * @return void |
||
| 690 | */ |
||
| 691 | public function children() { |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Specific Give Form |
||
| 700 | * |
||
| 701 | * @since 1.0 |
||
| 702 | * @access public |
||
| 703 | * |
||
| 704 | * @return void |
||
| 705 | */ |
||
| 706 | View Code Duplication | public function give_forms() { |
|
| 731 | |||
| 732 | /** |
||
| 733 | * Specific Gateway |
||
| 734 | * |
||
| 735 | * @since 1.8.17 |
||
| 736 | * @access public |
||
| 737 | * |
||
| 738 | * @return void |
||
| 739 | */ |
||
| 740 | View Code Duplication | public function gateway_filter() { |
|
| 765 | |||
| 766 | } |
||
| 767 |