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 ) { |
||
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * Setup donor address. |
||
| 232 | * |
||
| 233 | * @since 2.0 |
||
| 234 | * @access public |
||
| 235 | */ |
||
| 236 | public function setup_address() { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Magic __get function to dispatch a call to retrieve a private property. |
||
| 278 | * |
||
| 279 | * @since 1.0 |
||
| 280 | * @access public |
||
| 281 | * @param $key |
||
| 282 | * |
||
| 283 | * @return mixed|\WP_Error |
||
| 284 | */ |
||
| 285 | View Code Duplication | public function __get( $key ) { |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Creates a donor. |
||
| 302 | * |
||
| 303 | * @since 1.0 |
||
| 304 | * @access public |
||
| 305 | * |
||
| 306 | * @param array $data Array of attributes for a donor. |
||
| 307 | * |
||
| 308 | * @return bool|int False if not a valid creation, donor ID if user is found or valid creation. |
||
| 309 | */ |
||
| 310 | public function create( $data = array() ) { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Updates a donor record. |
||
| 370 | * |
||
| 371 | * @since 1.0 |
||
| 372 | * @access public |
||
| 373 | * |
||
| 374 | * @param array $data Array of data attributes for a donor (checked via whitelist). |
||
| 375 | * |
||
| 376 | * @return bool If the update was successful or not. |
||
| 377 | */ |
||
| 378 | public function update( $data = array() ) { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Attach Payment |
||
| 422 | * |
||
| 423 | * Attach payment to the donor then triggers increasing stats. |
||
| 424 | * |
||
| 425 | * @since 1.0 |
||
| 426 | * @access public |
||
| 427 | * |
||
| 428 | * @param int $payment_id The payment ID to attach to the donor. |
||
| 429 | * @param bool $update_stats For backwards compatibility, if we should increase the stats or not. |
||
| 430 | * |
||
| 431 | * @return bool If the attachment was successfully. |
||
| 432 | */ |
||
| 433 | public function attach_payment( $payment_id = 0, $update_stats = true ) { |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Remove Payment |
||
| 501 | * |
||
| 502 | * Remove a payment from this donor, then triggers reducing stats. |
||
| 503 | * |
||
| 504 | * @since 1.0 |
||
| 505 | * @access public |
||
| 506 | * |
||
| 507 | * @param int $payment_id The Payment ID to remove. |
||
| 508 | * @param bool $update_stats For backwards compatibility, if we should increase the stats or not. |
||
| 509 | * |
||
| 510 | * @return boolean If the removal was successful. |
||
| 511 | */ |
||
| 512 | public function remove_payment( $payment_id = 0, $update_stats = true ) { |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Increase the donation count of a donor. |
||
| 587 | * |
||
| 588 | * @since 1.0 |
||
| 589 | * @access public |
||
| 590 | * |
||
| 591 | * @param int $count The number to increase by. |
||
| 592 | * |
||
| 593 | * @return int The donation count. |
||
| 594 | */ |
||
| 595 | public function increase_purchase_count( $count = 1 ) { |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Decrease the donor donation count. |
||
| 634 | * |
||
| 635 | * @since 1.0 |
||
| 636 | * @access public |
||
| 637 | * |
||
| 638 | * @param int $count The amount to decrease by. |
||
| 639 | * |
||
| 640 | * @return mixed If successful, the new count, otherwise false. |
||
| 641 | */ |
||
| 642 | public function decrease_donation_count( $count = 1 ) { |
||
| 682 | |||
| 683 | /** |
||
| 684 | * Increase the donor's lifetime value. |
||
| 685 | * |
||
| 686 | * @since 1.0 |
||
| 687 | * @access public |
||
| 688 | * |
||
| 689 | * @param float $value The value to increase by. |
||
| 690 | * |
||
| 691 | * @return mixed If successful, the new value, otherwise false. |
||
| 692 | */ |
||
| 693 | public function increase_value( $value = 0.00 ) { |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Decrease a donor's lifetime value. |
||
| 727 | * |
||
| 728 | * @since 1.0 |
||
| 729 | * @access public |
||
| 730 | * |
||
| 731 | * @param float $value The value to decrease by. |
||
| 732 | * |
||
| 733 | * @return mixed If successful, the new value, otherwise false. |
||
| 734 | */ |
||
| 735 | public function decrease_value( $value = 0.00 ) { |
||
| 770 | |||
| 771 | /** |
||
| 772 | * Decrease/Increase a donor's lifetime value. |
||
| 773 | * |
||
| 774 | * This function will update donation stat on basis of current amount and new amount donation difference. |
||
| 775 | * Difference value can positive or negative. Negative value will decrease user donation stat while positive value increase donation stat. |
||
| 776 | * |
||
| 777 | * @since 1.0 |
||
| 778 | * @access public |
||
| 779 | * |
||
| 780 | * @param float $curr_amount Current Donation amount. |
||
| 781 | * @param float $new_amount New (changed) Donation amount. |
||
| 782 | * |
||
| 783 | * @return mixed If successful, the new donation stat value, otherwise false. |
||
| 784 | */ |
||
| 785 | public function update_donation_value( $curr_amount, $new_amount ) { |
||
| 808 | |||
| 809 | /** |
||
| 810 | * Get the parsed notes for a donor as an array. |
||
| 811 | * |
||
| 812 | * @since 1.0 |
||
| 813 | * @access public |
||
| 814 | * |
||
| 815 | * @param int $length The number of notes to get. |
||
| 816 | * @param int $paged What note to start at. |
||
| 817 | * |
||
| 818 | * @return array The notes requested. |
||
| 819 | */ |
||
| 820 | public function get_notes( $length = 20, $paged = 1 ) { |
||
| 833 | |||
| 834 | /** |
||
| 835 | * Get the total number of notes we have after parsing. |
||
| 836 | * |
||
| 837 | * @since 1.0 |
||
| 838 | * @access public |
||
| 839 | * |
||
| 840 | * @return int The number of notes for the donor. |
||
| 841 | */ |
||
| 842 | public function get_notes_count() { |
||
| 850 | |||
| 851 | /** |
||
| 852 | * Add a note for the donor. |
||
| 853 | * |
||
| 854 | * @since 1.0 |
||
| 855 | * @access public |
||
| 856 | * |
||
| 857 | * @param string $note The note to add. Default is empty. |
||
| 858 | * |
||
| 859 | * @return string|boolean The new note if added successfully, false otherwise. |
||
| 860 | */ |
||
| 861 | public function add_note( $note = '' ) { |
||
| 909 | |||
| 910 | /** |
||
| 911 | * Get the notes column for the donor |
||
| 912 | * |
||
| 913 | * @since 1.0 |
||
| 914 | * @access private |
||
| 915 | * |
||
| 916 | * @return string The Notes for the donor, non-parsed. |
||
| 917 | */ |
||
| 918 | private function get_raw_notes() { |
||
| 925 | |||
| 926 | /** |
||
| 927 | * Retrieve a meta field for a donor. |
||
| 928 | * |
||
| 929 | * @since 1.6 |
||
| 930 | * @access public |
||
| 931 | * |
||
| 932 | * @param string $meta_key The meta key to retrieve. Default is empty. |
||
| 933 | * @param bool $single Whether to return a single value. Default is true. |
||
| 934 | * |
||
| 935 | * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true. |
||
| 936 | */ |
||
| 937 | public function get_meta( $meta_key = '', $single = true ) { |
||
| 940 | |||
| 941 | /** |
||
| 942 | * Add a meta data field to a donor. |
||
| 943 | * |
||
| 944 | * @since 1.6 |
||
| 945 | * @access public |
||
| 946 | * |
||
| 947 | * @param string $meta_key Metadata name. Default is empty. |
||
| 948 | * @param mixed $meta_value Metadata value. |
||
| 949 | * @param bool $unique Optional. Whether the same key should not be added. Default is false. |
||
| 950 | * |
||
| 951 | * @return bool False for failure. True for success. |
||
| 952 | */ |
||
| 953 | public function add_meta( $meta_key = '', $meta_value, $unique = false ) { |
||
| 956 | |||
| 957 | /** |
||
| 958 | * Update a meta field based on donor ID. |
||
| 959 | * |
||
| 960 | * @since 1.6 |
||
| 961 | * @access public |
||
| 962 | * |
||
| 963 | * @param string $meta_key Metadata key. Default is empty. |
||
| 964 | * @param mixed $meta_value Metadata value. |
||
| 965 | * @param mixed $prev_value Optional. Previous value to check before removing. Default is empty. |
||
| 966 | * |
||
| 967 | * @return bool False on failure, true if success. |
||
| 968 | */ |
||
| 969 | public function update_meta( $meta_key = '', $meta_value, $prev_value = '' ) { |
||
| 972 | |||
| 973 | /** |
||
| 974 | * Remove metadata matching criteria from a donor. |
||
| 975 | * |
||
| 976 | * @since 1.6 |
||
| 977 | * @access public |
||
| 978 | * |
||
| 979 | * @param string $meta_key Metadata name. Default is empty. |
||
| 980 | * @param mixed $meta_value Optional. Metadata value. Default is empty. |
||
| 981 | * |
||
| 982 | * @return bool False for failure. True for success. |
||
| 983 | */ |
||
| 984 | public function delete_meta( $meta_key = '', $meta_value = '' ) { |
||
| 987 | |||
| 988 | /** |
||
| 989 | * Sanitize the data for update/create |
||
| 990 | * |
||
| 991 | * @since 1.0 |
||
| 992 | * @access private |
||
| 993 | * |
||
| 994 | * @param array $data The data to sanitize. |
||
| 995 | * |
||
| 996 | * @return array The sanitized data, based off column defaults. |
||
| 997 | */ |
||
| 998 | private function sanitize_columns( $data ) { |
||
| 1050 | |||
| 1051 | /** |
||
| 1052 | * Attach an email to the donor |
||
| 1053 | * |
||
| 1054 | * @since 1.7 |
||
| 1055 | * @access public |
||
| 1056 | * |
||
| 1057 | * @param string $email The email address to attach to the donor |
||
| 1058 | * @param bool $primary Allows setting the email added as the primary |
||
| 1059 | * |
||
| 1060 | * @return bool If the email was added successfully |
||
| 1061 | */ |
||
| 1062 | public function add_email( $email = '', $primary = false ) { |
||
| 1093 | |||
| 1094 | /** |
||
| 1095 | * Remove an email from the donor. |
||
| 1096 | * |
||
| 1097 | * @since 1.7 |
||
| 1098 | * @access public |
||
| 1099 | * |
||
| 1100 | * @param string $email The email address to remove from the donor. |
||
| 1101 | * |
||
| 1102 | * @return bool If the email was removed successfully. |
||
| 1103 | */ |
||
| 1104 | public function remove_email( $email = '' ) { |
||
| 1117 | |||
| 1118 | /** |
||
| 1119 | * Set an email address as the donor's primary email. |
||
| 1120 | * |
||
| 1121 | * This will move the donor's previous primary email to an additional email. |
||
| 1122 | * |
||
| 1123 | * @since 1.7 |
||
| 1124 | * @access public |
||
| 1125 | * |
||
| 1126 | * @param string $new_primary_email The email address to remove from the donor. |
||
| 1127 | * |
||
| 1128 | * @return bool If the email was set as primary successfully. |
||
| 1129 | */ |
||
| 1130 | public function set_primary_email( $new_primary_email = '' ) { |
||
| 1165 | |||
| 1166 | /** |
||
| 1167 | * Add donor address |
||
| 1168 | * |
||
| 1169 | * @since 2.0 |
||
| 1170 | * @access public |
||
| 1171 | * |
||
| 1172 | * @param string $address_type |
||
| 1173 | * @param array $address { |
||
| 1174 | * |
||
| 1175 | * @type string $address2 |
||
| 1176 | * @type string city |
||
| 1177 | * @type string zip |
||
| 1178 | * @type string state |
||
| 1179 | * @type string country |
||
| 1180 | * } |
||
| 1181 | * |
||
| 1182 | * @return bool |
||
| 1183 | */ |
||
| 1184 | public function add_address( $address_type, $address ) { |
||
| 1283 | |||
| 1284 | /** |
||
| 1285 | * Remove donor address |
||
| 1286 | * |
||
| 1287 | * @since 2.0 |
||
| 1288 | * @access public |
||
| 1289 | * @global wpdb $wpdb |
||
| 1290 | * |
||
| 1291 | * @param string $address_id |
||
| 1292 | * |
||
| 1293 | * @return bool |
||
| 1294 | */ |
||
| 1295 | public function remove_address( $address_id ) { |
||
| 1335 | |||
| 1336 | /** |
||
| 1337 | * Update donor address |
||
| 1338 | * |
||
| 1339 | * @since 2.0 |
||
| 1340 | * @access public |
||
| 1341 | * @global wpdb $wpdb |
||
| 1342 | * |
||
| 1343 | * @param string $address_id |
||
| 1344 | * @param array $address |
||
| 1345 | * |
||
| 1346 | * @return bool |
||
| 1347 | */ |
||
| 1348 | public function update_address( $address_id, $address ) { |
||
| 1398 | |||
| 1399 | |||
| 1400 | /** |
||
| 1401 | * Check if donor already has current address |
||
| 1402 | * |
||
| 1403 | * @since 2.0 |
||
| 1404 | * @access public |
||
| 1405 | * |
||
| 1406 | * @param string $current_address_type |
||
| 1407 | * @param array $current_address |
||
| 1408 | * |
||
| 1409 | * @return bool|null |
||
| 1410 | */ |
||
| 1411 | public function is_address_exist( $current_address_type, $current_address ) { |
||
| 1458 | |||
| 1459 | /** |
||
| 1460 | * Split donor name into first name and last name |
||
| 1461 | * |
||
| 1462 | * @param int $id Donor ID |
||
| 1463 | * @since 2.0 |
||
| 1464 | * @return object |
||
| 1465 | */ |
||
| 1466 | public function split_donor_name( $id ) { |
||
| 1483 | |||
| 1484 | /** |
||
| 1485 | * Retrieves first name of donor with backward compatibility |
||
| 1486 | * |
||
| 1487 | * @since 2.0 |
||
| 1488 | * @return string |
||
| 1489 | */ |
||
| 1490 | public function get_first_name() { |
||
| 1498 | |||
| 1499 | /** |
||
| 1500 | * Retrieves last name of donor with backward compatibility |
||
| 1501 | * |
||
| 1502 | * @since 2.0 |
||
| 1503 | * @return string |
||
| 1504 | */ |
||
| 1505 | public function get_last_name() { |
||
| 1516 | |||
| 1517 | } |
||
| 1518 |