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 ) { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * If querying a specific date, add the proper filters. |
||
| 308 | * |
||
| 309 | * @since 1.0 |
||
| 310 | * @access public |
||
| 311 | * |
||
| 312 | * @return void |
||
| 313 | */ |
||
| 314 | public function date_filter_pre() { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * If querying a specific date, remove filters after the query has been run |
||
| 326 | * to avoid affecting future queries. |
||
| 327 | * |
||
| 328 | * @since 1.0 |
||
| 329 | * @access public |
||
| 330 | * |
||
| 331 | * @return void |
||
| 332 | */ |
||
| 333 | public function date_filter_post() { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Post Status |
||
| 343 | * |
||
| 344 | * @since 1.0 |
||
| 345 | * @access public |
||
| 346 | * |
||
| 347 | * @return void |
||
| 348 | */ |
||
| 349 | public function status() { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Current Page |
||
| 360 | * |
||
| 361 | * @since 1.0 |
||
| 362 | * @access public |
||
| 363 | * |
||
| 364 | * @return void |
||
| 365 | */ |
||
| 366 | public function page() { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Posts Per Page |
||
| 377 | * |
||
| 378 | * @since 1.0 |
||
| 379 | * @access public |
||
| 380 | * |
||
| 381 | * @return void |
||
| 382 | */ |
||
| 383 | public function per_page() { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Current Month |
||
| 400 | * |
||
| 401 | * @since 1.0 |
||
| 402 | * @access public |
||
| 403 | * |
||
| 404 | * @return void |
||
| 405 | */ |
||
| 406 | public function month() { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Order by |
||
| 417 | * |
||
| 418 | * @since 1.0 |
||
| 419 | * @access public |
||
| 420 | * |
||
| 421 | * @return void |
||
| 422 | */ |
||
| 423 | public function orderby() { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Custom orderby. |
||
| 447 | * Note: currently custom sorting is only used for donation listing page. |
||
| 448 | * |
||
| 449 | * @since 1.8 |
||
| 450 | * @access public |
||
| 451 | * |
||
| 452 | * @param string $order |
||
| 453 | * @param WP_Query $query |
||
| 454 | * |
||
| 455 | * @return mixed |
||
| 456 | */ |
||
| 457 | public function custom_orderby( $order, $query ) { |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Specific User |
||
| 476 | * |
||
| 477 | * @since 1.0 |
||
| 478 | * @access public |
||
| 479 | * |
||
| 480 | * @return void |
||
| 481 | */ |
||
| 482 | public function user() { |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Specific donor id |
||
| 503 | * |
||
| 504 | * @access public |
||
| 505 | * @since 1.8.9 |
||
| 506 | * @return void |
||
| 507 | */ |
||
| 508 | public function donor() { |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Search |
||
| 523 | * |
||
| 524 | * @since 1.0 |
||
| 525 | * @access public |
||
| 526 | * |
||
| 527 | * @return void |
||
| 528 | */ |
||
| 529 | public function search() { |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Payment Mode |
||
| 638 | * |
||
| 639 | * @since 1.0 |
||
| 640 | * @access public |
||
| 641 | * |
||
| 642 | * @return void |
||
| 643 | */ |
||
| 644 | public function mode() { |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Children |
||
| 661 | * |
||
| 662 | * @since 1.0 |
||
| 663 | * @access public |
||
| 664 | * |
||
| 665 | * @return void |
||
| 666 | */ |
||
| 667 | public function children() { |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Specific Give Form |
||
| 676 | * |
||
| 677 | * @since 1.0 |
||
| 678 | * @access public |
||
| 679 | * |
||
| 680 | * @return void |
||
| 681 | */ |
||
| 682 | View Code Duplication | public function give_forms() { |
|
| 707 | |||
| 708 | /** |
||
| 709 | * Specific Gateway |
||
| 710 | * |
||
| 711 | * @since 1.8.17 |
||
| 712 | * @access public |
||
| 713 | * |
||
| 714 | * @return void |
||
| 715 | */ |
||
| 716 | View Code Duplication | public function gateway_filter() { |
|
| 741 | |||
| 742 | } |
||
| 743 |