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() ) { |
||
421 | |||
422 | /** |
||
423 | * Updates a donor record. |
||
424 | * |
||
425 | * @since 1.0 |
||
426 | * @access public |
||
427 | * |
||
428 | * @param array $data Array of data attributes for a donor (checked via whitelist). |
||
429 | * |
||
430 | * @return bool If the update was successful or not. |
||
431 | */ |
||
432 | public function update( $data = array() ) { |
||
474 | |||
475 | /** |
||
476 | * Attach Payment |
||
477 | * |
||
478 | * Attach payment to the donor then triggers increasing stats. |
||
479 | * |
||
480 | * @since 1.0 |
||
481 | * @access public |
||
482 | * |
||
483 | * @param int $payment_id The payment ID to attach to the donor. |
||
484 | * @param bool $update_stats For backwards compatibility, if we should increase the stats or not. |
||
485 | * |
||
486 | * @return bool If the attachment was successfully. |
||
487 | */ |
||
488 | public function attach_payment( $payment_id = 0, $update_stats = true ) { |
||
553 | |||
554 | /** |
||
555 | * Remove Payment |
||
556 | * |
||
557 | * Remove a payment from this donor, then triggers reducing stats. |
||
558 | * |
||
559 | * @since 1.0 |
||
560 | * @access public |
||
561 | * |
||
562 | * @param int $payment_id The Payment ID to remove. |
||
563 | * @param bool $update_stats For backwards compatibility, if we should increase the stats or not. |
||
564 | * |
||
565 | * @return boolean If the removal was successful. |
||
566 | */ |
||
567 | public function remove_payment( $payment_id = 0, $update_stats = true ) { |
||
639 | |||
640 | /** |
||
641 | * Increase the donation count of a donor. |
||
642 | * |
||
643 | * @since 1.0 |
||
644 | * @access public |
||
645 | * |
||
646 | * @param int $count The number to increase by. |
||
647 | * |
||
648 | * @return int The donation count. |
||
649 | */ |
||
650 | public function increase_purchase_count( $count = 1 ) { |
||
686 | |||
687 | /** |
||
688 | * Decrease the donor donation count. |
||
689 | * |
||
690 | * @since 1.0 |
||
691 | * @access public |
||
692 | * |
||
693 | * @param int $count The amount to decrease by. |
||
694 | * |
||
695 | * @return mixed If successful, the new count, otherwise false. |
||
696 | */ |
||
697 | public function decrease_donation_count( $count = 1 ) { |
||
737 | |||
738 | /** |
||
739 | * Increase the donor's lifetime value. |
||
740 | * |
||
741 | * @since 1.0 |
||
742 | * @access public |
||
743 | * |
||
744 | * @param float $value The value to increase by. |
||
745 | * |
||
746 | * @return mixed If successful, the new value, otherwise false. |
||
747 | */ |
||
748 | public function increase_value( $value = 0.00 ) { |
||
779 | |||
780 | /** |
||
781 | * Decrease a donor's lifetime value. |
||
782 | * |
||
783 | * @since 1.0 |
||
784 | * @access public |
||
785 | * |
||
786 | * @param float $value The value to decrease by. |
||
787 | * |
||
788 | * @return mixed If successful, the new value, otherwise false. |
||
789 | */ |
||
790 | public function decrease_value( $value = 0.00 ) { |
||
825 | |||
826 | /** |
||
827 | * Decrease/Increase a donor's lifetime value. |
||
828 | * |
||
829 | * This function will update donation stat on basis of current amount and new amount donation difference. |
||
830 | * Difference value can positive or negative. Negative value will decrease user donation stat while positive value |
||
831 | * increase donation stat. |
||
832 | * |
||
833 | * @since 1.0 |
||
834 | * @access public |
||
835 | * |
||
836 | * @param float $curr_amount Current Donation amount. |
||
837 | * @param float $new_amount New (changed) Donation amount. |
||
838 | * |
||
839 | * @return mixed If successful, the new donation stat value, otherwise false. |
||
840 | */ |
||
841 | public function update_donation_value( $curr_amount, $new_amount ) { |
||
864 | |||
865 | /** |
||
866 | * Get the parsed notes for a donor as an array. |
||
867 | * |
||
868 | * @since 1.0 |
||
869 | * @access public |
||
870 | * |
||
871 | * @param int $length The number of notes to get. |
||
872 | * @param int $paged What note to start at. |
||
873 | * |
||
874 | * @return array The notes requested. |
||
875 | */ |
||
876 | public function get_notes( $length = 20, $paged = 1 ) { |
||
889 | |||
890 | /** |
||
891 | * Get the total number of notes we have after parsing. |
||
892 | * |
||
893 | * @since 1.0 |
||
894 | * @access public |
||
895 | * |
||
896 | * @return int The number of notes for the donor. |
||
897 | */ |
||
898 | public function get_notes_count() { |
||
906 | |||
907 | /** |
||
908 | * Get the total donation amount. |
||
909 | * |
||
910 | * @since 1.8.17 |
||
911 | * |
||
912 | * @param array $args Pass any additional data. |
||
913 | * |
||
914 | * @return string|float |
||
915 | */ |
||
916 | public function get_total_donation_amount( $args = array() ) { |
||
929 | |||
930 | /** |
||
931 | * Add a note for the donor. |
||
932 | * |
||
933 | * @since 1.0 |
||
934 | * @access public |
||
935 | * |
||
936 | * @param string $note The note to add. Default is empty. |
||
937 | * |
||
938 | * @return string|boolean The new note if added successfully, false otherwise. |
||
939 | */ |
||
940 | public function add_note( $note = '' ) { |
||
988 | |||
989 | /** |
||
990 | * Get the notes column for the donor |
||
991 | * |
||
992 | * @since 1.0 |
||
993 | * @access private |
||
994 | * |
||
995 | * @return string The Notes for the donor, non-parsed. |
||
996 | */ |
||
997 | private function get_raw_notes() { |
||
1004 | |||
1005 | /** |
||
1006 | * Retrieve a meta field for a donor. |
||
1007 | * |
||
1008 | * @since 1.6 |
||
1009 | * @access public |
||
1010 | * |
||
1011 | * @param string $meta_key The meta key to retrieve. Default is empty. |
||
1012 | * @param bool $single Whether to return a single value. Default is true. |
||
1013 | * |
||
1014 | * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is |
||
1015 | * true. |
||
1016 | */ |
||
1017 | public function get_meta( $meta_key = '', $single = true ) { |
||
1020 | |||
1021 | /** |
||
1022 | * Add a meta data field to a donor. |
||
1023 | * |
||
1024 | * @since 1.6 |
||
1025 | * @access public |
||
1026 | * |
||
1027 | * @param string $meta_key Metadata name. Default is empty. |
||
1028 | * @param mixed $meta_value Metadata value. |
||
1029 | * @param bool $unique Optional. Whether the same key should not be added. Default is false. |
||
1030 | * |
||
1031 | * @return bool False for failure. True for success. |
||
1032 | */ |
||
1033 | public function add_meta( $meta_key = '', $meta_value, $unique = false ) { |
||
1036 | |||
1037 | /** |
||
1038 | * Update a meta field based on donor ID. |
||
1039 | * |
||
1040 | * @since 1.6 |
||
1041 | * @access public |
||
1042 | * |
||
1043 | * @param string $meta_key Metadata key. Default is empty. |
||
1044 | * @param mixed $meta_value Metadata value. |
||
1045 | * @param mixed $prev_value Optional. Previous value to check before removing. Default is empty. |
||
1046 | * |
||
1047 | * @return bool False on failure, true if success. |
||
1048 | */ |
||
1049 | public function update_meta( $meta_key = '', $meta_value, $prev_value = '' ) { |
||
1052 | |||
1053 | /** |
||
1054 | * Remove metadata matching criteria from a donor. |
||
1055 | * |
||
1056 | * @since 1.6 |
||
1057 | * @access public |
||
1058 | * |
||
1059 | * @param string $meta_key Metadata name. Default is empty. |
||
1060 | * @param mixed $meta_value Optional. Metadata value. Default is empty. |
||
1061 | * |
||
1062 | * @return bool False for failure. True for success. |
||
1063 | */ |
||
1064 | public function delete_meta( $meta_key = '', $meta_value = '' ) { |
||
1067 | |||
1068 | /** |
||
1069 | * Sanitize the data for update/create |
||
1070 | * |
||
1071 | * @since 1.0 |
||
1072 | * @access private |
||
1073 | * |
||
1074 | * @param array $data The data to sanitize. |
||
1075 | * |
||
1076 | * @return array The sanitized data, based off column defaults. |
||
1077 | */ |
||
1078 | private function sanitize_columns( $data ) { |
||
1130 | |||
1131 | /** |
||
1132 | * Attach an email to the donor |
||
1133 | * |
||
1134 | * @since 1.7 |
||
1135 | * @access public |
||
1136 | * |
||
1137 | * @param string $email The email address to attach to the donor |
||
1138 | * @param bool $primary Allows setting the email added as the primary |
||
1139 | * |
||
1140 | * @return bool If the email was added successfully |
||
1141 | */ |
||
1142 | public function add_email( $email = '', $primary = false ) { |
||
1173 | |||
1174 | /** |
||
1175 | * Remove an email from the donor. |
||
1176 | * |
||
1177 | * @since 1.7 |
||
1178 | * @access public |
||
1179 | * |
||
1180 | * @param string $email The email address to remove from the donor. |
||
1181 | * |
||
1182 | * @return bool If the email was removed successfully. |
||
1183 | */ |
||
1184 | public function remove_email( $email = '' ) { |
||
1197 | |||
1198 | /** |
||
1199 | * Set an email address as the donor's primary email. |
||
1200 | * |
||
1201 | * This will move the donor's previous primary email to an additional email. |
||
1202 | * |
||
1203 | * @since 1.7 |
||
1204 | * @access public |
||
1205 | * |
||
1206 | * @param string $new_primary_email The email address to remove from the donor. |
||
1207 | * |
||
1208 | * @return bool If the email was set as primary successfully. |
||
1209 | */ |
||
1210 | public function set_primary_email( $new_primary_email = '' ) { |
||
1245 | |||
1246 | /** |
||
1247 | * Check if address valid or not. |
||
1248 | * |
||
1249 | * @since 2.0 |
||
1250 | * @access private |
||
1251 | * |
||
1252 | * @param $address |
||
1253 | * |
||
1254 | * @return bool |
||
1255 | */ |
||
1256 | private function is_valid_address( $address ) { |
||
1274 | |||
1275 | /** |
||
1276 | * Add donor address |
||
1277 | * |
||
1278 | * @since 2.0 |
||
1279 | * @access public |
||
1280 | * |
||
1281 | * @param string $address_type |
||
1282 | * @param array $address { |
||
1283 | * |
||
1284 | * @type string $address2 |
||
1285 | * @type string city |
||
1286 | * @type string zip |
||
1287 | * @type string state |
||
1288 | * @type string country |
||
1289 | * } |
||
1290 | * |
||
1291 | * @return bool |
||
1292 | */ |
||
1293 | public function add_address( $address_type, $address ) { |
||
1362 | |||
1363 | /** |
||
1364 | * Remove donor address |
||
1365 | * |
||
1366 | * @since 2.0 |
||
1367 | * @access public |
||
1368 | * @global wpdb $wpdb |
||
1369 | * |
||
1370 | * @param string $address_id |
||
1371 | * |
||
1372 | * @return bool |
||
1373 | */ |
||
1374 | public function remove_address( $address_id ) { |
||
1404 | |||
1405 | /** |
||
1406 | * Update donor address |
||
1407 | * |
||
1408 | * @since 2.0 |
||
1409 | * @access public |
||
1410 | * @global wpdb $wpdb |
||
1411 | * |
||
1412 | * @param string $address_id |
||
1413 | * @param array $address |
||
1414 | * |
||
1415 | * @return bool |
||
1416 | */ |
||
1417 | public function update_address( $address_id, $address ) { |
||
1455 | |||
1456 | |||
1457 | /** |
||
1458 | * Check if donor already has current address |
||
1459 | * |
||
1460 | * @since 2.0 |
||
1461 | * @access public |
||
1462 | * |
||
1463 | * @param string $current_address_type |
||
1464 | * @param array $current_address |
||
1465 | * |
||
1466 | * @return bool|null |
||
1467 | */ |
||
1468 | public function does_address_exist( $current_address_type, $current_address ) { |
||
1509 | |||
1510 | /** |
||
1511 | * Compare address. |
||
1512 | * |
||
1513 | * @since 2.0 |
||
1514 | * @access private |
||
1515 | * |
||
1516 | * @param array $address_1 |
||
1517 | * @param array $address_2 |
||
1518 | * |
||
1519 | * @return bool |
||
1520 | */ |
||
1521 | private function is_address_match( $address_1, $address_2 ) { |
||
1526 | |||
1527 | /** |
||
1528 | * Split donor name into first name and last name |
||
1529 | * |
||
1530 | * @param int $id Donor ID |
||
1531 | * |
||
1532 | * @since 2.0 |
||
1533 | * @return object |
||
1534 | */ |
||
1535 | public function split_donor_name( $id ) { |
||
1553 | |||
1554 | /** |
||
1555 | * Retrieves first name of donor with backward compatibility |
||
1556 | * |
||
1557 | * @since 2.0 |
||
1558 | * @return string |
||
1559 | */ |
||
1560 | public function get_first_name() { |
||
1568 | |||
1569 | /** |
||
1570 | * Retrieves last name of donor with backward compatibility |
||
1571 | * |
||
1572 | * @since 2.0 |
||
1573 | * @return string |
||
1574 | */ |
||
1575 | public function get_last_name() { |
||
1586 | |||
1587 | /** |
||
1588 | * Retrieves company name of donor |
||
1589 | * |
||
1590 | * @since 2.1 |
||
1591 | * |
||
1592 | * @return string $company_name Donor Company Name |
||
1593 | */ |
||
1594 | public function get_company_name() { |
||
1599 | |||
1600 | /** |
||
1601 | * Retrieves last donation for the donor. |
||
1602 | * |
||
1603 | * @since 2.1 |
||
1604 | * |
||
1605 | * @return string $company_name Donor Company Name |
||
1606 | */ |
||
1607 | public function get_last_donation() { |
||
1614 | |||
1615 | /** |
||
1616 | * Retrieves last donation for the donor. |
||
1617 | * |
||
1618 | * @since 2.1 |
||
1619 | * |
||
1620 | * @param bool $formatted Whether to return with the date format or not. |
||
1621 | * |
||
1622 | * @return string The date of the last donation. |
||
1623 | */ |
||
1624 | public function get_last_donation_date( $formatted = false ) { |
||
1641 | |||
1642 | /** |
||
1643 | * Retrieves a donor's initials (first name and last name). |
||
1644 | * |
||
1645 | * @since 2.1 |
||
1646 | * |
||
1647 | * @return string The donor's two initials (no middle). |
||
1648 | */ |
||
1649 | public function get_donor_initals() { |
||
1657 | |||
1658 | } |
||
1659 |