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_Donors_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_Donors_Query, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Give_Donors_Query { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The args to pass to the give_get_donors() query |
||
| 30 | * |
||
| 31 | * @since 1.8.14 |
||
| 32 | * @access public |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | public $args = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The donors found based on the criteria set |
||
| 40 | * |
||
| 41 | * @since 1.8.14 |
||
| 42 | * @access public |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | public $donors = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The donors found based on the criteria set |
||
| 50 | * |
||
| 51 | * @since 1.8.14 |
||
| 52 | * @access public |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | public $table_name = ''; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The donors found based on the criteria set |
||
| 60 | * |
||
| 61 | * @since 1.8.14 |
||
| 62 | * @access public |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | public $meta_table_name = ''; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The donors found based on the criteria set |
||
| 70 | * |
||
| 71 | * @since 1.8.14 |
||
| 72 | * @access public |
||
| 73 | * |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | public $meta_type = ''; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Preserve args |
||
| 80 | * |
||
| 81 | * @since 2.4.0 |
||
| 82 | * @access public |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | public $_args = array(); |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Default query arguments. |
||
| 90 | * |
||
| 91 | * Not all of these are valid arguments that can be passed to WP_Query. The ones that are not, are modified before |
||
| 92 | * the query is run to convert them to the proper syntax. |
||
| 93 | * |
||
| 94 | * @since 1.8.14 |
||
| 95 | * @access public |
||
| 96 | * |
||
| 97 | * @param $args array The array of arguments that can be passed in and used for setting up this payment query. |
||
| 98 | */ |
||
| 99 | public function __construct( $args = array() ) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Modify the query/query arguments before we retrieve donors. |
||
| 140 | * |
||
| 141 | * @since 1.8.14 |
||
| 142 | * @access public |
||
| 143 | * |
||
| 144 | * @return void |
||
| 145 | */ |
||
| 146 | public function init() { |
||
| 148 | |||
| 149 | |||
| 150 | /** |
||
| 151 | * Retrieve donors. |
||
| 152 | * |
||
| 153 | * The query can be modified in two ways; either the action before the |
||
| 154 | * query is run, or the filter on the arguments (existing mainly for backwards |
||
| 155 | * compatibility). |
||
| 156 | * |
||
| 157 | * @since 1.8.14 |
||
| 158 | * @access public |
||
| 159 | * |
||
| 160 | * @global wpdb $wpdb |
||
| 161 | * |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | public function get_donors() { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get sql query from queried array. |
||
| 206 | * |
||
| 207 | * @since 2.0 |
||
| 208 | * @access public |
||
| 209 | * |
||
| 210 | * @global wpdb $wpdb |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | public function get_sql() { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Set query where clause. |
||
| 256 | * |
||
| 257 | * @since 1.8.14 |
||
| 258 | * @access private |
||
| 259 | * |
||
| 260 | * @global wpdb $wpdb |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | private function get_where_query() { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Set email where clause. |
||
| 291 | * |
||
| 292 | * @since 1.8.14 |
||
| 293 | * @access private |
||
| 294 | * |
||
| 295 | * @global wpdb $wpdb |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | private function get_where_email() { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Set donor where clause. |
||
| 322 | * |
||
| 323 | * @since 1.8.14 |
||
| 324 | * @access private |
||
| 325 | * |
||
| 326 | * @global wpdb $wpdb |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | View Code Duplication | private function get_where_donor() { |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Set date where clause. |
||
| 347 | * |
||
| 348 | * @since 1.8.14 |
||
| 349 | * @access private |
||
| 350 | * |
||
| 351 | * @global wpdb $wpdb |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | private function get_where_date() { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Set search where clause. |
||
| 377 | * |
||
| 378 | * @since 1.8.14 |
||
| 379 | * @access private |
||
| 380 | * |
||
| 381 | * @global wpdb $wpdb |
||
| 382 | * @return string |
||
| 383 | */ |
||
| 384 | private function get_where_search() { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Set user where clause. |
||
| 420 | * |
||
| 421 | * @since 1.8.14 |
||
| 422 | * @access private |
||
| 423 | * |
||
| 424 | * @global wpdb $wpdb |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | View Code Duplication | private function get_where_user() { |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Set orderby query |
||
| 445 | * |
||
| 446 | * @since 1.8.14 |
||
| 447 | * @access private |
||
| 448 | * |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | private function get_order_query() { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Set donation count value where clause. |
||
| 497 | * @todo: add phpunit test |
||
| 498 | * |
||
| 499 | * @since 2.2.0 |
||
| 500 | * @access private |
||
| 501 | * |
||
| 502 | * @global wpdb $wpdb |
||
| 503 | * @return string |
||
| 504 | */ |
||
| 505 | View Code Duplication | private function get_where_donation_count() { |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Set purchase value where clause. |
||
| 524 | * @todo: add phpunit test |
||
| 525 | * |
||
| 526 | * @since 2.1.0 |
||
| 527 | * @access private |
||
| 528 | * |
||
| 529 | * @global wpdb $wpdb |
||
| 530 | * @return string |
||
| 531 | */ |
||
| 532 | View Code Duplication | private function get_where_donation_amount() { |
|
| 548 | |||
| 549 | /** |
||
| 550 | * Set give_forms where clause. |
||
| 551 | * |
||
| 552 | * @todo : add phpunit test |
||
| 553 | * |
||
| 554 | * @since 2.1.0 |
||
| 555 | * @access private |
||
| 556 | * |
||
| 557 | * @global wpdb $wpdb |
||
| 558 | * @return string |
||
| 559 | */ |
||
| 560 | private function get_where_give_forms() { |
||
| 602 | |||
| 603 | /** |
||
| 604 | * If querying a specific date, add the proper filters. |
||
| 605 | * Note: This function currently only accept dates with admin defined core date format |
||
| 606 | * |
||
| 607 | * @since 2.4.0 |
||
| 608 | * @access public |
||
| 609 | * |
||
| 610 | * @return void |
||
| 611 | */ |
||
| 612 | public function date_filter_pre() { |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Set a query variable. |
||
| 639 | * |
||
| 640 | * @since 2.4.0 |
||
| 641 | * @access public |
||
| 642 | * |
||
| 643 | * @param $query_var |
||
| 644 | * @param $value |
||
| 645 | */ |
||
| 646 | View Code Duplication | public function __set( $query_var, $value ) { |
|
| 653 | |||
| 654 | } |
||
| 655 |