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() ) { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Set a query variable. |
||
| 100 | * |
||
| 101 | * @since 1.0 |
||
| 102 | * @access public |
||
| 103 | * |
||
| 104 | * @param $query_var |
||
| 105 | * @param $value |
||
| 106 | */ |
||
| 107 | public function __set( $query_var, $value ) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Unset a query variable. |
||
| 117 | * |
||
| 118 | * @since 1.0 |
||
| 119 | * @access public |
||
| 120 | * |
||
| 121 | * @param $query_var |
||
| 122 | */ |
||
| 123 | public function __unset( $query_var ) { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Modify the query/query arguments before we retrieve payments. |
||
| 129 | * |
||
| 130 | * @since 1.0 |
||
| 131 | * @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 | */ |
||
| 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 | * compatibility). |
||
| 185 | * |
||
| 186 | * @since 1.0 |
||
| 187 | * @access public |
||
| 188 | * |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | public function get_payments() { |
||
| 243 | |||
| 244 | |||
| 245 | /** |
||
| 246 | * Get payments by group |
||
| 247 | * |
||
| 248 | * @since 2.0 |
||
| 249 | * @access public |
||
| 250 | * |
||
| 251 | * @param string $group_by Valid donation property |
||
| 252 | * @param bool $count |
||
| 253 | * |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | public function get_payment_by_group( $group_by, $count = false ) { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * If querying a specific date, add the proper filters. |
||
| 305 | * |
||
| 306 | * @since 1.0 |
||
| 307 | * @access public |
||
| 308 | * |
||
| 309 | * @return void |
||
| 310 | */ |
||
| 311 | public function date_filter_pre() { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * If querying a specific date, remove filters after the query has been run |
||
| 323 | * to avoid affecting future queries. |
||
| 324 | * |
||
| 325 | * @since 1.0 |
||
| 326 | * @access public |
||
| 327 | * |
||
| 328 | * @return void |
||
| 329 | */ |
||
| 330 | public function date_filter_post() { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Post Status |
||
| 340 | * |
||
| 341 | * @since 1.0 |
||
| 342 | * @access public |
||
| 343 | * |
||
| 344 | * @return void |
||
| 345 | */ |
||
| 346 | public function status() { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Current Page |
||
| 357 | * |
||
| 358 | * @since 1.0 |
||
| 359 | * @access public |
||
| 360 | * |
||
| 361 | * @return void |
||
| 362 | */ |
||
| 363 | public function page() { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Posts Per Page |
||
| 374 | * |
||
| 375 | * @since 1.0 |
||
| 376 | * @access public |
||
| 377 | * |
||
| 378 | * @return void |
||
| 379 | */ |
||
| 380 | public function per_page() { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Current Month |
||
| 397 | * |
||
| 398 | * @since 1.0 |
||
| 399 | * @access public |
||
| 400 | * |
||
| 401 | * @return void |
||
| 402 | */ |
||
| 403 | public function month() { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Order by |
||
| 414 | * |
||
| 415 | * @since 1.0 |
||
| 416 | * @access public |
||
| 417 | * |
||
| 418 | * @return void |
||
| 419 | */ |
||
| 420 | public function orderby() { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Custom orderby. |
||
| 444 | * Note: currently custom sorting is only used for donation listing page. |
||
| 445 | * |
||
| 446 | * @since 1.8 |
||
| 447 | * @access public |
||
| 448 | * |
||
| 449 | * @param string $order |
||
| 450 | * @param WP_Query $query |
||
| 451 | * |
||
| 452 | * @return mixed |
||
| 453 | */ |
||
| 454 | public function custom_orderby( $order, $query ) { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Specific User |
||
| 473 | * |
||
| 474 | * @since 1.0 |
||
| 475 | * @access public |
||
| 476 | * |
||
| 477 | * @return void |
||
| 478 | */ |
||
| 479 | public function user() { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Specific donor id |
||
| 500 | * |
||
| 501 | * @access public |
||
| 502 | * @since 1.8.9 |
||
| 503 | * @return void |
||
| 504 | */ |
||
| 505 | public function donor() { |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Search |
||
| 520 | * |
||
| 521 | * @since 1.0 |
||
| 522 | * @access public |
||
| 523 | * |
||
| 524 | * @return void |
||
| 525 | */ |
||
| 526 | public function search() { |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Payment Mode |
||
| 635 | * |
||
| 636 | * @since 1.0 |
||
| 637 | * @access public |
||
| 638 | * |
||
| 639 | * @return void |
||
| 640 | */ |
||
| 641 | public function mode() { |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Children |
||
| 658 | * |
||
| 659 | * @since 1.0 |
||
| 660 | * @access public |
||
| 661 | * |
||
| 662 | * @return void |
||
| 663 | */ |
||
| 664 | public function children() { |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Specific Give Form |
||
| 673 | * |
||
| 674 | * @since 1.0 |
||
| 675 | * @access public |
||
| 676 | * |
||
| 677 | * @return void |
||
| 678 | */ |
||
| 679 | View Code Duplication | public function give_forms() { |
|
| 704 | |||
| 705 | /** |
||
| 706 | * Specific Gateway |
||
| 707 | * |
||
| 708 | * @since 1.8.17 |
||
| 709 | * @access public |
||
| 710 | * |
||
| 711 | * @return void |
||
| 712 | */ |
||
| 713 | View Code Duplication | public function gateway_filter() { |
|
| 738 | |||
| 739 | } |
||
| 740 |