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 | protected $notes = null; |
||
| 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 ) { |
||
| 237 | |||
| 238 | |||
| 239 | /** |
||
| 240 | * Setup donor address. |
||
| 241 | * |
||
| 242 | * @since 2.0 |
||
| 243 | * @access public |
||
| 244 | */ |
||
| 245 | public function setup_address() { |
||
| 275 | |||
| 276 | |||
| 277 | /** |
||
| 278 | * Get addresses from meta cache |
||
| 279 | * |
||
| 280 | * @since 2.5.0 |
||
| 281 | * @return array |
||
| 282 | */ |
||
| 283 | private function get_addresses_from_meta_cache() { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Returns the saved address for a donor |
||
| 302 | * |
||
| 303 | * @access public |
||
| 304 | * |
||
| 305 | * @since 2.1.3 |
||
| 306 | * |
||
| 307 | * @param array $args donor address. |
||
| 308 | * |
||
| 309 | * @return array The donor's address, if any |
||
| 310 | */ |
||
| 311 | public function get_donor_address( $args = array() ) { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Magic __get function to dispatch a call to retrieve a private property. |
||
| 355 | * |
||
| 356 | * @since 1.0 |
||
| 357 | * @access public |
||
| 358 | * |
||
| 359 | * @param $key |
||
| 360 | * |
||
| 361 | * @return mixed|\WP_Error |
||
| 362 | */ |
||
| 363 | public function __get( $key ) { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Creates a donor. |
||
| 380 | * |
||
| 381 | * @since 1.0 |
||
| 382 | * @access public |
||
| 383 | * |
||
| 384 | * @param array $data Array of attributes for a donor. |
||
| 385 | * |
||
| 386 | * @return bool|int False if not a valid creation, donor ID if user is found or valid creation. |
||
| 387 | */ |
||
| 388 | public function create( $data = array() ) { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Updates a donor record. |
||
| 448 | * |
||
| 449 | * @since 1.0 |
||
| 450 | * @access public |
||
| 451 | * |
||
| 452 | * @param array $data Array of data attributes for a donor (checked via whitelist). |
||
| 453 | * |
||
| 454 | * @return bool If the update was successful or not. |
||
| 455 | */ |
||
| 456 | public function update( $data = array() ) { |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Attach Payment |
||
| 501 | * |
||
| 502 | * Attach payment to the donor then triggers increasing stats. |
||
| 503 | * |
||
| 504 | * @since 1.0 |
||
| 505 | * @access public |
||
| 506 | * |
||
| 507 | * @param int $payment_id The payment ID to attach to the donor. |
||
| 508 | * @param bool $update_stats For backwards compatibility, if we should increase the stats or not. |
||
| 509 | * |
||
| 510 | * @return bool If the attachment was successfully. |
||
| 511 | */ |
||
| 512 | public function attach_payment( $payment_id = 0, $update_stats = true ) { |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Remove Payment |
||
| 580 | * |
||
| 581 | * Remove a payment from this donor, then triggers reducing stats. |
||
| 582 | * |
||
| 583 | * @since 1.0 |
||
| 584 | * @access public |
||
| 585 | * |
||
| 586 | * @param int $payment_id The Payment ID to remove. |
||
| 587 | * @param bool $update_stats For backwards compatibility, if we should increase the stats or not. |
||
| 588 | * |
||
| 589 | * @return boolean If the removal was successful. |
||
| 590 | */ |
||
| 591 | public function remove_payment( $payment_id = 0, $update_stats = true ) { |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Increase the donation count of a donor. |
||
| 666 | * |
||
| 667 | * @since 1.0 |
||
| 668 | * @access public |
||
| 669 | * |
||
| 670 | * @param int $count The number to increase by. |
||
| 671 | * |
||
| 672 | * @return int The donation count. |
||
| 673 | */ |
||
| 674 | public function increase_purchase_count( $count = 1 ) { |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Decrease the donor donation count. |
||
| 713 | * |
||
| 714 | * @since 1.0 |
||
| 715 | * @access public |
||
| 716 | * |
||
| 717 | * @param int $count The amount to decrease by. |
||
| 718 | * |
||
| 719 | * @return mixed If successful, the new count, otherwise false. |
||
| 720 | */ |
||
| 721 | public function decrease_donation_count( $count = 1 ) { |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Increase the donor's lifetime value. |
||
| 764 | * |
||
| 765 | * @since 1.0 |
||
| 766 | * @access public |
||
| 767 | * |
||
| 768 | * @param float $value The value to increase by. |
||
| 769 | * |
||
| 770 | * @return mixed If successful, the new value, otherwise false. |
||
| 771 | */ |
||
| 772 | public function increase_value( $value = 0.00 ) { |
||
| 803 | |||
| 804 | /** |
||
| 805 | * Decrease a donor's lifetime value. |
||
| 806 | * |
||
| 807 | * @since 1.0 |
||
| 808 | * @access public |
||
| 809 | * |
||
| 810 | * @param float $value The value to decrease by. |
||
| 811 | * |
||
| 812 | * @return mixed If successful, the new value, otherwise false. |
||
| 813 | */ |
||
| 814 | public function decrease_value( $value = 0.00 ) { |
||
| 849 | |||
| 850 | /** |
||
| 851 | * Decrease/Increase a donor's lifetime value. |
||
| 852 | * |
||
| 853 | * This function will update donation stat on basis of current amount and new amount donation difference. |
||
| 854 | * Difference value can positive or negative. Negative value will decrease user donation stat while positive value |
||
| 855 | * increase donation stat. |
||
| 856 | * |
||
| 857 | * @since 1.0 |
||
| 858 | * @access public |
||
| 859 | * |
||
| 860 | * @param float $curr_amount Current Donation amount. |
||
| 861 | * @param float $new_amount New (changed) Donation amount. |
||
| 862 | * |
||
| 863 | * @return mixed If successful, the new donation stat value, otherwise false. |
||
| 864 | */ |
||
| 865 | public function update_donation_value( $curr_amount, $new_amount ) { |
||
| 888 | |||
| 889 | /** |
||
| 890 | * Get the parsed notes for a donor as an array. |
||
| 891 | * |
||
| 892 | * @since 1.0 |
||
| 893 | * @access public |
||
| 894 | * |
||
| 895 | * @param int $length The number of notes to get. |
||
| 896 | * @param int $paged What note to start at. |
||
| 897 | * |
||
| 898 | * @return array The notes requested. |
||
| 899 | */ |
||
| 900 | public function get_notes( $length = 20, $paged = 1 ) { |
||
| 913 | |||
| 914 | /** |
||
| 915 | * Get the total number of notes we have after parsing. |
||
| 916 | * |
||
| 917 | * @since 1.0 |
||
| 918 | * @access public |
||
| 919 | * |
||
| 920 | * @return int The number of notes for the donor. |
||
| 921 | */ |
||
| 922 | public function get_notes_count() { |
||
| 930 | |||
| 931 | /** |
||
| 932 | * Get the total donation amount. |
||
| 933 | * |
||
| 934 | * @since 1.8.17 |
||
| 935 | * |
||
| 936 | * @param array $args Pass any additional data. |
||
| 937 | * |
||
| 938 | * @return string|float |
||
| 939 | */ |
||
| 940 | public function get_total_donation_amount( $args = array() ) { |
||
| 953 | |||
| 954 | /** |
||
| 955 | * Add a note for the donor. |
||
| 956 | * |
||
| 957 | * @since 1.0 |
||
| 958 | * @access public |
||
| 959 | * |
||
| 960 | * @param string $note The note to add. Default is empty. |
||
| 961 | * |
||
| 962 | * @return string|boolean The new note if added successfully, false otherwise. |
||
| 963 | */ |
||
| 964 | public function add_note( $note = '' ) { |
||
| 1024 | |||
| 1025 | /** |
||
| 1026 | * Get the notes column for the donor |
||
| 1027 | * |
||
| 1028 | * @since 1.0 |
||
| 1029 | * @access private |
||
| 1030 | * |
||
| 1031 | * @return string The Notes for the donor, non-parsed. |
||
| 1032 | */ |
||
| 1033 | private function get_raw_notes() { |
||
| 1053 | |||
| 1054 | /** |
||
| 1055 | * Retrieve a meta field for a donor. |
||
| 1056 | * |
||
| 1057 | * @since 1.6 |
||
| 1058 | * @access public |
||
| 1059 | * |
||
| 1060 | * @param string $meta_key The meta key to retrieve. Default is empty. |
||
| 1061 | * @param bool $single Whether to return a single value. Default is true. |
||
| 1062 | * |
||
| 1063 | * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is |
||
| 1064 | * true. |
||
| 1065 | */ |
||
| 1066 | public function get_meta( $meta_key = '', $single = true ) { |
||
| 1069 | |||
| 1070 | /** |
||
| 1071 | * Add a meta data field to a donor. |
||
| 1072 | * |
||
| 1073 | * @since 1.6 |
||
| 1074 | * @access public |
||
| 1075 | * |
||
| 1076 | * @param string $meta_key Metadata name. Default is empty. |
||
| 1077 | * @param mixed $meta_value Metadata value. |
||
| 1078 | * @param bool $unique Optional. Whether the same key should not be added. Default is false. |
||
| 1079 | * |
||
| 1080 | * @return bool False for failure. True for success. |
||
| 1081 | */ |
||
| 1082 | public function add_meta( $meta_key = '', $meta_value, $unique = false ) { |
||
| 1085 | |||
| 1086 | /** |
||
| 1087 | * Update a meta field based on donor ID. |
||
| 1088 | * |
||
| 1089 | * @since 1.6 |
||
| 1090 | * @access public |
||
| 1091 | * |
||
| 1092 | * @param string $meta_key Metadata key. Default is empty. |
||
| 1093 | * @param mixed $meta_value Metadata value. |
||
| 1094 | * @param mixed $prev_value Optional. Previous value to check before removing. Default is empty. |
||
| 1095 | * |
||
| 1096 | * @return bool False on failure, true if success. |
||
| 1097 | */ |
||
| 1098 | public function update_meta( $meta_key = '', $meta_value, $prev_value = '' ) { |
||
| 1101 | |||
| 1102 | /** |
||
| 1103 | * Remove metadata matching criteria from a donor. |
||
| 1104 | * |
||
| 1105 | * @since 1.6 |
||
| 1106 | * @access public |
||
| 1107 | * |
||
| 1108 | * @param string $meta_key Metadata name. Default is empty. |
||
| 1109 | * @param mixed $meta_value Optional. Metadata value. Default is empty. |
||
| 1110 | * |
||
| 1111 | * @return bool False for failure. True for success. |
||
| 1112 | */ |
||
| 1113 | public function delete_meta( $meta_key = '', $meta_value = '' ) { |
||
| 1116 | |||
| 1117 | /** |
||
| 1118 | * Sanitize the data for update/create |
||
| 1119 | * |
||
| 1120 | * @since 1.0 |
||
| 1121 | * @access private |
||
| 1122 | * |
||
| 1123 | * @param array $data The data to sanitize. |
||
| 1124 | * |
||
| 1125 | * @return array The sanitized data, based off column defaults. |
||
| 1126 | */ |
||
| 1127 | private function sanitize_columns( $data ) { |
||
| 1179 | |||
| 1180 | /** |
||
| 1181 | * Attach an email to the donor |
||
| 1182 | * |
||
| 1183 | * @since 1.7 |
||
| 1184 | * @access public |
||
| 1185 | * |
||
| 1186 | * @param string $email The email address to attach to the donor |
||
| 1187 | * @param bool $primary Allows setting the email added as the primary |
||
| 1188 | * |
||
| 1189 | * @return bool If the email was added successfully |
||
| 1190 | */ |
||
| 1191 | public function add_email( $email = '', $primary = false ) { |
||
| 1222 | |||
| 1223 | /** |
||
| 1224 | * Remove an email from the donor. |
||
| 1225 | * |
||
| 1226 | * @since 1.7 |
||
| 1227 | * @access public |
||
| 1228 | * |
||
| 1229 | * @param string $email The email address to remove from the donor. |
||
| 1230 | * |
||
| 1231 | * @return bool If the email was removed successfully. |
||
| 1232 | */ |
||
| 1233 | public function remove_email( $email = '' ) { |
||
| 1246 | |||
| 1247 | /** |
||
| 1248 | * Set an email address as the donor's primary email. |
||
| 1249 | * |
||
| 1250 | * This will move the donor's previous primary email to an additional email. |
||
| 1251 | * |
||
| 1252 | * @since 1.7 |
||
| 1253 | * @access public |
||
| 1254 | * |
||
| 1255 | * @param string $new_primary_email The email address to remove from the donor. |
||
| 1256 | * |
||
| 1257 | * @return bool If the email was set as primary successfully. |
||
| 1258 | */ |
||
| 1259 | public function set_primary_email( $new_primary_email = '' ) { |
||
| 1294 | |||
| 1295 | /** |
||
| 1296 | * Check if address valid or not. |
||
| 1297 | * |
||
| 1298 | * @since 2.0 |
||
| 1299 | * @access private |
||
| 1300 | * |
||
| 1301 | * @param $address |
||
| 1302 | * |
||
| 1303 | * @return bool |
||
| 1304 | */ |
||
| 1305 | private function is_valid_address( $address ) { |
||
| 1323 | |||
| 1324 | /** |
||
| 1325 | * Add donor address |
||
| 1326 | * |
||
| 1327 | * @since 2.0 |
||
| 1328 | * @access public |
||
| 1329 | * |
||
| 1330 | * @param string $address_type |
||
| 1331 | * @param array $address { |
||
| 1332 | * |
||
| 1333 | * @type string $address2 |
||
| 1334 | * @type string city |
||
| 1335 | * @type string zip |
||
| 1336 | * @type string state |
||
| 1337 | * @type string country |
||
| 1338 | * } |
||
| 1339 | * |
||
| 1340 | * @return bool |
||
| 1341 | */ |
||
| 1342 | public function add_address( $address_type, $address ) { |
||
| 1412 | |||
| 1413 | /** |
||
| 1414 | * Remove donor address |
||
| 1415 | * |
||
| 1416 | * @since 2.0 |
||
| 1417 | * @access public |
||
| 1418 | * @global wpdb $wpdb |
||
| 1419 | * |
||
| 1420 | * @param string $address_id |
||
| 1421 | * |
||
| 1422 | * @return bool |
||
| 1423 | */ |
||
| 1424 | public function remove_address( $address_id ) { |
||
| 1459 | |||
| 1460 | /** |
||
| 1461 | * Update donor address |
||
| 1462 | * |
||
| 1463 | * @since 2.0 |
||
| 1464 | * @access public |
||
| 1465 | * @global wpdb $wpdb |
||
| 1466 | * |
||
| 1467 | * @param string $address_id |
||
| 1468 | * @param array $address |
||
| 1469 | * |
||
| 1470 | * @return bool |
||
| 1471 | */ |
||
| 1472 | public function update_address( $address_id, $address ) { |
||
| 1511 | |||
| 1512 | |||
| 1513 | /** |
||
| 1514 | * Check if donor already has current address |
||
| 1515 | * |
||
| 1516 | * @since 2.0 |
||
| 1517 | * @access public |
||
| 1518 | * |
||
| 1519 | * @param string $current_address_type |
||
| 1520 | * @param array $current_address |
||
| 1521 | * |
||
| 1522 | * @return bool|null |
||
| 1523 | */ |
||
| 1524 | public function does_address_exist( $current_address_type, $current_address ) { |
||
| 1565 | |||
| 1566 | /** |
||
| 1567 | * Compare address. |
||
| 1568 | * |
||
| 1569 | * @since 2.0 |
||
| 1570 | * @access private |
||
| 1571 | * |
||
| 1572 | * @param array $address_1 |
||
| 1573 | * @param array $address_2 |
||
| 1574 | * |
||
| 1575 | * @return bool |
||
| 1576 | */ |
||
| 1577 | private function is_address_match( $address_1, $address_2 ) { |
||
| 1582 | |||
| 1583 | /** |
||
| 1584 | * Split donor name into first name and last name |
||
| 1585 | * |
||
| 1586 | * @param int $id Donor ID |
||
| 1587 | * |
||
| 1588 | * @since 2.0 |
||
| 1589 | * @return object |
||
| 1590 | */ |
||
| 1591 | public function split_donor_name( $id ) { |
||
| 1609 | |||
| 1610 | /** |
||
| 1611 | * Retrieves first name of donor with backward compatibility |
||
| 1612 | * |
||
| 1613 | * @since 2.0 |
||
| 1614 | * @return string |
||
| 1615 | */ |
||
| 1616 | public function get_first_name() { |
||
| 1624 | |||
| 1625 | /** |
||
| 1626 | * Retrieves last name of donor with backward compatibility |
||
| 1627 | * |
||
| 1628 | * @since 2.0 |
||
| 1629 | * @return string |
||
| 1630 | */ |
||
| 1631 | public function get_last_name() { |
||
| 1642 | |||
| 1643 | /** |
||
| 1644 | * Retrieves company name of donor |
||
| 1645 | * |
||
| 1646 | * @since 2.1 |
||
| 1647 | * |
||
| 1648 | * @return string $company_name Donor Company Name |
||
| 1649 | */ |
||
| 1650 | public function get_company_name() { |
||
| 1655 | |||
| 1656 | /** |
||
| 1657 | * Retrieves last donation for the donor. |
||
| 1658 | * |
||
| 1659 | * @since 2.1 |
||
| 1660 | * |
||
| 1661 | * @return string $company_name Donor Company Name |
||
| 1662 | */ |
||
| 1663 | public function get_last_donation() { |
||
| 1670 | |||
| 1671 | /** |
||
| 1672 | * Retrieves last donation for the donor. |
||
| 1673 | * |
||
| 1674 | * @since 2.1 |
||
| 1675 | * |
||
| 1676 | * @param bool $formatted Whether to return with the date format or not. |
||
| 1677 | * |
||
| 1678 | * @return string The date of the last donation. |
||
| 1679 | */ |
||
| 1680 | public function get_last_donation_date( $formatted = false ) { |
||
| 1697 | |||
| 1698 | /** |
||
| 1699 | * Retrieves a donor's initials (first name and last name). |
||
| 1700 | * |
||
| 1701 | * @since 2.1 |
||
| 1702 | * |
||
| 1703 | * @return string The donor's two initials (no middle). |
||
| 1704 | */ |
||
| 1705 | public function get_donor_initals() { |
||
| 1720 | |||
| 1721 | } |
||
| 1722 |