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 string |
||
| 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; |
||
| 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 bool $_id_or_email |
||
| 150 | * @param bool $by_user_id |
||
| 151 | */ |
||
| 152 | public function __construct( $_id_or_email = false, $by_user_id = false ) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Setup Donor |
||
| 183 | * |
||
| 184 | * Set donor variables. |
||
| 185 | * |
||
| 186 | * @since 1.0 |
||
| 187 | * @access private |
||
| 188 | * |
||
| 189 | * @param object $donor The Donor Object. |
||
| 190 | * |
||
| 191 | * @return bool If the setup was successful or not. |
||
| 192 | */ |
||
| 193 | 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() { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Magic __get function to dispatch a call to retrieve a private property. |
||
| 280 | * |
||
| 281 | * @since 1.0 |
||
| 282 | * @access public |
||
| 283 | * @param $key |
||
| 284 | * |
||
| 285 | * @return mixed|\WP_Error |
||
| 286 | */ |
||
| 287 | View Code Duplication | public function __get( $key ) { |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Creates a donor. |
||
| 304 | * |
||
| 305 | * @since 1.0 |
||
| 306 | * @access public |
||
| 307 | * |
||
| 308 | * @param array $data Array of attributes for a donor. |
||
| 309 | * |
||
| 310 | * @return bool|int False if not a valid creation, donor ID if user is found or valid creation. |
||
| 311 | */ |
||
| 312 | public function create( $data = array() ) { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Updates a donor record. |
||
| 372 | * |
||
| 373 | * @since 1.0 |
||
| 374 | * @access public |
||
| 375 | * |
||
| 376 | * @param array $data Array of data attributes for a donor (checked via whitelist). |
||
| 377 | * |
||
| 378 | * @return bool If the update was successful or not. |
||
| 379 | */ |
||
| 380 | public function update( $data = array() ) { |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Attach Payment |
||
| 424 | * |
||
| 425 | * Attach payment to the donor then triggers increasing stats. |
||
| 426 | * |
||
| 427 | * @since 1.0 |
||
| 428 | * @access public |
||
| 429 | * |
||
| 430 | * @param int $payment_id The payment ID to attach to the donor. |
||
| 431 | * @param bool $update_stats For backwards compatibility, if we should increase the stats or not. |
||
| 432 | * |
||
| 433 | * @return bool If the attachment was successfully. |
||
| 434 | */ |
||
| 435 | public function attach_payment( $payment_id = 0, $update_stats = true ) { |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Remove Payment |
||
| 503 | * |
||
| 504 | * Remove a payment from this donor, then triggers reducing stats. |
||
| 505 | * |
||
| 506 | * @since 1.0 |
||
| 507 | * @access public |
||
| 508 | * |
||
| 509 | * @param int $payment_id The Payment ID to remove. |
||
| 510 | * @param bool $update_stats For backwards compatibility, if we should increase the stats or not. |
||
| 511 | * |
||
| 512 | * @return boolean If the removal was successful. |
||
| 513 | */ |
||
| 514 | public function remove_payment( $payment_id = 0, $update_stats = true ) { |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Increase the donation count of a donor. |
||
| 589 | * |
||
| 590 | * @since 1.0 |
||
| 591 | * @access public |
||
| 592 | * |
||
| 593 | * @param int $count The number to increase by. |
||
| 594 | * |
||
| 595 | * @return int The donation count. |
||
| 596 | */ |
||
| 597 | public function increase_purchase_count( $count = 1 ) { |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Decrease the donor donation count. |
||
| 636 | * |
||
| 637 | * @since 1.0 |
||
| 638 | * @access public |
||
| 639 | * |
||
| 640 | * @param int $count The amount to decrease by. |
||
| 641 | * |
||
| 642 | * @return mixed If successful, the new count, otherwise false. |
||
| 643 | */ |
||
| 644 | public function decrease_donation_count( $count = 1 ) { |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Increase the donor's lifetime value. |
||
| 687 | * |
||
| 688 | * @since 1.0 |
||
| 689 | * @access public |
||
| 690 | * |
||
| 691 | * @param float $value The value to increase by. |
||
| 692 | * |
||
| 693 | * @return mixed If successful, the new value, otherwise false. |
||
| 694 | */ |
||
| 695 | public function increase_value( $value = 0.00 ) { |
||
| 726 | |||
| 727 | /** |
||
| 728 | * Decrease a donor's lifetime value. |
||
| 729 | * |
||
| 730 | * @since 1.0 |
||
| 731 | * @access public |
||
| 732 | * |
||
| 733 | * @param float $value The value to decrease by. |
||
| 734 | * |
||
| 735 | * @return mixed If successful, the new value, otherwise false. |
||
| 736 | */ |
||
| 737 | public function decrease_value( $value = 0.00 ) { |
||
| 772 | |||
| 773 | /** |
||
| 774 | * Decrease/Increase a donor's lifetime value. |
||
| 775 | * |
||
| 776 | * This function will update donation stat on basis of current amount and new amount donation difference. |
||
| 777 | * Difference value can positive or negative. Negative value will decrease user donation stat while positive value increase donation stat. |
||
| 778 | * |
||
| 779 | * @since 1.0 |
||
| 780 | * @access public |
||
| 781 | * |
||
| 782 | * @param float $curr_amount Current Donation amount. |
||
| 783 | * @param float $new_amount New (changed) Donation amount. |
||
| 784 | * |
||
| 785 | * @return mixed If successful, the new donation stat value, otherwise false. |
||
| 786 | */ |
||
| 787 | public function update_donation_value( $curr_amount, $new_amount ) { |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Get the parsed notes for a donor as an array. |
||
| 813 | * |
||
| 814 | * @since 1.0 |
||
| 815 | * @access public |
||
| 816 | * |
||
| 817 | * @param int $length The number of notes to get. |
||
| 818 | * @param int $paged What note to start at. |
||
| 819 | * |
||
| 820 | * @return array The notes requested. |
||
| 821 | */ |
||
| 822 | public function get_notes( $length = 20, $paged = 1 ) { |
||
| 835 | |||
| 836 | /** |
||
| 837 | * Get the total number of notes we have after parsing. |
||
| 838 | * |
||
| 839 | * @since 1.0 |
||
| 840 | * @access public |
||
| 841 | * |
||
| 842 | * @return int The number of notes for the donor. |
||
| 843 | */ |
||
| 844 | public function get_notes_count() { |
||
| 852 | |||
| 853 | /** |
||
| 854 | * Get the total donation amount. |
||
| 855 | * |
||
| 856 | * @since 1.8.17 |
||
| 857 | * |
||
| 858 | * @param array $args Pass any additional data. |
||
| 859 | * |
||
| 860 | * @return string|float |
||
| 861 | */ |
||
| 862 | public function get_total_donation_amount( $args = array() ) { |
||
| 875 | |||
| 876 | /** |
||
| 877 | * Add a note for the donor. |
||
| 878 | * |
||
| 879 | * @since 1.0 |
||
| 880 | * @access public |
||
| 881 | * |
||
| 882 | * @param string $note The note to add. Default is empty. |
||
| 883 | * |
||
| 884 | * @return string|boolean The new note if added successfully, false otherwise. |
||
| 885 | */ |
||
| 886 | public function add_note( $note = '' ) { |
||
| 934 | |||
| 935 | /** |
||
| 936 | * Get the notes column for the donor |
||
| 937 | * |
||
| 938 | * @since 1.0 |
||
| 939 | * @access private |
||
| 940 | * |
||
| 941 | * @return string The Notes for the donor, non-parsed. |
||
| 942 | */ |
||
| 943 | private function get_raw_notes() { |
||
| 950 | |||
| 951 | /** |
||
| 952 | * Retrieve a meta field for a donor. |
||
| 953 | * |
||
| 954 | * @since 1.6 |
||
| 955 | * @access public |
||
| 956 | * |
||
| 957 | * @param string $meta_key The meta key to retrieve. Default is empty. |
||
| 958 | * @param bool $single Whether to return a single value. Default is true. |
||
| 959 | * |
||
| 960 | * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true. |
||
| 961 | */ |
||
| 962 | public function get_meta( $meta_key = '', $single = true ) { |
||
| 965 | |||
| 966 | /** |
||
| 967 | * Add a meta data field to a donor. |
||
| 968 | * |
||
| 969 | * @since 1.6 |
||
| 970 | * @access public |
||
| 971 | * |
||
| 972 | * @param string $meta_key Metadata name. Default is empty. |
||
| 973 | * @param mixed $meta_value Metadata value. |
||
| 974 | * @param bool $unique Optional. Whether the same key should not be added. Default is false. |
||
| 975 | * |
||
| 976 | * @return bool False for failure. True for success. |
||
| 977 | */ |
||
| 978 | public function add_meta( $meta_key = '', $meta_value, $unique = false ) { |
||
| 981 | |||
| 982 | /** |
||
| 983 | * Update a meta field based on donor ID. |
||
| 984 | * |
||
| 985 | * @since 1.6 |
||
| 986 | * @access public |
||
| 987 | * |
||
| 988 | * @param string $meta_key Metadata key. Default is empty. |
||
| 989 | * @param mixed $meta_value Metadata value. |
||
| 990 | * @param mixed $prev_value Optional. Previous value to check before removing. Default is empty. |
||
| 991 | * |
||
| 992 | * @return bool False on failure, true if success. |
||
| 993 | */ |
||
| 994 | public function update_meta( $meta_key = '', $meta_value, $prev_value = '' ) { |
||
| 997 | |||
| 998 | /** |
||
| 999 | * Remove metadata matching criteria from a donor. |
||
| 1000 | * |
||
| 1001 | * @since 1.6 |
||
| 1002 | * @access public |
||
| 1003 | * |
||
| 1004 | * @param string $meta_key Metadata name. Default is empty. |
||
| 1005 | * @param mixed $meta_value Optional. Metadata value. Default is empty. |
||
| 1006 | * |
||
| 1007 | * @return bool False for failure. True for success. |
||
| 1008 | */ |
||
| 1009 | public function delete_meta( $meta_key = '', $meta_value = '' ) { |
||
| 1012 | |||
| 1013 | /** |
||
| 1014 | * Sanitize the data for update/create |
||
| 1015 | * |
||
| 1016 | * @since 1.0 |
||
| 1017 | * @access private |
||
| 1018 | * |
||
| 1019 | * @param array $data The data to sanitize. |
||
| 1020 | * |
||
| 1021 | * @return array The sanitized data, based off column defaults. |
||
| 1022 | */ |
||
| 1023 | private function sanitize_columns( $data ) { |
||
| 1075 | |||
| 1076 | /** |
||
| 1077 | * Attach an email to the donor |
||
| 1078 | * |
||
| 1079 | * @since 1.7 |
||
| 1080 | * @access public |
||
| 1081 | * |
||
| 1082 | * @param string $email The email address to attach to the donor |
||
| 1083 | * @param bool $primary Allows setting the email added as the primary |
||
| 1084 | * |
||
| 1085 | * @return bool If the email was added successfully |
||
| 1086 | */ |
||
| 1087 | public function add_email( $email = '', $primary = false ) { |
||
| 1118 | |||
| 1119 | /** |
||
| 1120 | * Remove an email from the donor. |
||
| 1121 | * |
||
| 1122 | * @since 1.7 |
||
| 1123 | * @access public |
||
| 1124 | * |
||
| 1125 | * @param string $email The email address to remove from the donor. |
||
| 1126 | * |
||
| 1127 | * @return bool If the email was removed successfully. |
||
| 1128 | */ |
||
| 1129 | public function remove_email( $email = '' ) { |
||
| 1142 | |||
| 1143 | /** |
||
| 1144 | * Set an email address as the donor's primary email. |
||
| 1145 | * |
||
| 1146 | * This will move the donor's previous primary email to an additional email. |
||
| 1147 | * |
||
| 1148 | * @since 1.7 |
||
| 1149 | * @access public |
||
| 1150 | * |
||
| 1151 | * @param string $new_primary_email The email address to remove from the donor. |
||
| 1152 | * |
||
| 1153 | * @return bool If the email was set as primary successfully. |
||
| 1154 | */ |
||
| 1155 | public function set_primary_email( $new_primary_email = '' ) { |
||
| 1190 | |||
| 1191 | /** |
||
| 1192 | * Check if address valid or not. |
||
| 1193 | * |
||
| 1194 | * @since 2.0 |
||
| 1195 | * @access private |
||
| 1196 | * |
||
| 1197 | * @param $address |
||
| 1198 | * |
||
| 1199 | * @return bool |
||
| 1200 | */ |
||
| 1201 | private function is_valid_address( $address ) { |
||
| 1219 | |||
| 1220 | /** |
||
| 1221 | * Add donor address |
||
| 1222 | * |
||
| 1223 | * @since 2.0 |
||
| 1224 | * @access public |
||
| 1225 | * |
||
| 1226 | * @param string $address_type |
||
| 1227 | * @param array $address { |
||
| 1228 | * |
||
| 1229 | * @type string $address2 |
||
| 1230 | * @type string city |
||
| 1231 | * @type string zip |
||
| 1232 | * @type string state |
||
| 1233 | * @type string country |
||
| 1234 | * } |
||
| 1235 | * |
||
| 1236 | * @return bool |
||
| 1237 | */ |
||
| 1238 | public function add_address( $address_type, $address ) { |
||
| 1322 | |||
| 1323 | /** |
||
| 1324 | * Remove donor address |
||
| 1325 | * |
||
| 1326 | * @since 2.0 |
||
| 1327 | * @access public |
||
| 1328 | * @global wpdb $wpdb |
||
| 1329 | * |
||
| 1330 | * @param string $address_id |
||
| 1331 | * |
||
| 1332 | * @return bool |
||
| 1333 | */ |
||
| 1334 | public function remove_address( $address_id ) { |
||
| 1374 | |||
| 1375 | /** |
||
| 1376 | * Update donor address |
||
| 1377 | * |
||
| 1378 | * @since 2.0 |
||
| 1379 | * @access public |
||
| 1380 | * @global wpdb $wpdb |
||
| 1381 | * |
||
| 1382 | * @param string $address_id |
||
| 1383 | * @param array $address |
||
| 1384 | * |
||
| 1385 | * @return bool |
||
| 1386 | */ |
||
| 1387 | public function update_address( $address_id, $address ) { |
||
| 1437 | |||
| 1438 | |||
| 1439 | /** |
||
| 1440 | * Check if donor already has current address |
||
| 1441 | * |
||
| 1442 | * @since 2.0 |
||
| 1443 | * @access public |
||
| 1444 | * |
||
| 1445 | * @param string $current_address_type |
||
| 1446 | * @param array $current_address |
||
| 1447 | * |
||
| 1448 | * @return bool|null |
||
| 1449 | */ |
||
| 1450 | public function is_address_exist( $current_address_type, $current_address ) { |
||
| 1491 | |||
| 1492 | /** |
||
| 1493 | * Compare address. |
||
| 1494 | * |
||
| 1495 | * @since 2.0 |
||
| 1496 | * @access private |
||
| 1497 | * |
||
| 1498 | * @param array $address_1 |
||
| 1499 | * @param array $address_2 |
||
| 1500 | * |
||
| 1501 | * @return bool |
||
| 1502 | */ |
||
| 1503 | private function is_address_match( $address_1, $address_2 ) { |
||
| 1508 | |||
| 1509 | /** |
||
| 1510 | * Split donor name into first name and last name |
||
| 1511 | * |
||
| 1512 | * @param int $id Donor ID |
||
| 1513 | * @since 2.0 |
||
| 1514 | * @return object |
||
| 1515 | */ |
||
| 1516 | public function split_donor_name( $id ) { |
||
| 1533 | |||
| 1534 | /** |
||
| 1535 | * Retrieves first name of donor with backward compatibility |
||
| 1536 | * |
||
| 1537 | * @since 2.0 |
||
| 1538 | * @return string |
||
| 1539 | */ |
||
| 1540 | public function get_first_name() { |
||
| 1548 | |||
| 1549 | /** |
||
| 1550 | * Retrieves last name of donor with backward compatibility |
||
| 1551 | * |
||
| 1552 | * @since 2.0 |
||
| 1553 | * @return string |
||
| 1554 | */ |
||
| 1555 | public function get_last_name() { |
||
| 1566 | |||
| 1567 | } |
||
| 1568 |