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 ) { |
||
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 ) { |
||
239 | |||
240 | |||
241 | /** |
||
242 | * Setup donor address. |
||
243 | * |
||
244 | * @since 2.0 |
||
245 | * @access public |
||
246 | */ |
||
247 | public function setup_address() { |
||
280 | |||
281 | /** |
||
282 | * Returns the saved address for a donor |
||
283 | * |
||
284 | * @access public |
||
285 | * |
||
286 | * @since 2.1.3 |
||
287 | * |
||
288 | * @param array $args donor address. |
||
289 | * |
||
290 | * @return array The donor's address, if any |
||
291 | */ |
||
292 | public function get_donor_address( $args = array() ) { |
||
333 | |||
334 | /** |
||
335 | * Magic __get function to dispatch a call to retrieve a private property. |
||
336 | * |
||
337 | * @since 1.0 |
||
338 | * @access public |
||
339 | * @param $key |
||
340 | * |
||
341 | * @return mixed|\WP_Error |
||
342 | */ |
||
343 | View Code Duplication | public function __get( $key ) { |
|
357 | |||
358 | /** |
||
359 | * Creates a donor. |
||
360 | * |
||
361 | * @since 1.0 |
||
362 | * @access public |
||
363 | * |
||
364 | * @param array $data Array of attributes for a donor. |
||
365 | * |
||
366 | * @return bool|int False if not a valid creation, donor ID if user is found or valid creation. |
||
367 | */ |
||
368 | public function create( $data = array() ) { |
||
425 | |||
426 | /** |
||
427 | * Updates a donor record. |
||
428 | * |
||
429 | * @since 1.0 |
||
430 | * @access public |
||
431 | * |
||
432 | * @param array $data Array of data attributes for a donor (checked via whitelist). |
||
433 | * |
||
434 | * @return bool If the update was successful or not. |
||
435 | */ |
||
436 | public function update( $data = array() ) { |
||
478 | |||
479 | /** |
||
480 | * Attach Payment |
||
481 | * |
||
482 | * Attach payment to the donor then triggers increasing stats. |
||
483 | * |
||
484 | * @since 1.0 |
||
485 | * @access public |
||
486 | * |
||
487 | * @param int $payment_id The payment ID to attach to the donor. |
||
488 | * @param bool $update_stats For backwards compatibility, if we should increase the stats or not. |
||
489 | * |
||
490 | * @return bool If the attachment was successfully. |
||
491 | */ |
||
492 | public function attach_payment( $payment_id = 0, $update_stats = true ) { |
||
557 | |||
558 | /** |
||
559 | * Remove Payment |
||
560 | * |
||
561 | * Remove a payment from this donor, then triggers reducing stats. |
||
562 | * |
||
563 | * @since 1.0 |
||
564 | * @access public |
||
565 | * |
||
566 | * @param int $payment_id The Payment ID to remove. |
||
567 | * @param bool $update_stats For backwards compatibility, if we should increase the stats or not. |
||
568 | * |
||
569 | * @return boolean If the removal was successful. |
||
570 | */ |
||
571 | public function remove_payment( $payment_id = 0, $update_stats = true ) { |
||
643 | |||
644 | /** |
||
645 | * Increase the donation count of a donor. |
||
646 | * |
||
647 | * @since 1.0 |
||
648 | * @access public |
||
649 | * |
||
650 | * @param int $count The number to increase by. |
||
651 | * |
||
652 | * @return int The donation count. |
||
653 | */ |
||
654 | public function increase_purchase_count( $count = 1 ) { |
||
690 | |||
691 | /** |
||
692 | * Decrease the donor donation count. |
||
693 | * |
||
694 | * @since 1.0 |
||
695 | * @access public |
||
696 | * |
||
697 | * @param int $count The amount to decrease by. |
||
698 | * |
||
699 | * @return mixed If successful, the new count, otherwise false. |
||
700 | */ |
||
701 | public function decrease_donation_count( $count = 1 ) { |
||
741 | |||
742 | /** |
||
743 | * Increase the donor's lifetime value. |
||
744 | * |
||
745 | * @since 1.0 |
||
746 | * @access public |
||
747 | * |
||
748 | * @param float $value The value to increase by. |
||
749 | * |
||
750 | * @return mixed If successful, the new value, otherwise false. |
||
751 | */ |
||
752 | public function increase_value( $value = 0.00 ) { |
||
783 | |||
784 | /** |
||
785 | * Decrease a donor's lifetime value. |
||
786 | * |
||
787 | * @since 1.0 |
||
788 | * @access public |
||
789 | * |
||
790 | * @param float $value The value to decrease by. |
||
791 | * |
||
792 | * @return mixed If successful, the new value, otherwise false. |
||
793 | */ |
||
794 | public function decrease_value( $value = 0.00 ) { |
||
829 | |||
830 | /** |
||
831 | * Decrease/Increase a donor's lifetime value. |
||
832 | * |
||
833 | * This function will update donation stat on basis of current amount and new amount donation difference. |
||
834 | * Difference value can positive or negative. Negative value will decrease user donation stat while positive value increase donation stat. |
||
835 | * |
||
836 | * @since 1.0 |
||
837 | * @access public |
||
838 | * |
||
839 | * @param float $curr_amount Current Donation amount. |
||
840 | * @param float $new_amount New (changed) Donation amount. |
||
841 | * |
||
842 | * @return mixed If successful, the new donation stat value, otherwise false. |
||
843 | */ |
||
844 | public function update_donation_value( $curr_amount, $new_amount ) { |
||
867 | |||
868 | /** |
||
869 | * Get the parsed notes for a donor as an array. |
||
870 | * |
||
871 | * @since 1.0 |
||
872 | * @access public |
||
873 | * |
||
874 | * @param int $length The number of notes to get. |
||
875 | * @param int $paged What note to start at. |
||
876 | * |
||
877 | * @return array The notes requested. |
||
878 | */ |
||
879 | public function get_notes( $length = 20, $paged = 1 ) { |
||
892 | |||
893 | /** |
||
894 | * Get the total number of notes we have after parsing. |
||
895 | * |
||
896 | * @since 1.0 |
||
897 | * @access public |
||
898 | * |
||
899 | * @return int The number of notes for the donor. |
||
900 | */ |
||
901 | public function get_notes_count() { |
||
909 | |||
910 | /** |
||
911 | * Get the total donation amount. |
||
912 | * |
||
913 | * @since 1.8.17 |
||
914 | * |
||
915 | * @param array $args Pass any additional data. |
||
916 | * |
||
917 | * @return string|float |
||
918 | */ |
||
919 | public function get_total_donation_amount( $args = array() ) { |
||
932 | |||
933 | /** |
||
934 | * Add a note for the donor. |
||
935 | * |
||
936 | * @since 1.0 |
||
937 | * @access public |
||
938 | * |
||
939 | * @param string $note The note to add. Default is empty. |
||
940 | * |
||
941 | * @return string|boolean The new note if added successfully, false otherwise. |
||
942 | */ |
||
943 | public function add_note( $note = '' ) { |
||
991 | |||
992 | /** |
||
993 | * Get the notes column for the donor |
||
994 | * |
||
995 | * @since 1.0 |
||
996 | * @access private |
||
997 | * |
||
998 | * @return string The Notes for the donor, non-parsed. |
||
999 | */ |
||
1000 | private function get_raw_notes() { |
||
1007 | |||
1008 | /** |
||
1009 | * Retrieve a meta field for a donor. |
||
1010 | * |
||
1011 | * @since 1.6 |
||
1012 | * @access public |
||
1013 | * |
||
1014 | * @param string $meta_key The meta key to retrieve. Default is empty. |
||
1015 | * @param bool $single Whether to return a single value. Default is true. |
||
1016 | * |
||
1017 | * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true. |
||
1018 | */ |
||
1019 | public function get_meta( $meta_key = '', $single = true ) { |
||
1022 | |||
1023 | /** |
||
1024 | * Add a meta data field to a donor. |
||
1025 | * |
||
1026 | * @since 1.6 |
||
1027 | * @access public |
||
1028 | * |
||
1029 | * @param string $meta_key Metadata name. Default is empty. |
||
1030 | * @param mixed $meta_value Metadata value. |
||
1031 | * @param bool $unique Optional. Whether the same key should not be added. Default is false. |
||
1032 | * |
||
1033 | * @return bool False for failure. True for success. |
||
1034 | */ |
||
1035 | public function add_meta( $meta_key = '', $meta_value, $unique = false ) { |
||
1038 | |||
1039 | /** |
||
1040 | * Update a meta field based on donor ID. |
||
1041 | * |
||
1042 | * @since 1.6 |
||
1043 | * @access public |
||
1044 | * |
||
1045 | * @param string $meta_key Metadata key. Default is empty. |
||
1046 | * @param mixed $meta_value Metadata value. |
||
1047 | * @param mixed $prev_value Optional. Previous value to check before removing. Default is empty. |
||
1048 | * |
||
1049 | * @return bool False on failure, true if success. |
||
1050 | */ |
||
1051 | public function update_meta( $meta_key = '', $meta_value, $prev_value = '' ) { |
||
1054 | |||
1055 | /** |
||
1056 | * Remove metadata matching criteria from a donor. |
||
1057 | * |
||
1058 | * @since 1.6 |
||
1059 | * @access public |
||
1060 | * |
||
1061 | * @param string $meta_key Metadata name. Default is empty. |
||
1062 | * @param mixed $meta_value Optional. Metadata value. Default is empty. |
||
1063 | * |
||
1064 | * @return bool False for failure. True for success. |
||
1065 | */ |
||
1066 | public function delete_meta( $meta_key = '', $meta_value = '' ) { |
||
1069 | |||
1070 | /** |
||
1071 | * Sanitize the data for update/create |
||
1072 | * |
||
1073 | * @since 1.0 |
||
1074 | * @access private |
||
1075 | * |
||
1076 | * @param array $data The data to sanitize. |
||
1077 | * |
||
1078 | * @return array The sanitized data, based off column defaults. |
||
1079 | */ |
||
1080 | private function sanitize_columns( $data ) { |
||
1132 | |||
1133 | /** |
||
1134 | * Attach an email to the donor |
||
1135 | * |
||
1136 | * @since 1.7 |
||
1137 | * @access public |
||
1138 | * |
||
1139 | * @param string $email The email address to attach to the donor |
||
1140 | * @param bool $primary Allows setting the email added as the primary |
||
1141 | * |
||
1142 | * @return bool If the email was added successfully |
||
1143 | */ |
||
1144 | public function add_email( $email = '', $primary = false ) { |
||
1175 | |||
1176 | /** |
||
1177 | * Remove an email from the donor. |
||
1178 | * |
||
1179 | * @since 1.7 |
||
1180 | * @access public |
||
1181 | * |
||
1182 | * @param string $email The email address to remove from the donor. |
||
1183 | * |
||
1184 | * @return bool If the email was removed successfully. |
||
1185 | */ |
||
1186 | public function remove_email( $email = '' ) { |
||
1199 | |||
1200 | /** |
||
1201 | * Set an email address as the donor's primary email. |
||
1202 | * |
||
1203 | * This will move the donor's previous primary email to an additional email. |
||
1204 | * |
||
1205 | * @since 1.7 |
||
1206 | * @access public |
||
1207 | * |
||
1208 | * @param string $new_primary_email The email address to remove from the donor. |
||
1209 | * |
||
1210 | * @return bool If the email was set as primary successfully. |
||
1211 | */ |
||
1212 | public function set_primary_email( $new_primary_email = '' ) { |
||
1247 | |||
1248 | /** |
||
1249 | * Check if address valid or not. |
||
1250 | * |
||
1251 | * @since 2.0 |
||
1252 | * @access private |
||
1253 | * |
||
1254 | * @param $address |
||
1255 | * |
||
1256 | * @return bool |
||
1257 | */ |
||
1258 | private function is_valid_address( $address ) { |
||
1276 | |||
1277 | /** |
||
1278 | * Add donor address |
||
1279 | * |
||
1280 | * @since 2.0 |
||
1281 | * @access public |
||
1282 | * |
||
1283 | * @param string $address_type |
||
1284 | * @param array $address { |
||
1285 | * |
||
1286 | * @type string $address2 |
||
1287 | * @type string city |
||
1288 | * @type string zip |
||
1289 | * @type string state |
||
1290 | * @type string country |
||
1291 | * } |
||
1292 | * |
||
1293 | * @return bool |
||
1294 | */ |
||
1295 | public function add_address( $address_type, $address ) { |
||
1379 | |||
1380 | /** |
||
1381 | * Remove donor address |
||
1382 | * |
||
1383 | * @since 2.0 |
||
1384 | * @access public |
||
1385 | * @global wpdb $wpdb |
||
1386 | * |
||
1387 | * @param string $address_id |
||
1388 | * |
||
1389 | * @return bool |
||
1390 | */ |
||
1391 | public function remove_address( $address_id ) { |
||
1431 | |||
1432 | /** |
||
1433 | * Update donor address |
||
1434 | * |
||
1435 | * @since 2.0 |
||
1436 | * @access public |
||
1437 | * @global wpdb $wpdb |
||
1438 | * |
||
1439 | * @param string $address_id |
||
1440 | * @param array $address |
||
1441 | * |
||
1442 | * @return bool |
||
1443 | */ |
||
1444 | public function update_address( $address_id, $address ) { |
||
1492 | |||
1493 | |||
1494 | /** |
||
1495 | * Check if donor already has current address |
||
1496 | * |
||
1497 | * @since 2.0 |
||
1498 | * @access public |
||
1499 | * |
||
1500 | * @param string $current_address_type |
||
1501 | * @param array $current_address |
||
1502 | * |
||
1503 | * @return bool|null |
||
1504 | */ |
||
1505 | public function is_address_exist( $current_address_type, $current_address ) { |
||
1546 | |||
1547 | /** |
||
1548 | * Compare address. |
||
1549 | * |
||
1550 | * @since 2.0 |
||
1551 | * @access private |
||
1552 | * |
||
1553 | * @param array $address_1 |
||
1554 | * @param array $address_2 |
||
1555 | * |
||
1556 | * @return bool |
||
1557 | */ |
||
1558 | private function is_address_match( $address_1, $address_2 ) { |
||
1563 | |||
1564 | /** |
||
1565 | * Split donor name into first name and last name |
||
1566 | * |
||
1567 | * @param int $id Donor ID |
||
1568 | * @since 2.0 |
||
1569 | * @return object |
||
1570 | */ |
||
1571 | public function split_donor_name( $id ) { |
||
1588 | |||
1589 | /** |
||
1590 | * Retrieves first name of donor with backward compatibility |
||
1591 | * |
||
1592 | * @since 2.0 |
||
1593 | * @return string |
||
1594 | */ |
||
1595 | public function get_first_name() { |
||
1603 | |||
1604 | /** |
||
1605 | * Retrieves last name of donor with backward compatibility |
||
1606 | * |
||
1607 | * @since 2.0 |
||
1608 | * @return string |
||
1609 | */ |
||
1610 | public function get_last_name() { |
||
1621 | |||
1622 | /** |
||
1623 | * Retrieves company name of donor |
||
1624 | * |
||
1625 | * @since 2.1.0 |
||
1626 | * |
||
1627 | * @return string $company_name Donor Company Name |
||
1628 | */ |
||
1629 | public function get_company_name() { |
||
1634 | } |
||
1635 |