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 |
||
24 | class Give_Donate_Form { |
||
25 | |||
26 | /** |
||
27 | * The donation ID |
||
28 | * |
||
29 | * @since 1.0 |
||
30 | * @access public |
||
31 | * |
||
32 | * @var int |
||
33 | */ |
||
34 | public $ID = 0; |
||
35 | |||
36 | /** |
||
37 | * The donation price |
||
38 | * |
||
39 | * @since 1.0 |
||
40 | * @access private |
||
41 | * |
||
42 | * @var float |
||
43 | */ |
||
44 | private $price; |
||
45 | |||
46 | /** |
||
47 | * The minimum donation price |
||
48 | * |
||
49 | * @since 1.3.6 |
||
50 | * @access private |
||
51 | * |
||
52 | * @var float |
||
53 | */ |
||
54 | private $minimum_price; |
||
55 | |||
56 | /** |
||
57 | * The donation goal |
||
58 | * |
||
59 | * @since 1.0 |
||
60 | * @access private |
||
61 | * |
||
62 | * @var float |
||
63 | */ |
||
64 | private $goal; |
||
65 | |||
66 | /** |
||
67 | * The donation prices, if Price Levels are enabled |
||
68 | * |
||
69 | * @since 1.0 |
||
70 | * @access private |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | private $prices; |
||
75 | |||
76 | /** |
||
77 | * The form's sale count |
||
78 | * |
||
79 | * @since 1.0 |
||
80 | * @access private |
||
81 | * |
||
82 | * @var int |
||
83 | */ |
||
84 | private $sales; |
||
85 | |||
86 | /** |
||
87 | * The form's total earnings |
||
88 | * |
||
89 | * @since 1.0 |
||
90 | * @access private |
||
91 | * |
||
92 | * @var float |
||
93 | */ |
||
94 | private $earnings; |
||
95 | |||
96 | /** |
||
97 | * Declare the default properties in WP_Post as we can't extend it |
||
98 | * Anything we've declared above has been removed. |
||
99 | */ |
||
100 | |||
101 | /** |
||
102 | * The post author |
||
103 | 57 | * |
|
104 | * @since 1.0 |
||
105 | * @access public |
||
106 | 57 | * |
|
107 | * @var int |
||
108 | 57 | */ |
|
109 | public $post_author = 0; |
||
110 | |||
111 | /** |
||
112 | * The post date |
||
113 | * |
||
114 | * @since 1.0 |
||
115 | * @access public |
||
116 | * |
||
117 | * @var string |
||
118 | */ |
||
119 | public $post_date = '0000-00-00 00:00:00'; |
||
120 | 57 | ||
121 | /** |
||
122 | 57 | * The post GTM date |
|
123 | 2 | * |
|
124 | * @since 1.0 |
||
125 | * @access public |
||
126 | 57 | * |
|
127 | * @var string |
||
128 | */ |
||
129 | public $post_date_gmt = '0000-00-00 00:00:00'; |
||
130 | 57 | ||
131 | /** |
||
132 | * The post content |
||
133 | * |
||
134 | 57 | * @since 1.0 |
|
135 | * @access public |
||
136 | * |
||
137 | * @var string |
||
138 | 57 | */ |
|
139 | 57 | public $post_content = ''; |
|
140 | 57 | ||
141 | /** |
||
142 | 57 | * The post title |
|
143 | * |
||
144 | 57 | * @since 1.0 |
|
145 | * @access public |
||
146 | 57 | * |
|
147 | * @var string |
||
148 | */ |
||
149 | public $post_title = ''; |
||
150 | |||
151 | /** |
||
152 | * The post excerpt |
||
153 | * |
||
154 | * @since 1.0 |
||
155 | * @access public |
||
156 | * |
||
157 | * @var string |
||
158 | */ |
||
159 | public $post_excerpt = ''; |
||
160 | 44 | ||
161 | /** |
||
162 | 44 | * The post status |
|
163 | * |
||
164 | 44 | * @since 1.0 |
|
165 | * @access public |
||
166 | * |
||
167 | * @var string |
||
168 | */ |
||
169 | public $post_status = 'publish'; |
||
170 | |||
171 | /** |
||
172 | * The comment status |
||
173 | * |
||
174 | * @since 1.0 |
||
175 | * @access public |
||
176 | * |
||
177 | * @var string |
||
178 | */ |
||
179 | public $comment_status = 'open'; |
||
180 | |||
181 | /** |
||
182 | * The ping status |
||
183 | * |
||
184 | * @since 1.0 |
||
185 | * @access public |
||
186 | * |
||
187 | * @var string |
||
188 | */ |
||
189 | public $ping_status = 'open'; |
||
190 | |||
191 | /** |
||
192 | * The post password |
||
193 | * |
||
194 | * @since 1.0 |
||
195 | * @access public |
||
196 | * |
||
197 | * @var string |
||
198 | */ |
||
199 | public $post_password = ''; |
||
200 | |||
201 | /** |
||
202 | * The post name |
||
203 | * |
||
204 | * @since 1.0 |
||
205 | * @access public |
||
206 | * |
||
207 | * @var string |
||
208 | */ |
||
209 | public $post_name = ''; |
||
210 | |||
211 | /** |
||
212 | * Ping |
||
213 | * |
||
214 | * @since 1.0 |
||
215 | * @access public |
||
216 | * |
||
217 | * @var string |
||
218 | */ |
||
219 | public $to_ping = ''; |
||
220 | |||
221 | /** |
||
222 | * Pinged |
||
223 | * |
||
224 | * @since 1.0 |
||
225 | * @access public |
||
226 | * |
||
227 | * @var string |
||
228 | 1 | */ |
|
229 | public $pinged = ''; |
||
230 | 1 | ||
231 | /** |
||
232 | * The post modified date |
||
233 | * |
||
234 | * @since 1.0 |
||
235 | * @access public |
||
236 | * |
||
237 | * @var string |
||
238 | */ |
||
239 | public $post_modified = '0000-00-00 00:00:00'; |
||
240 | |||
241 | /** |
||
242 | * The post modified GTM date |
||
243 | * |
||
244 | * @since 1.0 |
||
245 | * @access public |
||
246 | * |
||
247 | * @var string |
||
248 | */ |
||
249 | public $post_modified_gmt = '0000-00-00 00:00:00'; |
||
250 | 15 | ||
251 | /** |
||
252 | 15 | * The post filtered content |
|
253 | * |
||
254 | 15 | * @since 1.0 |
|
255 | * @access public |
||
256 | 15 | * |
|
257 | * @var string |
||
258 | 4 | */ |
|
259 | public $post_content_filtered = ''; |
||
260 | 4 | ||
261 | /** |
||
262 | 11 | * The post parent |
|
263 | * |
||
264 | * @since 1.0 |
||
265 | * @access public |
||
266 | 15 | * |
|
267 | * @var int |
||
268 | */ |
||
269 | public $post_parent = 0; |
||
270 | |||
271 | /** |
||
272 | * The post GUID |
||
273 | * |
||
274 | * @since 1.0 |
||
275 | * @access public |
||
276 | 15 | * |
|
277 | * @var string |
||
278 | */ |
||
279 | public $guid = ''; |
||
280 | |||
281 | /** |
||
282 | * The menu order |
||
283 | * |
||
284 | * @since 1.0 |
||
285 | 2 | * @access public |
|
286 | * |
||
287 | 2 | * @var int |
|
288 | */ |
||
289 | 2 | public $menu_order = 0; |
|
290 | 2 | ||
291 | /** |
||
292 | 2 | * The mime type0 |
|
293 | * |
||
294 | 1 | * @since 1.0 |
|
295 | * @access public |
||
296 | 1 | * |
|
297 | * @var string |
||
298 | 1 | */ |
|
299 | public $post_mime_type = ''; |
||
300 | |||
301 | /** |
||
302 | 2 | * The comment count |
|
303 | * |
||
304 | 2 | * @since 1.0 |
|
305 | * @access public |
||
306 | * |
||
307 | * @var int |
||
308 | */ |
||
309 | public $comment_count = 0; |
||
310 | |||
311 | /** |
||
312 | * Filtered |
||
313 | 36 | * |
|
314 | * @since 1.0 |
||
315 | 36 | * @access public |
|
316 | * |
||
317 | 36 | * @var string |
|
318 | */ |
||
319 | 36 | public $filter; |
|
320 | |||
321 | /** |
||
322 | * Class Constructor |
||
323 | * |
||
324 | * Set up the Give Donate Form Class. |
||
325 | * |
||
326 | * @since 1.0 |
||
327 | * @access public |
||
328 | * |
||
329 | 36 | * @param bool $_id Post id. Default is false. |
|
330 | * @param array $_args Arguments passed. |
||
331 | * |
||
332 | * @return void |
||
333 | */ |
||
334 | public function __construct( $_id = false, $_args = array() ) { |
||
340 | |||
341 | 2 | /** |
|
342 | * Given the donation form data, let's set the variables |
||
343 | 2 | * |
|
344 | * @since 1.5 |
||
345 | 2 | * @access private |
|
346 | * |
||
347 | 1 | * @param WP_Post $donation_form WP_Post Object for the donation form. |
|
348 | * |
||
349 | 1 | * @return bool If the setup was successful or not. |
|
350 | */ |
||
351 | 2 | private function setup_donation_form( $donation_form ) { |
|
380 | |||
381 | /** |
||
382 | * Magic __get function to dispatch a call to retrieve a private property |
||
383 | * |
||
384 | * @since 1.0 |
||
385 | * @access public |
||
386 | * |
||
387 | * @param string $key |
||
388 | * |
||
389 | * @return mixed |
||
390 | * @throws Exception |
||
391 | */ |
||
392 | public function __get( $key ) { |
||
406 | |||
407 | /** |
||
408 | * Creates a donation form |
||
409 | * |
||
410 | * @since 1.5 |
||
411 | 53 | * @access public |
|
412 | * |
||
413 | * @param array $data Array of attributes for a donation form. |
||
414 | * |
||
415 | * @return mixed False if data isn't passed and class not instantiated for creation, or New Form ID. |
||
416 | */ |
||
417 | public function create( $data = array() ) { |
||
453 | 42 | ||
454 | /** |
||
455 | * Retrieve the ID |
||
456 | * |
||
457 | * @since 1.0 |
||
458 | 42 | * @access public |
|
459 | * |
||
460 | 42 | * @return int Donation form ID. |
|
461 | */ |
||
462 | public function get_ID() { |
||
465 | |||
466 | /** |
||
467 | * Retrieve the donation form name |
||
468 | * |
||
469 | * @since 1.5 |
||
470 | * @access public |
||
471 | * |
||
472 | * @return string Donation form name. |
||
473 | 42 | */ |
|
474 | public function get_name() { |
||
477 | 42 | ||
478 | /** |
||
479 | 42 | * Retrieve the price |
|
480 | * |
||
481 | 42 | * @since 1.0 |
|
482 | * @access public |
||
483 | 42 | * |
|
484 | * @return float Price. |
||
485 | */ |
||
486 | public function get_price() { |
||
514 | |||
515 | /** |
||
516 | * Retrieve the minimum price. |
||
517 | 15 | * |
|
518 | * @since 1.3.6 |
||
519 | 16 | * @access public |
|
520 | * |
||
521 | * @return float Minimum price. |
||
522 | */ |
||
523 | public function get_minimum_price() { |
||
544 | 43 | ||
545 | /** |
||
546 | 43 | * Retrieve the variable prices |
|
547 | * |
||
548 | * @since 1.0 |
||
549 | * @access public |
||
550 | * |
||
551 | * @return array Variable prices. |
||
552 | */ |
||
553 | public function get_prices() { |
||
572 | |||
573 | /** |
||
574 | * Retrieve the goal |
||
575 | * |
||
576 | * @since 1.0 |
||
577 | * @access public |
||
578 | * |
||
579 | 19 | * @return float Goal. |
|
580 | */ |
||
581 | 19 | public function get_goal() { |
|
602 | |||
603 | /** |
||
604 | * Determine if single price mode is enabled or disabled |
||
605 | * |
||
606 | * @since 1.0 |
||
607 | * @access public |
||
608 | * |
||
609 | * @return bool |
||
610 | */ |
||
611 | public function is_single_price_mode() { |
||
631 | |||
632 | /** |
||
633 | * Determine if custom price mode is enabled or disabled |
||
634 | * |
||
635 | * @since 1.6 |
||
636 | * @access public |
||
637 | * |
||
638 | 1 | * @return bool |
|
639 | */ |
||
640 | 1 | public function is_custom_price_mode() { |
|
660 | 42 | ||
661 | /** |
||
662 | 42 | * Has Variable Prices |
|
663 | 1 | * |
|
664 | * Determine if the donation form has variable prices enabled |
||
665 | * |
||
666 | * @since 1.0 |
||
667 | 42 | * @access public |
|
668 | * |
||
669 | 42 | * @return bool |
|
670 | 42 | */ |
|
671 | 42 | public function has_variable_prices() { |
|
689 | |||
690 | /** |
||
691 | * Retrieve the donation form type, set or multi-level |
||
692 | * |
||
693 | * @since 1.5 |
||
694 | * @access public |
||
695 | * |
||
696 | * @return string Type of donation form, either 'set' or 'multi'. |
||
697 | */ |
||
698 | public function get_type() { |
||
713 | |||
714 | /** |
||
715 | * Get form tag classes. |
||
716 | * |
||
717 | * Provides the classes for the donation <form> html tag and filters for customization. |
||
718 | * |
||
719 | * @since 1.6 |
||
720 | * @access public |
||
721 | * |
||
722 | * @param $args |
||
723 | * |
||
724 | * @return string |
||
725 | */ |
||
726 | public function get_form_classes( $args ) { |
||
745 | |||
746 | /** |
||
747 | * Get form wrap Classes. |
||
748 | * |
||
749 | * Provides the classes for the donation form div wrapper and filters for customization. |
||
750 | * |
||
751 | * @access public |
||
752 | * |
||
753 | * @param $args |
||
754 | * |
||
755 | * @return string |
||
756 | */ |
||
757 | public function get_form_wrap_classes( $args ) { |
||
772 | |||
773 | /** |
||
774 | * Get if form type set or not. |
||
775 | * |
||
776 | * @since 1.6 |
||
777 | * @access public |
||
778 | * |
||
779 | * @return bool |
||
780 | * |
||
781 | public function is_set_type_donation_form() { |
||
782 | $form_type = $this->get_type(); |
||
783 | |||
784 | return ( 'set' === $form_type ? true : false ); |
||
785 | |||
786 | } |
||
787 | |||
788 | /** |
||
789 | * Get if form type multi or not. |
||
790 | * |
||
791 | * @since 1.6 |
||
792 | * @access public |
||
793 | * |
||
794 | * @return bool True if form type is 'multi' and false otherwise. |
||
795 | */ |
||
796 | public function is_multi_type_donation_form() { |
||
802 | |||
803 | /** |
||
804 | * Retrieve the sale count for the donation form |
||
805 | * |
||
806 | * @since 1.0 |
||
807 | * @access public |
||
808 | * |
||
809 | * @return int Donation form sale count. |
||
810 | */ |
||
811 | public function get_sales() { |
||
831 | |||
832 | /** |
||
833 | * Increment the sale count by one |
||
834 | * |
||
835 | * @since 1.0 |
||
836 | * @access public |
||
837 | * |
||
838 | * @param int $quantity The quantity to increase the donations by. Default is 1. |
||
839 | * |
||
840 | * @return int|false New number of total sales. |
||
841 | */ |
||
842 | public function increase_sales( $quantity = 1 ) { |
||
858 | |||
859 | /** |
||
860 | * Decrement the sale count by one |
||
861 | * |
||
862 | * @since 1.0 |
||
863 | * @access public |
||
864 | * |
||
865 | * @param int $quantity The quantity to decrease by. Default is 1. |
||
866 | * |
||
867 | * @return int|false New number of total sales. |
||
868 | */ |
||
869 | public function decrease_sales( $quantity = 1 ) { |
||
892 | |||
893 | /** |
||
894 | * Retrieve the total earnings for the form |
||
895 | * |
||
896 | * @since 1.0 |
||
897 | * @access public |
||
898 | * |
||
899 | * @return float Donation form total earnings. |
||
900 | */ |
||
901 | public function get_earnings() { |
||
921 | |||
922 | /** |
||
923 | * Increase the earnings by the given amount |
||
924 | * |
||
925 | * @since 1.0 |
||
926 | * @access public |
||
927 | * |
||
928 | * @param int $amount Amount of donation. Default is 0. |
||
929 | * |
||
930 | * @return float|false |
||
931 | */ |
||
932 | public function increase_earnings( $amount = 0 ) { |
||
948 | |||
949 | /** |
||
950 | * Decrease the earnings by the given amount |
||
951 | * |
||
952 | * @since 1.0 |
||
953 | * @access public |
||
954 | * |
||
955 | * @param int $amount Amount of donation. |
||
956 | * |
||
957 | * @return float|false |
||
958 | */ |
||
959 | public function decrease_earnings( $amount ) { |
||
981 | |||
982 | /** |
||
983 | * Determine if the donation is free or if the given price ID is free |
||
984 | * |
||
985 | * @since 1.0 |
||
986 | * @access public |
||
987 | * |
||
988 | * @param int $price_id Price ID. Default is false. |
||
989 | * |
||
990 | * @return bool |
||
991 | */ |
||
992 | public function is_free( $price_id = false ) { |
||
1010 | |||
1011 | /** |
||
1012 | * Determine if donation form closed or not |
||
1013 | * |
||
1014 | * Form will be close if: |
||
1015 | * a. form has fixed goal |
||
1016 | * b. close form when goal achieved cmb2 setting is set to 'Yes' |
||
1017 | * c. goal has been achieved |
||
1018 | * |
||
1019 | * @since 1.4.5 |
||
1020 | * @access public |
||
1021 | * |
||
1022 | * @return bool |
||
1023 | */ |
||
1024 | public function is_close_donation_form() { |
||
1031 | |||
1032 | /** |
||
1033 | * Updates a single meta entry for the donation form |
||
1034 | * |
||
1035 | * @since 1.5 |
||
1036 | * @access private |
||
1037 | * |
||
1038 | * @param string $meta_key The meta_key to update. |
||
1039 | * @param string|array|object $meta_value The value to put into the meta. |
||
1040 | * |
||
1041 | * @return bool The result of the update query. |
||
1042 | */ |
||
1043 | private function update_meta( $meta_key = '', $meta_value = '' ) { |
||
1073 | |||
1074 | } |
||
1075 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.