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 |
||
32 | class Give_Donate_Form { |
||
33 | |||
34 | /** |
||
35 | * The donation ID. |
||
36 | * |
||
37 | * @since 1.0 |
||
38 | * @access public |
||
39 | * |
||
40 | * @var int |
||
41 | */ |
||
42 | public $ID = 0; |
||
43 | |||
44 | /** |
||
45 | * The donation price. |
||
46 | * |
||
47 | * @since 1.0 |
||
48 | * @access private |
||
49 | * |
||
50 | * @var float |
||
51 | */ |
||
52 | private $price; |
||
53 | |||
54 | /** |
||
55 | * The minimum donation price. |
||
56 | * |
||
57 | * @since 1.3.6 |
||
58 | * @access private |
||
59 | * |
||
60 | * @var float |
||
61 | */ |
||
62 | private $minimum_price; |
||
63 | |||
64 | /** |
||
65 | * The donation prices, if Price Levels are enabled. |
||
66 | * |
||
67 | * @since 1.0 |
||
68 | * @access private |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | private $prices; |
||
73 | |||
74 | /** |
||
75 | * The donation goal. |
||
76 | * |
||
77 | * @since 1.0 |
||
78 | * @access private |
||
79 | * |
||
80 | * @var float |
||
81 | */ |
||
82 | private $goal; |
||
83 | |||
84 | /** |
||
85 | * The form's sale count. |
||
86 | * |
||
87 | * @since 1.0 |
||
88 | * @access private |
||
89 | * |
||
90 | * @var int |
||
91 | */ |
||
92 | private $sales; |
||
93 | |||
94 | /** |
||
95 | * The form's total earnings |
||
96 | * |
||
97 | * @since 1.0 |
||
98 | * @access private |
||
99 | * |
||
100 | * @var float |
||
101 | */ |
||
102 | private $earnings; |
||
103 | 57 | ||
104 | /** |
||
105 | * Declare the default properties in WP_Post as we can't extend it |
||
106 | 57 | * Anything we've declared above has been removed. |
|
107 | */ |
||
108 | 57 | ||
109 | /** |
||
110 | * The post author |
||
111 | * |
||
112 | * @since 1.0 |
||
113 | * @access public |
||
114 | * |
||
115 | * @var int |
||
116 | */ |
||
117 | public $post_author = 0; |
||
118 | |||
119 | /** |
||
120 | 57 | * The post date |
|
121 | * |
||
122 | 57 | * @since 1.0 |
|
123 | 2 | * @access public |
|
124 | * |
||
125 | * @var string |
||
126 | 57 | */ |
|
127 | public $post_date = '0000-00-00 00:00:00'; |
||
128 | |||
129 | /** |
||
130 | 57 | * The post GTM date |
|
131 | * |
||
132 | * @since 1.0 |
||
133 | * @access public |
||
134 | 57 | * |
|
135 | * @var string |
||
136 | */ |
||
137 | public $post_date_gmt = '0000-00-00 00:00:00'; |
||
138 | 57 | ||
139 | 57 | /** |
|
140 | 57 | * The post content |
|
141 | * |
||
142 | 57 | * @since 1.0 |
|
143 | * @access public |
||
144 | 57 | * |
|
145 | * @var string |
||
146 | 57 | */ |
|
147 | public $post_content = ''; |
||
148 | |||
149 | /** |
||
150 | * The post title |
||
151 | * |
||
152 | * @since 1.0 |
||
153 | * @access public |
||
154 | * |
||
155 | * @var string |
||
156 | */ |
||
157 | public $post_title = ''; |
||
158 | |||
159 | /** |
||
160 | 44 | * The post excerpt |
|
161 | * |
||
162 | 44 | * @since 1.0 |
|
163 | * @access public |
||
164 | 44 | * |
|
165 | * @var string |
||
166 | */ |
||
167 | public $post_excerpt = ''; |
||
168 | |||
169 | /** |
||
170 | * The post status |
||
171 | * |
||
172 | * @since 1.0 |
||
173 | * @access public |
||
174 | * |
||
175 | * @var string |
||
176 | */ |
||
177 | public $post_status = 'publish'; |
||
178 | |||
179 | /** |
||
180 | * The comment status |
||
181 | * |
||
182 | * @since 1.0 |
||
183 | * @access public |
||
184 | * |
||
185 | * @var string |
||
186 | */ |
||
187 | public $comment_status = 'open'; |
||
188 | |||
189 | /** |
||
190 | * The ping status |
||
191 | * |
||
192 | * @since 1.0 |
||
193 | * @access public |
||
194 | * |
||
195 | * @var string |
||
196 | */ |
||
197 | public $ping_status = 'open'; |
||
198 | |||
199 | /** |
||
200 | * The post password |
||
201 | * |
||
202 | * @since 1.0 |
||
203 | * @access public |
||
204 | * |
||
205 | * @var string |
||
206 | */ |
||
207 | public $post_password = ''; |
||
208 | |||
209 | /** |
||
210 | * The post name |
||
211 | * |
||
212 | * @since 1.0 |
||
213 | * @access public |
||
214 | * |
||
215 | * @var string |
||
216 | */ |
||
217 | public $post_name = ''; |
||
218 | |||
219 | /** |
||
220 | * Ping |
||
221 | * |
||
222 | * @since 1.0 |
||
223 | * @access public |
||
224 | * |
||
225 | * @var string |
||
226 | */ |
||
227 | public $to_ping = ''; |
||
228 | 1 | ||
229 | /** |
||
230 | 1 | * Pinged |
|
231 | * |
||
232 | * @since 1.0 |
||
233 | * @access public |
||
234 | * |
||
235 | * @var string |
||
236 | */ |
||
237 | public $pinged = ''; |
||
238 | |||
239 | /** |
||
240 | * The post modified date |
||
241 | * |
||
242 | * @since 1.0 |
||
243 | * @access public |
||
244 | * |
||
245 | * @var string |
||
246 | */ |
||
247 | public $post_modified = '0000-00-00 00:00:00'; |
||
248 | |||
249 | /** |
||
250 | 15 | * The post modified GTM date |
|
251 | * |
||
252 | 15 | * @since 1.0 |
|
253 | * @access public |
||
254 | 15 | * |
|
255 | * @var string |
||
256 | 15 | */ |
|
257 | public $post_modified_gmt = '0000-00-00 00:00:00'; |
||
258 | 4 | ||
259 | /** |
||
260 | 4 | * The post filtered content |
|
261 | * |
||
262 | 11 | * @since 1.0 |
|
263 | * @access public |
||
264 | * |
||
265 | * @var string |
||
266 | 15 | */ |
|
267 | public $post_content_filtered = ''; |
||
268 | |||
269 | /** |
||
270 | * The post parent |
||
271 | * |
||
272 | * @since 1.0 |
||
273 | * @access public |
||
274 | * |
||
275 | * @var int |
||
276 | 15 | */ |
|
277 | public $post_parent = 0; |
||
278 | |||
279 | /** |
||
280 | * The post GUID |
||
281 | * |
||
282 | * @since 1.0 |
||
283 | * @access public |
||
284 | * |
||
285 | 2 | * @var string |
|
286 | */ |
||
287 | 2 | public $guid = ''; |
|
288 | |||
289 | 2 | /** |
|
290 | 2 | * The menu order |
|
291 | * |
||
292 | 2 | * @since 1.0 |
|
293 | * @access public |
||
294 | 1 | * |
|
295 | * @var int |
||
296 | 1 | */ |
|
297 | public $menu_order = 0; |
||
298 | 1 | ||
299 | /** |
||
300 | * The mime type0 |
||
301 | * |
||
302 | 2 | * @since 1.0 |
|
303 | * @access public |
||
304 | 2 | * |
|
305 | * @var string |
||
306 | */ |
||
307 | public $post_mime_type = ''; |
||
308 | |||
309 | /** |
||
310 | * The comment count |
||
311 | * |
||
312 | * @since 1.0 |
||
313 | 36 | * @access public |
|
314 | * |
||
315 | 36 | * @var int |
|
316 | */ |
||
317 | 36 | public $comment_count = 0; |
|
318 | |||
319 | 36 | /** |
|
320 | * Filtered |
||
321 | * |
||
322 | * @since 1.0 |
||
323 | * @access public |
||
324 | * |
||
325 | * @var string |
||
326 | */ |
||
327 | public $filter; |
||
328 | |||
329 | 36 | /** |
|
330 | * Class Constructor |
||
331 | * |
||
332 | * Set up the Give Donate Form Class. |
||
333 | * |
||
334 | * @since 1.0 |
||
335 | * @access public |
||
336 | * |
||
337 | * @param int|bool $_id Post id. Default is false. |
||
338 | * @param array $_args Arguments passed. |
||
339 | 2 | */ |
|
340 | public function __construct( $_id = false, $_args = array() ) { |
||
346 | |||
347 | 1 | /** |
|
348 | * Given the donation form data, let's set the variables |
||
349 | 1 | * |
|
350 | * @since 1.5 |
||
351 | 2 | * @access private |
|
352 | * |
||
353 | * @param WP_Post $donation_form WP_Post Object for the donation form. |
||
354 | * |
||
355 | 2 | * @return bool If the setup was successful or not. |
|
356 | */ |
||
357 | 2 | private function setup_donation_form( $donation_form ) { |
|
386 | |||
387 | /** |
||
388 | * Magic __get function to dispatch a call to retrieve a private property |
||
389 | * |
||
390 | * @since 1.0 |
||
391 | * @access public |
||
392 | * |
||
393 | * @param string $key |
||
394 | * |
||
395 | * @return mixed |
||
396 | 53 | */ |
|
397 | View Code Duplication | public function __get( $key ) { |
|
398 | 53 | ||
399 | 53 | if ( method_exists( $this, 'get_' . $key ) ) { |
|
400 | |||
401 | 53 | return call_user_func( array( $this, 'get_' . $key ) ); |
|
402 | 36 | ||
403 | 36 | } else { |
|
404 | |||
405 | /* translators: %s: property key */ |
||
406 | return new WP_Error( 'give-form-invalid-property', sprintf( esc_html__( 'Can\'t get property %s.', 'give' ), $key ) ); |
||
407 | |||
408 | } |
||
409 | |||
410 | } |
||
411 | 53 | ||
412 | /** |
||
413 | * Creates a donation form |
||
414 | * |
||
415 | * @since 1.5 |
||
416 | * @access public |
||
417 | * |
||
418 | * @param array $data Array of attributes for a donation form. |
||
419 | * |
||
420 | * @return bool|int False if data isn't passed and class not instantiated for creation, or New Form ID. |
||
421 | */ |
||
422 | public function create( $data = array() ) { |
||
458 | 42 | ||
459 | /** |
||
460 | 42 | * Retrieve the ID |
|
461 | * |
||
462 | * @since 1.0 |
||
463 | * @access public |
||
464 | * |
||
465 | * @return int Donation form ID. |
||
466 | */ |
||
467 | public function get_ID() { |
||
470 | |||
471 | /** |
||
472 | * Retrieve the donation form name |
||
473 | 42 | * |
|
474 | * @since 1.5 |
||
475 | 42 | * @access public |
|
476 | 42 | * |
|
477 | 42 | * @return string Donation form name. |
|
478 | */ |
||
479 | 42 | public function get_name() { |
|
482 | |||
483 | 42 | /** |
|
484 | * Retrieve the price |
||
485 | * |
||
486 | * @since 1.0 |
||
487 | 1 | * @access public |
|
488 | * |
||
489 | * @return float Price. |
||
490 | */ |
||
491 | View Code Duplication | public function get_price() { |
|
519 | 16 | ||
520 | /** |
||
521 | * Retrieve the minimum price. |
||
522 | * |
||
523 | * @since 1.3.6 |
||
524 | * @access public |
||
525 | * |
||
526 | * @return float Minimum price. |
||
527 | */ |
||
528 | public function get_minimum_price() { |
||
542 | |||
543 | /** |
||
544 | 43 | * Retrieve the variable prices |
|
545 | * |
||
546 | 43 | * @since 1.0 |
|
547 | * @access public |
||
548 | * |
||
549 | * @return array Variable prices. |
||
550 | */ |
||
551 | public function get_prices() { |
||
570 | |||
571 | /** |
||
572 | * Get donation form level info |
||
573 | * |
||
574 | * @since 2.0.6 |
||
575 | * @access public |
||
576 | * |
||
577 | * @param $price_id |
||
578 | * |
||
579 | 19 | * @return array|null |
|
580 | */ |
||
581 | 19 | public function get_level_info( $price_id ) { |
|
600 | |||
601 | |||
602 | /** |
||
603 | * Retrieve the goal |
||
604 | * |
||
605 | * @since 1.0 |
||
606 | * @access public |
||
607 | * |
||
608 | * @return float Goal. |
||
609 | */ |
||
610 | public function get_goal() { |
||
629 | |||
630 | /** |
||
631 | * Determine if single price mode is enabled or disabled |
||
632 | * |
||
633 | * @since 1.0 |
||
634 | * @access public |
||
635 | * |
||
636 | * @return bool |
||
637 | */ |
||
638 | 1 | View Code Duplication | public function is_single_price_mode() { |
658 | 42 | ||
659 | /** |
||
660 | 42 | * Determine if custom price mode is enabled or disabled |
|
661 | * |
||
662 | 42 | * @since 1.6 |
|
663 | 1 | * @access public |
|
664 | * |
||
665 | * @return bool |
||
666 | */ |
||
667 | 42 | public function is_custom_price_mode() { |
|
687 | |||
688 | /** |
||
689 | * Determine if custom price mode is enabled or disabled |
||
690 | * |
||
691 | * @since 1.8.18 |
||
692 | * @access public |
||
693 | * |
||
694 | * @param string|float $amount Donation Amount. |
||
695 | * |
||
696 | * @return bool |
||
697 | */ |
||
698 | public function is_custom_price( $amount ) { |
||
726 | |||
727 | /** |
||
728 | * Has Variable Prices |
||
729 | * |
||
730 | * Determine if the donation form has variable prices enabled |
||
731 | * |
||
732 | * @since 1.0 |
||
733 | * @access public |
||
734 | * |
||
735 | * @return bool |
||
736 | */ |
||
737 | View Code Duplication | public function has_variable_prices() { |
|
755 | |||
756 | /** |
||
757 | * Retrieve the donation form type, set or multi-level |
||
758 | * |
||
759 | * @since 1.5 |
||
760 | * @access public |
||
761 | * |
||
762 | * @return string Type of donation form, either 'set' or 'multi'. |
||
763 | */ |
||
764 | View Code Duplication | public function get_type() { |
|
779 | |||
780 | /** |
||
781 | * Get form tag classes. |
||
782 | * |
||
783 | * Provides the classes for the donation <form> html tag and filters for customization. |
||
784 | * |
||
785 | * @since 1.6 |
||
786 | * @access public |
||
787 | * |
||
788 | * @param $args |
||
789 | * |
||
790 | * @return string |
||
791 | */ |
||
792 | public function get_form_classes( $args ) { |
||
811 | |||
812 | /** |
||
813 | * Get form wrap Classes. |
||
814 | * |
||
815 | * Provides the classes for the donation form div wrapper and filters for customization. |
||
816 | * |
||
817 | * @access public |
||
818 | * |
||
819 | * @param $args |
||
820 | * |
||
821 | * @return string |
||
822 | */ |
||
823 | public function get_form_wrap_classes( $args ) { |
||
855 | |||
856 | /** |
||
857 | * Get if form type set or not. |
||
858 | * |
||
859 | * @since 1.6 |
||
860 | * @access public |
||
861 | * |
||
862 | * @return bool |
||
863 | */ |
||
864 | public function is_set_type_donation_form() { |
||
869 | |||
870 | /** |
||
871 | * Get if form type multi or not. |
||
872 | * |
||
873 | * @since 1.6 |
||
874 | * @access public |
||
875 | * |
||
876 | * @return bool True if form type is 'multi' and false otherwise. |
||
877 | */ |
||
878 | public function is_multi_type_donation_form() { |
||
884 | |||
885 | /** |
||
886 | * Retrieve the sale count for the donation form |
||
887 | * |
||
888 | * @since 1.0 |
||
889 | * @access public |
||
890 | * |
||
891 | * @return int Donation form sale count. |
||
892 | */ |
||
893 | View Code Duplication | public function get_sales() { |
|
913 | |||
914 | /** |
||
915 | * Increment the sale count by one |
||
916 | * |
||
917 | * @since 1.0 |
||
918 | * @access public |
||
919 | * |
||
920 | * @param int $quantity The quantity to increase the donations by. Default is 1. |
||
921 | * |
||
922 | * @return int|false New number of total sales. |
||
923 | */ |
||
924 | public function increase_sales( $quantity = 1 ) { |
||
940 | |||
941 | /** |
||
942 | * Decrement the sale count by one |
||
943 | * |
||
944 | * @since 1.0 |
||
945 | * @access public |
||
946 | * |
||
947 | * @param int $quantity The quantity to decrease by. Default is 1. |
||
948 | * |
||
949 | * @return int|false New number of total sales. |
||
950 | */ |
||
951 | public function decrease_sales( $quantity = 1 ) { |
||
974 | |||
975 | /** |
||
976 | * Retrieve the total earnings for the form |
||
977 | * |
||
978 | * @since 1.0 |
||
979 | * @access public |
||
980 | * |
||
981 | * @return float Donation form total earnings. |
||
982 | */ |
||
983 | View Code Duplication | public function get_earnings() { |
|
1003 | |||
1004 | /** |
||
1005 | * Increase the earnings by the given amount |
||
1006 | * |
||
1007 | * @since 1.0 |
||
1008 | * @access public |
||
1009 | * |
||
1010 | * @param int $amount Amount of donation. Default is 0. |
||
1011 | * |
||
1012 | * @return float|false |
||
1013 | */ |
||
1014 | View Code Duplication | public function increase_earnings( $amount = 0 ) { |
|
1030 | |||
1031 | /** |
||
1032 | * Decrease the earnings by the given amount |
||
1033 | * |
||
1034 | * @since 1.0 |
||
1035 | * @access public |
||
1036 | * |
||
1037 | * @param int $amount Amount of donation. |
||
1038 | * |
||
1039 | * @return float|false |
||
1040 | */ |
||
1041 | View Code Duplication | public function decrease_earnings( $amount ) { |
|
1063 | |||
1064 | /** |
||
1065 | * Determine if the donation is free or if the given price ID is free |
||
1066 | * |
||
1067 | * @since 1.0 |
||
1068 | * @access public |
||
1069 | * |
||
1070 | * @param int $price_id Price ID. Default is false. |
||
1071 | * |
||
1072 | * @return bool |
||
1073 | */ |
||
1074 | public function is_free( $price_id = false ) { |
||
1092 | |||
1093 | /** |
||
1094 | * Determine if donation form closed or not |
||
1095 | * |
||
1096 | * Form will be close if: |
||
1097 | * a. form has fixed goal |
||
1098 | * b. close form when goal achieved cmb2 setting is set to 'Yes' |
||
1099 | * c. goal has been achieved |
||
1100 | * |
||
1101 | * @since 1.4.5 |
||
1102 | * @access public |
||
1103 | * |
||
1104 | * @return bool |
||
1105 | */ |
||
1106 | public function is_close_donation_form() { |
||
1127 | |||
1128 | /** |
||
1129 | * Updates a single meta entry for the donation form |
||
1130 | * |
||
1131 | * @since 1.5 |
||
1132 | * @access private |
||
1133 | * |
||
1134 | * @param string $meta_key The meta_key to update. |
||
1135 | * @param string|array|object $meta_value The value to put into the meta. |
||
1136 | * |
||
1137 | * @return bool The result of the update query. |
||
1138 | */ |
||
1139 | private function update_meta( $meta_key = '', $meta_value = '' ) { |
||
1155 | |||
1156 | } |
||
1157 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.