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 | * Default query arguments. |
||
| 80 | * |
||
| 81 | * Not all of these are valid arguments that can be passed to WP_Query. The ones that are not, are modified before |
||
| 82 | * the query is run to convert them to the proper syntax. |
||
| 83 | * |
||
| 84 | * @since 1.8.14 |
||
| 85 | * @access public |
||
| 86 | * |
||
| 87 | * @param $args array The array of arguments that can be passed in and used for setting up this payment query. |
||
| 88 | */ |
||
| 89 | public function __construct( $args = array() ) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Modify the query/query arguments before we retrieve donors. |
||
| 127 | * |
||
| 128 | * @since 1.8.14 |
||
| 129 | * @access public |
||
| 130 | * |
||
| 131 | * @return void |
||
| 132 | */ |
||
| 133 | public function init() { |
||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * Retrieve donors. |
||
| 139 | * |
||
| 140 | * The query can be modified in two ways; either the action before the |
||
| 141 | * query is run, or the filter on the arguments (existing mainly for backwards |
||
| 142 | * compatibility). |
||
| 143 | * |
||
| 144 | * @since 1.8.14 |
||
| 145 | * @access public |
||
| 146 | * |
||
| 147 | * @global wpdb $wpdb |
||
| 148 | * |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | public function get_donors() { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get sql query from queried array. |
||
| 193 | * |
||
| 194 | * @since 2.0 |
||
| 195 | * @access public |
||
| 196 | * |
||
| 197 | * @global wpdb $wpdb |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | public function get_sql() { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Set query where clause. |
||
| 243 | * |
||
| 244 | * @since 1.8.14 |
||
| 245 | * @access private |
||
| 246 | * |
||
| 247 | * @global wpdb $wpdb |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | private function get_where_query() { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Set email where clause. |
||
| 278 | * |
||
| 279 | * @since 1.8.14 |
||
| 280 | * @access private |
||
| 281 | * |
||
| 282 | * @global wpdb $wpdb |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | private function get_where_email() { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Set donor where clause. |
||
| 309 | * |
||
| 310 | * @since 1.8.14 |
||
| 311 | * @access private |
||
| 312 | * |
||
| 313 | * @global wpdb $wpdb |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | View Code Duplication | private function get_where_donor() { |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Set date where clause. |
||
| 334 | * |
||
| 335 | * @since 1.8.14 |
||
| 336 | * @access private |
||
| 337 | * |
||
| 338 | * @global wpdb $wpdb |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | private function get_where_date() { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Set search where clause. |
||
| 364 | * |
||
| 365 | * @since 1.8.14 |
||
| 366 | * @access private |
||
| 367 | * |
||
| 368 | * @global wpdb $wpdb |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | private function get_where_search() { |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Set user where clause. |
||
| 396 | * |
||
| 397 | * @since 1.8.14 |
||
| 398 | * @access private |
||
| 399 | * |
||
| 400 | * @global wpdb $wpdb |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | View Code Duplication | private function get_where_user() { |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Set orderby query |
||
| 421 | * |
||
| 422 | * @since 1.8.14 |
||
| 423 | * @access private |
||
| 424 | * |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | private function get_order_query() { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Set donation count value where clause. |
||
| 473 | * @todo: add phpunit test |
||
| 474 | * |
||
| 475 | * @since 2.2.0 |
||
| 476 | * @access private |
||
| 477 | * |
||
| 478 | * @global wpdb $wpdb |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | View Code Duplication | private function get_where_donation_count() { |
|
| 497 | |||
| 498 | /** |
||
| 499 | * Set purchase value where clause. |
||
| 500 | * @todo: add phpunit test |
||
| 501 | * |
||
| 502 | * @since 2.1.0 |
||
| 503 | * @access private |
||
| 504 | * |
||
| 505 | * @global wpdb $wpdb |
||
| 506 | * @return string |
||
| 507 | */ |
||
| 508 | View Code Duplication | private function get_where_donation_amount() { |
|
| 524 | |||
| 525 | /** |
||
| 526 | * Set give_forms where clause. |
||
| 527 | * |
||
| 528 | * @todo : add phpunit test |
||
| 529 | * |
||
| 530 | * @since 2.1.0 |
||
| 531 | * @access private |
||
| 532 | * |
||
| 533 | * @global wpdb $wpdb |
||
| 534 | * @return string |
||
| 535 | */ |
||
| 536 | private function get_where_give_forms() { |
||
| 578 | } |
||
| 579 |