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_Donate_Form 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_Donate_Form, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class Give_Donate_Form { |
||
34 | |||
35 | /** |
||
36 | * The donation ID. |
||
37 | * |
||
38 | * @since 1.0 |
||
39 | * @access public |
||
40 | * |
||
41 | * @var int |
||
42 | */ |
||
43 | public $ID = 0; |
||
44 | |||
45 | /** |
||
46 | * The donation price. |
||
47 | * |
||
48 | * @since 1.0 |
||
49 | * @access private |
||
50 | * |
||
51 | * @var float |
||
52 | */ |
||
53 | private $price; |
||
54 | |||
55 | /** |
||
56 | * The minimum donation price. |
||
57 | * |
||
58 | * @since 1.3.6 |
||
59 | * @access private |
||
60 | * |
||
61 | * @var float |
||
62 | */ |
||
63 | private $minimum_price; |
||
64 | |||
65 | /** |
||
66 | * The maximum donation price. |
||
67 | * |
||
68 | * @since 2.0 |
||
69 | * @access private |
||
70 | * |
||
71 | * @var float |
||
72 | */ |
||
73 | private $maximum_price; |
||
74 | |||
75 | /** |
||
76 | * The donation prices, if Price Levels are enabled. |
||
77 | * |
||
78 | * @since 1.0 |
||
79 | * @access private |
||
80 | * |
||
81 | * @var array |
||
82 | */ |
||
83 | private $prices; |
||
84 | |||
85 | /** |
||
86 | * The donation goal. |
||
87 | * |
||
88 | * @since 1.0 |
||
89 | * @access private |
||
90 | * |
||
91 | * @var float |
||
92 | */ |
||
93 | private $goal; |
||
94 | |||
95 | /** |
||
96 | * The form's sale count. |
||
97 | * |
||
98 | * @since 1.0 |
||
99 | * @access private |
||
100 | * |
||
101 | * @var int |
||
102 | */ |
||
103 | private $sales; |
||
104 | |||
105 | /** |
||
106 | * The form's total earnings |
||
107 | * |
||
108 | * @since 1.0 |
||
109 | * @access private |
||
110 | * |
||
111 | * @var float |
||
112 | */ |
||
113 | private $earnings; |
||
114 | |||
115 | /** |
||
116 | * Declare the default properties in WP_Post as we can't extend it |
||
117 | * Anything we've declared above has been removed. |
||
118 | */ |
||
119 | |||
120 | /** |
||
121 | * The post author |
||
122 | * |
||
123 | * @since 1.0 |
||
124 | * @access public |
||
125 | * |
||
126 | * @var int |
||
127 | */ |
||
128 | public $post_author = 0; |
||
129 | |||
130 | /** |
||
131 | * The post date |
||
132 | * |
||
133 | * @since 1.0 |
||
134 | * @access public |
||
135 | * |
||
136 | * @var string |
||
137 | */ |
||
138 | public $post_date = '0000-00-00 00:00:00'; |
||
139 | |||
140 | /** |
||
141 | * The post GTM date |
||
142 | * |
||
143 | * @since 1.0 |
||
144 | * @access public |
||
145 | * |
||
146 | * @var string |
||
147 | */ |
||
148 | public $post_date_gmt = '0000-00-00 00:00:00'; |
||
149 | |||
150 | /** |
||
151 | * The post content |
||
152 | * |
||
153 | * @since 1.0 |
||
154 | * @access public |
||
155 | * |
||
156 | * @var string |
||
157 | */ |
||
158 | public $post_content = ''; |
||
159 | |||
160 | /** |
||
161 | * The post title |
||
162 | * |
||
163 | * @since 1.0 |
||
164 | * @access public |
||
165 | * |
||
166 | * @var string |
||
167 | */ |
||
168 | public $post_title = ''; |
||
169 | |||
170 | /** |
||
171 | * The post excerpt |
||
172 | * |
||
173 | * @since 1.0 |
||
174 | * @access public |
||
175 | * |
||
176 | * @var string |
||
177 | */ |
||
178 | public $post_excerpt = ''; |
||
179 | |||
180 | /** |
||
181 | * The post status |
||
182 | * |
||
183 | * @since 1.0 |
||
184 | * @access public |
||
185 | * |
||
186 | * @var string |
||
187 | */ |
||
188 | public $post_status = 'publish'; |
||
189 | |||
190 | /** |
||
191 | * The comment status |
||
192 | * |
||
193 | * @since 1.0 |
||
194 | * @access public |
||
195 | * |
||
196 | * @var string |
||
197 | */ |
||
198 | public $comment_status = 'open'; |
||
199 | |||
200 | /** |
||
201 | * The ping status |
||
202 | * |
||
203 | * @since 1.0 |
||
204 | * @access public |
||
205 | * |
||
206 | * @var string |
||
207 | */ |
||
208 | public $ping_status = 'open'; |
||
209 | |||
210 | /** |
||
211 | * The post password |
||
212 | * |
||
213 | * @since 1.0 |
||
214 | * @access public |
||
215 | * |
||
216 | * @var string |
||
217 | */ |
||
218 | public $post_password = ''; |
||
219 | |||
220 | /** |
||
221 | * The post name |
||
222 | * |
||
223 | * @since 1.0 |
||
224 | * @access public |
||
225 | * |
||
226 | * @var string |
||
227 | */ |
||
228 | public $post_name = ''; |
||
229 | |||
230 | /** |
||
231 | * Ping |
||
232 | * |
||
233 | * @since 1.0 |
||
234 | * @access public |
||
235 | * |
||
236 | * @var string |
||
237 | */ |
||
238 | public $to_ping = ''; |
||
239 | |||
240 | /** |
||
241 | * Pinged |
||
242 | * |
||
243 | * @since 1.0 |
||
244 | * @access public |
||
245 | * |
||
246 | * @var string |
||
247 | */ |
||
248 | public $pinged = ''; |
||
249 | |||
250 | /** |
||
251 | * The post modified date |
||
252 | * |
||
253 | * @since 1.0 |
||
254 | * @access public |
||
255 | * |
||
256 | * @var string |
||
257 | */ |
||
258 | public $post_modified = '0000-00-00 00:00:00'; |
||
259 | |||
260 | /** |
||
261 | * The post modified GTM date |
||
262 | * |
||
263 | * @since 1.0 |
||
264 | * @access public |
||
265 | * |
||
266 | * @var string |
||
267 | */ |
||
268 | public $post_modified_gmt = '0000-00-00 00:00:00'; |
||
269 | |||
270 | /** |
||
271 | * The post filtered content |
||
272 | * |
||
273 | * @since 1.0 |
||
274 | * @access public |
||
275 | * |
||
276 | * @var string |
||
277 | */ |
||
278 | public $post_content_filtered = ''; |
||
279 | |||
280 | /** |
||
281 | * The post parent |
||
282 | * |
||
283 | * @since 1.0 |
||
284 | * @access public |
||
285 | * |
||
286 | * @var int |
||
287 | */ |
||
288 | public $post_parent = 0; |
||
289 | |||
290 | /** |
||
291 | * The post GUID |
||
292 | * |
||
293 | * @since 1.0 |
||
294 | * @access public |
||
295 | * |
||
296 | * @var string |
||
297 | */ |
||
298 | public $guid = ''; |
||
299 | |||
300 | /** |
||
301 | * The menu order |
||
302 | * |
||
303 | * @since 1.0 |
||
304 | * @access public |
||
305 | * |
||
306 | * @var int |
||
307 | */ |
||
308 | public $menu_order = 0; |
||
309 | |||
310 | /** |
||
311 | * The mime type0 |
||
312 | * |
||
313 | * @since 1.0 |
||
314 | * @access public |
||
315 | * |
||
316 | * @var string |
||
317 | */ |
||
318 | public $post_mime_type = ''; |
||
319 | |||
320 | /** |
||
321 | * The comment count |
||
322 | * |
||
323 | * @since 1.0 |
||
324 | * @access public |
||
325 | * |
||
326 | * @var int |
||
327 | */ |
||
328 | public $comment_count = 0; |
||
329 | |||
330 | /** |
||
331 | * Filtered |
||
332 | * |
||
333 | * @since 1.0 |
||
334 | * @access public |
||
335 | * |
||
336 | * @var string |
||
337 | */ |
||
338 | public $filter; |
||
339 | |||
340 | /** |
||
341 | * Class Constructor |
||
342 | * |
||
343 | * Set up the Give Donate Form Class. |
||
344 | * |
||
345 | * @since 1.0 |
||
346 | * @access public |
||
347 | * |
||
348 | * @param int|bool $_id Post id. Default is false. |
||
349 | * @param array $_args Arguments passed. |
||
350 | */ |
||
351 | public function __construct( $_id = false, $_args = array() ) { |
||
356 | |||
357 | /** |
||
358 | * Given the donation form data, let's set the variables |
||
359 | * |
||
360 | * @since 1.5 |
||
361 | * @access private |
||
362 | * |
||
363 | * @param WP_Post $donation_form WP_Post Object for the donation form. |
||
364 | * |
||
365 | * @return bool If the setup was successful or not. |
||
366 | */ |
||
367 | private function setup_donation_form( $donation_form ) { |
||
387 | |||
388 | /** |
||
389 | * Magic __get function to dispatch a call to retrieve a private property |
||
390 | * |
||
391 | * @since 1.0 |
||
392 | * @access public |
||
393 | * |
||
394 | * @param string $key |
||
395 | * |
||
396 | * @return mixed |
||
397 | */ |
||
398 | public function __get( $key ) { |
||
408 | |||
409 | /** |
||
410 | * Creates a donation form |
||
411 | * |
||
412 | * @since 1.5 |
||
413 | * @access public |
||
414 | * |
||
415 | * @param array $data Array of attributes for a donation form. |
||
416 | * |
||
417 | * @return bool|int False if data isn't passed and class not instantiated for creation, or New Form ID. |
||
418 | */ |
||
419 | public function create( $data = array() ) { |
||
455 | |||
456 | /** |
||
457 | * Retrieve the ID |
||
458 | * |
||
459 | * @since 1.0 |
||
460 | * @access public |
||
461 | * |
||
462 | * @return int Donation form ID. |
||
463 | */ |
||
464 | public function get_ID() { |
||
467 | |||
468 | /** |
||
469 | * Retrieve the donation form name |
||
470 | * |
||
471 | * @since 1.5 |
||
472 | * @access public |
||
473 | * |
||
474 | * @return string Donation form name. |
||
475 | */ |
||
476 | public function get_name() { |
||
479 | |||
480 | /** |
||
481 | * Retrieve the price |
||
482 | * |
||
483 | * @since 1.0 |
||
484 | * @access public |
||
485 | * |
||
486 | * @return float Price. |
||
487 | */ |
||
488 | View Code Duplication | public function get_price() { |
|
516 | |||
517 | /** |
||
518 | * Retrieve the minimum price. |
||
519 | * |
||
520 | * @since 1.3.6 |
||
521 | * @access public |
||
522 | * |
||
523 | * @return float Minimum price. |
||
524 | */ |
||
525 | public function get_minimum_price() { |
||
544 | |||
545 | /** |
||
546 | * Retrieve the maximum price. |
||
547 | * |
||
548 | * @since 2.1 |
||
549 | * @access public |
||
550 | * |
||
551 | * @return float Maximum price. |
||
552 | */ |
||
553 | View Code Duplication | public function get_maximum_price() { |
|
565 | |||
566 | /** |
||
567 | * Retrieve the variable prices |
||
568 | * |
||
569 | * @since 1.0 |
||
570 | * @access public |
||
571 | * |
||
572 | * @return array Variable prices. |
||
573 | */ |
||
574 | public function get_prices() { |
||
593 | |||
594 | /** |
||
595 | * Get donation form level info |
||
596 | * |
||
597 | * @since 2.0.6 |
||
598 | * @access public |
||
599 | * |
||
600 | * @param $price_id |
||
601 | * |
||
602 | * @return array|null |
||
603 | */ |
||
604 | public function get_level_info( $price_id ) { |
||
623 | |||
624 | |||
625 | /** |
||
626 | * Retrieve the goal |
||
627 | * |
||
628 | * @since 1.0 |
||
629 | * @access public |
||
630 | * |
||
631 | * @return float Goal. |
||
632 | */ |
||
633 | public function get_goal() { |
||
656 | |||
657 | /** |
||
658 | * Determine if single price mode is enabled or disabled |
||
659 | * |
||
660 | * @since 1.0 |
||
661 | * @access public |
||
662 | * |
||
663 | * @return bool |
||
664 | */ |
||
665 | View Code Duplication | public function is_single_price_mode() { |
|
685 | |||
686 | /** |
||
687 | * Determine if custom price mode is enabled or disabled |
||
688 | * |
||
689 | * @since 1.6 |
||
690 | * @access public |
||
691 | * |
||
692 | * @return bool |
||
693 | */ |
||
694 | public function is_custom_price_mode() { |
||
714 | |||
715 | /** |
||
716 | * Determine if custom price mode is enabled or disabled |
||
717 | * |
||
718 | * @since 1.8.18 |
||
719 | * @access public |
||
720 | * |
||
721 | * @param string|float $amount Donation Amount. |
||
722 | * |
||
723 | * @return bool |
||
724 | */ |
||
725 | public function is_custom_price( $amount ) { |
||
753 | |||
754 | /** |
||
755 | * Has Variable Prices |
||
756 | * |
||
757 | * Determine if the donation form has variable prices enabled |
||
758 | * |
||
759 | * @since 1.0 |
||
760 | * @access public |
||
761 | * |
||
762 | * @return bool |
||
763 | */ |
||
764 | View Code Duplication | public function has_variable_prices() { |
|
782 | |||
783 | /** |
||
784 | * Retrieve the donation form type, set or multi-level |
||
785 | * |
||
786 | * @since 1.5 |
||
787 | * @access public |
||
788 | * |
||
789 | * @return string Type of donation form, either 'set' or 'multi'. |
||
790 | */ |
||
791 | View Code Duplication | public function get_type() { |
|
806 | |||
807 | /** |
||
808 | * Get form tag classes. |
||
809 | * |
||
810 | * Provides the classes for the donation <form> html tag and filters for customization. |
||
811 | * |
||
812 | * @since 1.6 |
||
813 | * @access public |
||
814 | * |
||
815 | * @param $args |
||
816 | * |
||
817 | * @return string |
||
818 | */ |
||
819 | public function get_form_classes( $args ) { |
||
838 | |||
839 | /** |
||
840 | * Get form wrap Classes. |
||
841 | * |
||
842 | * Provides the classes for the donation form div wrapper and filters for customization. |
||
843 | * |
||
844 | * @access public |
||
845 | * |
||
846 | * @param $args |
||
847 | * |
||
848 | * @return string |
||
849 | */ |
||
850 | public function get_form_wrap_classes( $args ) { |
||
882 | |||
883 | /** |
||
884 | * Get if form type set or not. |
||
885 | * |
||
886 | * @since 1.6 |
||
887 | * @access public |
||
888 | * |
||
889 | * @return bool |
||
890 | */ |
||
891 | public function is_set_type_donation_form() { |
||
896 | |||
897 | /** |
||
898 | * Get if form type multi or not. |
||
899 | * |
||
900 | * @since 1.6 |
||
901 | * @access public |
||
902 | * |
||
903 | * @return bool True if form type is 'multi' and false otherwise. |
||
904 | */ |
||
905 | public function is_multi_type_donation_form() { |
||
911 | |||
912 | /** |
||
913 | * Retrieve the sale count for the donation form |
||
914 | * |
||
915 | * @since 1.0 |
||
916 | * @access public |
||
917 | * |
||
918 | * @return int Donation form sale count. |
||
919 | */ |
||
920 | View Code Duplication | public function get_sales() { |
|
940 | |||
941 | /** |
||
942 | * Increment the sale count by one |
||
943 | * |
||
944 | * @since 1.0 |
||
945 | * @access public |
||
946 | * |
||
947 | * @param int $quantity The quantity to increase the donations by. Default is 1. |
||
948 | * |
||
949 | * @return int|false New number of total sales. |
||
950 | */ |
||
951 | public function increase_sales( $quantity = 1 ) { |
||
967 | |||
968 | /** |
||
969 | * Decrement the sale count by one |
||
970 | * |
||
971 | * @since 1.0 |
||
972 | * @access public |
||
973 | * |
||
974 | * @param int $quantity The quantity to decrease by. Default is 1. |
||
975 | * |
||
976 | * @return int|false New number of total sales. |
||
977 | */ |
||
978 | public function decrease_sales( $quantity = 1 ) { |
||
1001 | |||
1002 | /** |
||
1003 | * Retrieve the total earnings for the form |
||
1004 | * |
||
1005 | * @since 1.0 |
||
1006 | * @access public |
||
1007 | * |
||
1008 | * @return float Donation form total earnings. |
||
1009 | */ |
||
1010 | View Code Duplication | public function get_earnings() { |
|
1030 | |||
1031 | /** |
||
1032 | * Increase the earnings by the given amount |
||
1033 | * |
||
1034 | * @since 1.0 |
||
1035 | * @since 2.1 Pass the donation ID. |
||
1036 | * |
||
1037 | * @access public |
||
1038 | * |
||
1039 | * @param int $amount Amount of donation. Default is 0. |
||
1040 | * @param int $payment_id Donation ID. |
||
1041 | * |
||
1042 | * @return float|false |
||
1043 | */ |
||
1044 | View Code Duplication | public function increase_earnings( $amount = 0, $payment_id = 0 ) { |
|
1072 | |||
1073 | /** |
||
1074 | * Decrease the earnings by the given amount |
||
1075 | * |
||
1076 | * @since 1.0 |
||
1077 | * @access public |
||
1078 | * |
||
1079 | * @param int $amount Amount of donation. |
||
1080 | * @param int $payment_id Donation ID. |
||
1081 | * |
||
1082 | * @return float|false |
||
1083 | */ |
||
1084 | View Code Duplication | public function decrease_earnings( $amount, $payment_id = 0 ) { |
|
1115 | |||
1116 | /** |
||
1117 | * Determine if donation form closed or not |
||
1118 | * |
||
1119 | * Form will be close if: |
||
1120 | * a. form has fixed goal |
||
1121 | * b. close form when goal achieved cmb2 setting is set to 'Yes' |
||
1122 | * c. goal has been achieved |
||
1123 | * |
||
1124 | * @since 1.4.5 |
||
1125 | * @access public |
||
1126 | * |
||
1127 | * @return bool |
||
1128 | */ |
||
1129 | public function is_close_donation_form() { |
||
1151 | |||
1152 | |||
1153 | /** |
||
1154 | * Check whether donation form has goal or not |
||
1155 | * |
||
1156 | * @since 2.4.2 |
||
1157 | * @access public |
||
1158 | * |
||
1159 | * @return bool |
||
1160 | */ |
||
1161 | public function has_goal() { |
||
1170 | |||
1171 | /** |
||
1172 | * Updates a single meta entry for the donation form |
||
1173 | * |
||
1174 | * @since 1.5 |
||
1175 | * @access private |
||
1176 | * |
||
1177 | * @param string $meta_key The meta_key to update. |
||
1178 | * @param string|array|object $meta_value The value to put into the meta. |
||
1179 | * |
||
1180 | * @return bool The result of the update query. |
||
1181 | */ |
||
1182 | private function update_meta( $meta_key = '', $meta_value = '' ) { |
||
1198 | |||
1199 | /** |
||
1200 | * Backward Compatible function for is_close_donation_form() |
||
1201 | * |
||
1202 | * @since 2.1.0 |
||
1203 | * |
||
1204 | * @return bool |
||
1205 | */ |
||
1206 | private function bc_210_is_close_donation_form() { |
||
1244 | |||
1245 | } |
||
1246 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.