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_DB_Donors 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_DB_Donors, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Give_DB_Donors extends Give_DB { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Give_DB_Donors constructor. |
||
| 28 | * |
||
| 29 | * Set up the Give DB Donor class. |
||
| 30 | * |
||
| 31 | * @since 1.0 |
||
| 32 | * @access public |
||
| 33 | */ |
||
| 34 | public function __construct() { |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Get columns and formats |
||
| 55 | * |
||
| 56 | * @since 1.0 |
||
| 57 | * @access public |
||
| 58 | * |
||
| 59 | * @return array Columns and formats. |
||
| 60 | */ |
||
| 61 | View Code Duplication | public function get_columns() { |
|
| 62 | return array( |
||
| 63 | 'id' => '%d', |
||
| 64 | 'user_id' => '%d', |
||
| 65 | 'name' => '%s', |
||
| 66 | 'email' => '%s', |
||
| 67 | 'payment_ids' => '%s', |
||
| 68 | 'purchase_value' => '%f', |
||
| 69 | 'purchase_count' => '%d', |
||
| 70 | 'notes' => '%s', |
||
| 71 | 'date_created' => '%s', |
||
| 72 | ); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Get default column values |
||
| 77 | * |
||
| 78 | * @since 1.0 |
||
| 79 | * @access public |
||
| 80 | * |
||
| 81 | * @return array Default column values. |
||
| 82 | */ |
||
| 83 | View Code Duplication | public function get_column_defaults() { |
|
| 84 | return array( |
||
| 85 | 'user_id' => 0, |
||
| 86 | 'email' => '', |
||
| 87 | 'name' => '', |
||
| 88 | 'payment_ids' => '', |
||
| 89 | 'purchase_value' => 0.00, |
||
| 90 | 'purchase_count' => 0, |
||
| 91 | 'notes' => '', |
||
| 92 | 'date_created' => date( 'Y-m-d H:i:s' ), |
||
| 93 | ); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Add a donor |
||
| 98 | * |
||
| 99 | * @since 1.0 |
||
| 100 | * @access public |
||
| 101 | * |
||
| 102 | * @param array $data |
||
| 103 | * |
||
| 104 | * @return int|bool |
||
| 105 | */ |
||
| 106 | public function add( $data = array() ) { |
||
| 107 | |||
| 108 | $defaults = array( |
||
| 109 | 'payment_ids' => '' |
||
| 110 | ); |
||
| 111 | |||
| 112 | $args = wp_parse_args( $data, $defaults ); |
||
| 113 | |||
| 114 | if ( empty( $args['email'] ) ) { |
||
| 115 | return false; |
||
| 116 | } |
||
| 117 | |||
| 118 | View Code Duplication | if ( ! empty( $args['payment_ids'] ) && is_array( $args['payment_ids'] ) ) { |
|
| 119 | $args['payment_ids'] = implode( ',', array_unique( array_values( $args['payment_ids'] ) ) ); |
||
| 120 | } |
||
| 121 | |||
| 122 | $donor = $this->get_donor_by( 'email', $args['email'] ); |
||
| 123 | |||
| 124 | // update an existing donor. |
||
| 125 | if ( $donor ) { |
||
| 126 | |||
| 127 | // Update the payment IDs attached to the donor |
||
| 128 | if ( ! empty( $args['payment_ids'] ) ) { |
||
| 129 | |||
| 130 | if ( empty( $donor->payment_ids ) ) { |
||
| 131 | |||
| 132 | $donor->payment_ids = $args['payment_ids']; |
||
| 133 | |||
| 134 | } else { |
||
| 135 | |||
| 136 | $existing_ids = array_map( 'absint', explode( ',', $donor->payment_ids ) ); |
||
| 137 | $payment_ids = array_map( 'absint', explode( ',', $args['payment_ids'] ) ); |
||
| 138 | $payment_ids = array_merge( $payment_ids, $existing_ids ); |
||
| 139 | $donor->payment_ids = implode( ',', array_unique( array_values( $payment_ids ) ) ); |
||
| 140 | |||
| 141 | } |
||
| 142 | |||
| 143 | $args['payment_ids'] = $donor->payment_ids; |
||
| 144 | |||
| 145 | } |
||
| 146 | |||
| 147 | $this->update( $donor->id, $args ); |
||
| 148 | |||
| 149 | return $donor->id; |
||
| 150 | |||
| 151 | } else { |
||
| 152 | |||
| 153 | return $this->insert( $args, 'donor' ); |
||
| 154 | |||
| 155 | } |
||
| 156 | |||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Delete a donor. |
||
| 161 | * |
||
| 162 | * NOTE: This should not be called directly as it does not make necessary changes to |
||
| 163 | * the payment meta and logs. Use give_donor_delete() instead. |
||
| 164 | * |
||
| 165 | * @since 1.0 |
||
| 166 | * @access public |
||
| 167 | * |
||
| 168 | * @param bool|string|int $_id_or_email |
||
| 169 | * |
||
| 170 | * @return bool|int |
||
| 171 | */ |
||
| 172 | public function delete( $_id_or_email = false ) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Delete a donor. |
||
| 195 | * |
||
| 196 | * NOTE: This should not be called directly as it does not make necessary changes to |
||
| 197 | * the payment meta and logs. Use give_donor_delete() instead. |
||
| 198 | * |
||
| 199 | * @since 1.0 |
||
| 200 | * @access public |
||
| 201 | * |
||
| 202 | * @param int $user_id |
||
| 203 | * |
||
| 204 | * @return bool|int |
||
| 205 | */ |
||
| 206 | public function delete_by_user_id( $user_id = false ) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Checks if a donor exists |
||
| 218 | * |
||
| 219 | * @since 1.0 |
||
| 220 | * @access public |
||
| 221 | * |
||
| 222 | * @param string $value The value to search for. Default is empty. |
||
| 223 | * @param string $field The Donor ID or email to search in. Default is 'email'. |
||
| 224 | * |
||
| 225 | * @return bool True is exists, false otherwise. |
||
| 226 | */ |
||
| 227 | public function exists( $value = '', $field = 'email' ) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Attaches a payment ID to a donor |
||
| 240 | * |
||
| 241 | * @since 1.0 |
||
| 242 | * @access public |
||
| 243 | * |
||
| 244 | * @param int $donor_id Donor ID. |
||
| 245 | * @param int $payment_id Payment ID. |
||
| 246 | * |
||
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | public function attach_payment( $donor_id = 0, $payment_id = 0 ) { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Removes a payment ID from a donor. |
||
| 264 | * |
||
| 265 | * @since 1.0 |
||
| 266 | * @access public |
||
| 267 | * |
||
| 268 | * @param int $donor_id Donor ID. |
||
| 269 | * @param int $payment_id Payment ID. |
||
| 270 | * |
||
| 271 | * @return bool |
||
| 272 | */ |
||
| 273 | public function remove_payment( $donor_id = 0, $payment_id = 0 ) { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Increments donor's donation stats. |
||
| 288 | * |
||
| 289 | * @access public |
||
| 290 | * |
||
| 291 | * @param int $donor_id Donor ID. |
||
| 292 | * @param float $amount Amoumt. |
||
| 293 | * |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | View Code Duplication | public function increment_stats( $donor_id = 0, $amount = 0.00 ) { |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Decrements donor's donation stats. |
||
| 313 | * |
||
| 314 | * @since 1.0 |
||
| 315 | * @access public |
||
| 316 | * |
||
| 317 | * @param int $donor_id Donor ID. |
||
| 318 | * @param float $amount Amount. |
||
| 319 | * |
||
| 320 | * @return bool |
||
| 321 | */ |
||
| 322 | View Code Duplication | public function decrement_stats( $donor_id = 0, $amount = 0.00 ) { |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Updates the email address of a donor record when the email on a user is updated |
||
| 339 | * |
||
| 340 | * @since 1.4.3 |
||
| 341 | * @access public |
||
| 342 | * |
||
| 343 | * @param int $user_id User ID. |
||
| 344 | * @param WP_User|bool $old_user_data User data. |
||
| 345 | * |
||
| 346 | * @return bool |
||
| 347 | */ |
||
| 348 | public function update_donor_email_on_user_update( $user_id = 0, $old_user_data = false ) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Retrieves a single donor from the database |
||
| 398 | * |
||
| 399 | * @since 1.0 |
||
| 400 | * @access public |
||
| 401 | * |
||
| 402 | * @param string $field ID or email. Default is 'id'. |
||
| 403 | * @param mixed $value The Customer ID or email to search. Default is 0. |
||
| 404 | * |
||
| 405 | * @return mixed Upon success, an object of the donor. Upon failure, NULL |
||
| 406 | */ |
||
| 407 | public function get_donor_by( $field = 'id', $value = 0 ) { |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Retrieve donors from the database. |
||
| 476 | * |
||
| 477 | * @since 1.0 |
||
| 478 | * @access public |
||
| 479 | * |
||
| 480 | * @param array $args |
||
| 481 | * |
||
| 482 | * @return array|object|null Customers array or object. Null if not found. |
||
| 483 | */ |
||
| 484 | public function get_donors( $args = array() ) { |
||
| 603 | |||
| 604 | |||
| 605 | /** |
||
| 606 | * Count the total number of donors in the database |
||
| 607 | * |
||
| 608 | * @since 1.0 |
||
| 609 | * @access public |
||
| 610 | * |
||
| 611 | * @param array $args |
||
| 612 | * |
||
| 613 | * @return int Total number of donors. |
||
| 614 | */ |
||
| 615 | public function count( $args = array() ) { |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Create the table |
||
| 657 | * |
||
| 658 | * @since 1.0 |
||
| 659 | * @access public |
||
| 660 | * |
||
| 661 | * @return void |
||
| 662 | */ |
||
| 663 | View Code Duplication | public function create_table() { |
|
| 686 | |||
| 687 | /** |
||
| 688 | * Check if the Customers table was ever installed |
||
| 689 | * |
||
| 690 | * @since 1.4.3 |
||
| 691 | * @access public |
||
| 692 | * |
||
| 693 | * @return bool Returns if the donors table was installed and upgrade routine run. |
||
| 694 | */ |
||
| 695 | public function installed() { |
||
| 698 | |||
| 699 | } |
||
| 700 |