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_Donor 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_Donor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Give_Donor { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The donor ID |
||
| 28 | * |
||
| 29 | * @since 1.0 |
||
| 30 | * @access public |
||
| 31 | * |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | public $id = 0; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The donor's donation count. |
||
| 38 | * |
||
| 39 | * @since 1.0 |
||
| 40 | * @access public |
||
| 41 | * |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | public $purchase_count = 0; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The donor's lifetime value. |
||
| 48 | * |
||
| 49 | * @since 1.0 |
||
| 50 | * @access public |
||
| 51 | * |
||
| 52 | * @var int |
||
| 53 | */ |
||
| 54 | public $purchase_value = 0; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The donor's email. |
||
| 58 | * |
||
| 59 | * @since 1.0 |
||
| 60 | * @access public |
||
| 61 | * |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | public $email; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The donor's emails. |
||
| 68 | * |
||
| 69 | * @since 1.7 |
||
| 70 | * @access public |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | public $emails; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The donor's name. |
||
| 78 | * |
||
| 79 | * @since 1.0 |
||
| 80 | * @access public |
||
| 81 | * |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | public $name; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The donor creation date. |
||
| 88 | * |
||
| 89 | * @since 1.0 |
||
| 90 | * @access public |
||
| 91 | * |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | public $date_created; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * The payment IDs associated with the donor. |
||
| 98 | * |
||
| 99 | * @since 1.0 |
||
| 100 | * @access public |
||
| 101 | * |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | public $payment_ids; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * The user ID associated with the donor. |
||
| 108 | * |
||
| 109 | * @since 1.0 |
||
| 110 | * @access public |
||
| 111 | * |
||
| 112 | * @var int |
||
| 113 | */ |
||
| 114 | public $user_id; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Donor notes saved by admins. |
||
| 118 | * |
||
| 119 | * @since 1.0 |
||
| 120 | * @access public |
||
| 121 | * |
||
| 122 | * @var array |
||
| 123 | */ |
||
| 124 | public $notes; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Donor address. |
||
| 128 | * |
||
| 129 | * @since 1.0 |
||
| 130 | * @access public |
||
| 131 | * |
||
| 132 | * @var array |
||
| 133 | */ |
||
| 134 | public $address = array(); |
||
| 135 | |||
| 136 | /** |
||
| 137 | * The Database Abstraction |
||
| 138 | * |
||
| 139 | * @since 1.0 |
||
| 140 | * @access protected |
||
| 141 | * |
||
| 142 | * @var Give_DB_Donors |
||
| 143 | */ |
||
| 144 | protected $db; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Give_Donor constructor. |
||
| 148 | * |
||
| 149 | * @param int|bool $_id_or_email |
||
| 150 | * @param bool $by_user_id |
||
| 151 | */ |
||
| 152 | public function __construct( $_id_or_email = false, $by_user_id = false ) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Setup Donor |
||
| 180 | * |
||
| 181 | * Set donor variables. |
||
| 182 | * |
||
| 183 | * @since 1.0 |
||
| 184 | * @access private |
||
| 185 | * |
||
| 186 | * @param object $donor The Donor Object. |
||
| 187 | * |
||
| 188 | * @return bool If the setup was successful or not. |
||
| 189 | */ |
||
| 190 | private function setup_donor( $donor ) { |
||
| 236 | |||
| 237 | |||
| 238 | /** |
||
| 239 | * Setup donor address. |
||
| 240 | * |
||
| 241 | * @since 2.0 |
||
| 242 | * @access public |
||
| 243 | */ |
||
| 244 | public function setup_address() { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Returns the saved address for a donor |
||
| 273 | * |
||
| 274 | * @access public |
||
| 275 | * |
||
| 276 | * @since 2.1.3 |
||
| 277 | * |
||
| 278 | * @param array $args donor address. |
||
| 279 | * |
||
| 280 | * @return array The donor's address, if any |
||
| 281 | */ |
||
| 282 | public function get_donor_address( $args = array() ) { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Magic __get function to dispatch a call to retrieve a private property. |
||
| 326 | * |
||
| 327 | * @since 1.0 |
||
| 328 | * @access public |
||
| 329 | * |
||
| 330 | * @param $key |
||
| 331 | * |
||
| 332 | * @return mixed|\WP_Error |
||
| 333 | */ |
||
| 334 | View Code Duplication | public function __get( $key ) { |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Creates a donor. |
||
| 351 | * |
||
| 352 | * @since 1.0 |
||
| 353 | * @access public |
||
| 354 | * |
||
| 355 | * @param array $data Array of attributes for a donor. |
||
| 356 | * |
||
| 357 | * @return bool|int False if not a valid creation, donor ID if user is found or valid creation. |
||
| 358 | */ |
||
| 359 | public function create( $data = array() ) { |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Updates a donor record. |
||
| 419 | * |
||
| 420 | * @since 1.0 |
||
| 421 | * @access public |
||
| 422 | * |
||
| 423 | * @param array $data Array of data attributes for a donor (checked via whitelist). |
||
| 424 | * |
||
| 425 | * @return bool If the update was successful or not. |
||
| 426 | */ |
||
| 427 | public function update( $data = array() ) { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Attach Payment |
||
| 472 | * |
||
| 473 | * Attach payment to the donor then triggers increasing stats. |
||
| 474 | * |
||
| 475 | * @since 1.0 |
||
| 476 | * @access public |
||
| 477 | * |
||
| 478 | * @param int $payment_id The payment ID to attach to the donor. |
||
| 479 | * @param bool $update_stats For backwards compatibility, if we should increase the stats or not. |
||
| 480 | * |
||
| 481 | * @return bool If the attachment was successfully. |
||
| 482 | */ |
||
| 483 | public function attach_payment( $payment_id = 0, $update_stats = true ) { |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Remove Payment |
||
| 551 | * |
||
| 552 | * Remove a payment from this donor, then triggers reducing stats. |
||
| 553 | * |
||
| 554 | * @since 1.0 |
||
| 555 | * @access public |
||
| 556 | * |
||
| 557 | * @param int $payment_id The Payment ID to remove. |
||
| 558 | * @param bool $update_stats For backwards compatibility, if we should increase the stats or not. |
||
| 559 | * |
||
| 560 | * @return boolean If the removal was successful. |
||
| 561 | */ |
||
| 562 | public function remove_payment( $payment_id = 0, $update_stats = true ) { |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Increase the donation count of a donor. |
||
| 637 | * |
||
| 638 | * @since 1.0 |
||
| 639 | * @access public |
||
| 640 | * |
||
| 641 | * @param int $count The number to increase by. |
||
| 642 | * |
||
| 643 | * @return int The donation count. |
||
| 644 | */ |
||
| 645 | public function increase_purchase_count( $count = 1 ) { |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Decrease the donor donation count. |
||
| 684 | * |
||
| 685 | * @since 1.0 |
||
| 686 | * @access public |
||
| 687 | * |
||
| 688 | * @param int $count The amount to decrease by. |
||
| 689 | * |
||
| 690 | * @return mixed If successful, the new count, otherwise false. |
||
| 691 | */ |
||
| 692 | public function decrease_donation_count( $count = 1 ) { |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Increase the donor's lifetime value. |
||
| 735 | * |
||
| 736 | * @since 1.0 |
||
| 737 | * @access public |
||
| 738 | * |
||
| 739 | * @param float $value The value to increase by. |
||
| 740 | * |
||
| 741 | * @return mixed If successful, the new value, otherwise false. |
||
| 742 | */ |
||
| 743 | public function increase_value( $value = 0.00 ) { |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Decrease a donor's lifetime value. |
||
| 777 | * |
||
| 778 | * @since 1.0 |
||
| 779 | * @access public |
||
| 780 | * |
||
| 781 | * @param float $value The value to decrease by. |
||
| 782 | * |
||
| 783 | * @return mixed If successful, the new value, otherwise false. |
||
| 784 | */ |
||
| 785 | public function decrease_value( $value = 0.00 ) { |
||
| 820 | |||
| 821 | /** |
||
| 822 | * Decrease/Increase a donor's lifetime value. |
||
| 823 | * |
||
| 824 | * This function will update donation stat on basis of current amount and new amount donation difference. |
||
| 825 | * Difference value can positive or negative. Negative value will decrease user donation stat while positive value |
||
| 826 | * increase donation stat. |
||
| 827 | * |
||
| 828 | * @since 1.0 |
||
| 829 | * @access public |
||
| 830 | * |
||
| 831 | * @param float $curr_amount Current Donation amount. |
||
| 832 | * @param float $new_amount New (changed) Donation amount. |
||
| 833 | * |
||
| 834 | * @return mixed If successful, the new donation stat value, otherwise false. |
||
| 835 | */ |
||
| 836 | public function update_donation_value( $curr_amount, $new_amount ) { |
||
| 859 | |||
| 860 | /** |
||
| 861 | * Get the parsed notes for a donor as an array. |
||
| 862 | * |
||
| 863 | * @since 1.0 |
||
| 864 | * @access public |
||
| 865 | * |
||
| 866 | * @param int $length The number of notes to get. |
||
| 867 | * @param int $paged What note to start at. |
||
| 868 | * |
||
| 869 | * @return array The notes requested. |
||
| 870 | */ |
||
| 871 | public function get_notes( $length = 20, $paged = 1 ) { |
||
| 884 | |||
| 885 | /** |
||
| 886 | * Get the total number of notes we have after parsing. |
||
| 887 | * |
||
| 888 | * @since 1.0 |
||
| 889 | * @access public |
||
| 890 | * |
||
| 891 | * @return int The number of notes for the donor. |
||
| 892 | */ |
||
| 893 | public function get_notes_count() { |
||
| 901 | |||
| 902 | /** |
||
| 903 | * Get the total donation amount. |
||
| 904 | * |
||
| 905 | * @since 1.8.17 |
||
| 906 | * |
||
| 907 | * @param array $args Pass any additional data. |
||
| 908 | * |
||
| 909 | * @return string|float |
||
| 910 | */ |
||
| 911 | public function get_total_donation_amount( $args = array() ) { |
||
| 924 | |||
| 925 | /** |
||
| 926 | * Add a note for the donor. |
||
| 927 | * |
||
| 928 | * @since 1.0 |
||
| 929 | * @access public |
||
| 930 | * |
||
| 931 | * @param string $note The note to add. Default is empty. |
||
| 932 | * |
||
| 933 | * @return string|boolean The new note if added successfully, false otherwise. |
||
| 934 | */ |
||
| 935 | public function add_note( $note = '' ) { |
||
| 995 | |||
| 996 | /** |
||
| 997 | * Get the notes column for the donor |
||
| 998 | * |
||
| 999 | * @since 1.0 |
||
| 1000 | * @access private |
||
| 1001 | * |
||
| 1002 | * @return string The Notes for the donor, non-parsed. |
||
| 1003 | */ |
||
| 1004 | private function get_raw_notes() { |
||
| 1024 | |||
| 1025 | /** |
||
| 1026 | * Retrieve a meta field for a donor. |
||
| 1027 | * |
||
| 1028 | * @since 1.6 |
||
| 1029 | * @access public |
||
| 1030 | * |
||
| 1031 | * @param string $meta_key The meta key to retrieve. Default is empty. |
||
| 1032 | * @param bool $single Whether to return a single value. Default is true. |
||
| 1033 | * |
||
| 1034 | * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is |
||
| 1035 | * true. |
||
| 1036 | */ |
||
| 1037 | public function get_meta( $meta_key = '', $single = true ) { |
||
| 1040 | |||
| 1041 | /** |
||
| 1042 | * Add a meta data field to a donor. |
||
| 1043 | * |
||
| 1044 | * @since 1.6 |
||
| 1045 | * @access public |
||
| 1046 | * |
||
| 1047 | * @param string $meta_key Metadata name. Default is empty. |
||
| 1048 | * @param mixed $meta_value Metadata value. |
||
| 1049 | * @param bool $unique Optional. Whether the same key should not be added. Default is false. |
||
| 1050 | * |
||
| 1051 | * @return bool False for failure. True for success. |
||
| 1052 | */ |
||
| 1053 | public function add_meta( $meta_key = '', $meta_value, $unique = false ) { |
||
| 1056 | |||
| 1057 | /** |
||
| 1058 | * Update a meta field based on donor ID. |
||
| 1059 | * |
||
| 1060 | * @since 1.6 |
||
| 1061 | * @access public |
||
| 1062 | * |
||
| 1063 | * @param string $meta_key Metadata key. Default is empty. |
||
| 1064 | * @param mixed $meta_value Metadata value. |
||
| 1065 | * @param mixed $prev_value Optional. Previous value to check before removing. Default is empty. |
||
| 1066 | * |
||
| 1067 | * @return bool False on failure, true if success. |
||
| 1068 | */ |
||
| 1069 | public function update_meta( $meta_key = '', $meta_value, $prev_value = '' ) { |
||
| 1072 | |||
| 1073 | /** |
||
| 1074 | * Remove metadata matching criteria from a donor. |
||
| 1075 | * |
||
| 1076 | * @since 1.6 |
||
| 1077 | * @access public |
||
| 1078 | * |
||
| 1079 | * @param string $meta_key Metadata name. Default is empty. |
||
| 1080 | * @param mixed $meta_value Optional. Metadata value. Default is empty. |
||
| 1081 | * |
||
| 1082 | * @return bool False for failure. True for success. |
||
| 1083 | */ |
||
| 1084 | public function delete_meta( $meta_key = '', $meta_value = '' ) { |
||
| 1087 | |||
| 1088 | /** |
||
| 1089 | * Sanitize the data for update/create |
||
| 1090 | * |
||
| 1091 | * @since 1.0 |
||
| 1092 | * @access private |
||
| 1093 | * |
||
| 1094 | * @param array $data The data to sanitize. |
||
| 1095 | * |
||
| 1096 | * @return array The sanitized data, based off column defaults. |
||
| 1097 | */ |
||
| 1098 | private function sanitize_columns( $data ) { |
||
| 1150 | |||
| 1151 | /** |
||
| 1152 | * Attach an email to the donor |
||
| 1153 | * |
||
| 1154 | * @since 1.7 |
||
| 1155 | * @access public |
||
| 1156 | * |
||
| 1157 | * @param string $email The email address to attach to the donor |
||
| 1158 | * @param bool $primary Allows setting the email added as the primary |
||
| 1159 | * |
||
| 1160 | * @return bool If the email was added successfully |
||
| 1161 | */ |
||
| 1162 | public function add_email( $email = '', $primary = false ) { |
||
| 1193 | |||
| 1194 | /** |
||
| 1195 | * Remove an email from the donor. |
||
| 1196 | * |
||
| 1197 | * @since 1.7 |
||
| 1198 | * @access public |
||
| 1199 | * |
||
| 1200 | * @param string $email The email address to remove from the donor. |
||
| 1201 | * |
||
| 1202 | * @return bool If the email was removed successfully. |
||
| 1203 | */ |
||
| 1204 | public function remove_email( $email = '' ) { |
||
| 1217 | |||
| 1218 | /** |
||
| 1219 | * Set an email address as the donor's primary email. |
||
| 1220 | * |
||
| 1221 | * This will move the donor's previous primary email to an additional email. |
||
| 1222 | * |
||
| 1223 | * @since 1.7 |
||
| 1224 | * @access public |
||
| 1225 | * |
||
| 1226 | * @param string $new_primary_email The email address to remove from the donor. |
||
| 1227 | * |
||
| 1228 | * @return bool If the email was set as primary successfully. |
||
| 1229 | */ |
||
| 1230 | public function set_primary_email( $new_primary_email = '' ) { |
||
| 1265 | |||
| 1266 | /** |
||
| 1267 | * Check if address valid or not. |
||
| 1268 | * |
||
| 1269 | * @since 2.0 |
||
| 1270 | * @access private |
||
| 1271 | * |
||
| 1272 | * @param $address |
||
| 1273 | * |
||
| 1274 | * @return bool |
||
| 1275 | */ |
||
| 1276 | private function is_valid_address( $address ) { |
||
| 1294 | |||
| 1295 | /** |
||
| 1296 | * Add donor address |
||
| 1297 | * |
||
| 1298 | * @since 2.0 |
||
| 1299 | * @access public |
||
| 1300 | * |
||
| 1301 | * @param string $address_type |
||
| 1302 | * @param array $address { |
||
| 1303 | * |
||
| 1304 | * @type string $address2 |
||
| 1305 | * @type string city |
||
| 1306 | * @type string zip |
||
| 1307 | * @type string state |
||
| 1308 | * @type string country |
||
| 1309 | * } |
||
| 1310 | * |
||
| 1311 | * @return bool |
||
| 1312 | */ |
||
| 1313 | public function add_address( $address_type, $address ) { |
||
| 1383 | |||
| 1384 | /** |
||
| 1385 | * Remove donor address |
||
| 1386 | * |
||
| 1387 | * @since 2.0 |
||
| 1388 | * @access public |
||
| 1389 | * @global wpdb $wpdb |
||
| 1390 | * |
||
| 1391 | * @param string $address_id |
||
| 1392 | * |
||
| 1393 | * @return bool |
||
| 1394 | */ |
||
| 1395 | public function remove_address( $address_id ) { |
||
| 1425 | |||
| 1426 | /** |
||
| 1427 | * Update donor address |
||
| 1428 | * |
||
| 1429 | * @since 2.0 |
||
| 1430 | * @access public |
||
| 1431 | * @global wpdb $wpdb |
||
| 1432 | * |
||
| 1433 | * @param string $address_id |
||
| 1434 | * @param array $address |
||
| 1435 | * |
||
| 1436 | * @return bool |
||
| 1437 | */ |
||
| 1438 | public function update_address( $address_id, $address ) { |
||
| 1477 | |||
| 1478 | |||
| 1479 | /** |
||
| 1480 | * Check if donor already has current address |
||
| 1481 | * |
||
| 1482 | * @since 2.0 |
||
| 1483 | * @access public |
||
| 1484 | * |
||
| 1485 | * @param string $current_address_type |
||
| 1486 | * @param array $current_address |
||
| 1487 | * |
||
| 1488 | * @return bool|null |
||
| 1489 | */ |
||
| 1490 | public function does_address_exist( $current_address_type, $current_address ) { |
||
| 1531 | |||
| 1532 | /** |
||
| 1533 | * Compare address. |
||
| 1534 | * |
||
| 1535 | * @since 2.0 |
||
| 1536 | * @access private |
||
| 1537 | * |
||
| 1538 | * @param array $address_1 |
||
| 1539 | * @param array $address_2 |
||
| 1540 | * |
||
| 1541 | * @return bool |
||
| 1542 | */ |
||
| 1543 | private function is_address_match( $address_1, $address_2 ) { |
||
| 1548 | |||
| 1549 | /** |
||
| 1550 | * Split donor name into first name and last name |
||
| 1551 | * |
||
| 1552 | * @param int $id Donor ID |
||
| 1553 | * |
||
| 1554 | * @since 2.0 |
||
| 1555 | * @return object |
||
| 1556 | */ |
||
| 1557 | public function split_donor_name( $id ) { |
||
| 1575 | |||
| 1576 | /** |
||
| 1577 | * Retrieves first name of donor with backward compatibility |
||
| 1578 | * |
||
| 1579 | * @since 2.0 |
||
| 1580 | * @return string |
||
| 1581 | */ |
||
| 1582 | public function get_first_name() { |
||
| 1590 | |||
| 1591 | /** |
||
| 1592 | * Retrieves last name of donor with backward compatibility |
||
| 1593 | * |
||
| 1594 | * @since 2.0 |
||
| 1595 | * @return string |
||
| 1596 | */ |
||
| 1597 | public function get_last_name() { |
||
| 1608 | |||
| 1609 | /** |
||
| 1610 | * Retrieves company name of donor |
||
| 1611 | * |
||
| 1612 | * @since 2.1 |
||
| 1613 | * |
||
| 1614 | * @return string $company_name Donor Company Name |
||
| 1615 | */ |
||
| 1616 | public function get_company_name() { |
||
| 1621 | |||
| 1622 | /** |
||
| 1623 | * Retrieves last donation for the donor. |
||
| 1624 | * |
||
| 1625 | * @since 2.1 |
||
| 1626 | * |
||
| 1627 | * @return string $company_name Donor Company Name |
||
| 1628 | */ |
||
| 1629 | public function get_last_donation() { |
||
| 1636 | |||
| 1637 | /** |
||
| 1638 | * Retrieves last donation for the donor. |
||
| 1639 | * |
||
| 1640 | * @since 2.1 |
||
| 1641 | * |
||
| 1642 | * @param bool $formatted Whether to return with the date format or not. |
||
| 1643 | * |
||
| 1644 | * @return string The date of the last donation. |
||
| 1645 | */ |
||
| 1646 | public function get_last_donation_date( $formatted = false ) { |
||
| 1663 | |||
| 1664 | /** |
||
| 1665 | * Retrieves a donor's initials (first name and last name). |
||
| 1666 | * |
||
| 1667 | * @since 2.1 |
||
| 1668 | * |
||
| 1669 | * @return string The donor's two initials (no middle). |
||
| 1670 | */ |
||
| 1671 | public function get_donor_initals() { |
||
| 1686 | |||
| 1687 | } |
||
| 1688 |