Complex classes like Give 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, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
52 | final class Give { |
||
53 | |||
54 | /** Singleton *************************************************************/ |
||
55 | |||
56 | /** |
||
57 | * Give Instance |
||
58 | * |
||
59 | * @since 1.0 |
||
60 | * @access private |
||
61 | * |
||
62 | * @var Give() The one true Give |
||
63 | */ |
||
64 | protected static $_instance; |
||
65 | |||
66 | /** |
||
67 | * Give Roles Object |
||
68 | * |
||
69 | * @since 1.0 |
||
70 | * @access public |
||
71 | * |
||
72 | * @var Give_Roles object |
||
73 | */ |
||
74 | public $roles; |
||
75 | |||
76 | /** |
||
77 | * Give Settings Object |
||
78 | * |
||
79 | * @since 1.0 |
||
80 | * @access public |
||
81 | * |
||
82 | * @var Give_Admin_Settings object |
||
83 | */ |
||
84 | public $give_settings; |
||
85 | |||
86 | /** |
||
87 | * Give Session Object |
||
88 | * |
||
89 | * This holds donation data for user's session. |
||
90 | * |
||
91 | * @since 1.0 |
||
92 | * @access public |
||
93 | * |
||
94 | * @var Give_Session object |
||
95 | */ |
||
96 | public $session; |
||
97 | |||
98 | /** |
||
99 | * Give Session DB Object |
||
100 | * |
||
101 | * This holds donation data for user's session. |
||
102 | * |
||
103 | * @since 1.0 |
||
104 | * @access public |
||
105 | * |
||
106 | * @var Give_DB_Sessions object |
||
107 | */ |
||
108 | public $session_db; |
||
109 | |||
110 | /** |
||
111 | * Give HTML Element Helper Object |
||
112 | * |
||
113 | * @since 1.0 |
||
114 | * @access public |
||
115 | * |
||
116 | * @var Give_HTML_Elements object |
||
117 | */ |
||
118 | public $html; |
||
119 | |||
120 | /** |
||
121 | * Give Emails Object |
||
122 | * |
||
123 | * @since 1.0 |
||
124 | * @access public |
||
125 | * |
||
126 | * @var Give_Emails object |
||
127 | */ |
||
128 | public $emails; |
||
129 | |||
130 | /** |
||
131 | * Give Email Template Tags Object |
||
132 | * |
||
133 | * @since 1.0 |
||
134 | * @access public |
||
135 | * |
||
136 | * @var Give_Email_Template_Tags object |
||
137 | */ |
||
138 | public $email_tags; |
||
139 | |||
140 | /** |
||
141 | * Give Donors DB Object |
||
142 | * |
||
143 | * @since 1.0 |
||
144 | * @access public |
||
145 | * |
||
146 | * @var Give_DB_Donors object |
||
147 | */ |
||
148 | public $donors; |
||
149 | |||
150 | /** |
||
151 | * Give Donor meta DB Object |
||
152 | * |
||
153 | * @since 1.6 |
||
154 | * @access public |
||
155 | * |
||
156 | * @var Give_DB_Donor_Meta object |
||
157 | */ |
||
158 | public $donor_meta; |
||
159 | |||
160 | /** |
||
161 | * Give Sequential Donation DB Object |
||
162 | * |
||
163 | * @since 2.1.0 |
||
164 | * @access public |
||
165 | * |
||
166 | * @var Give_DB_Sequential_Ordering object |
||
167 | */ |
||
168 | public $sequential_donation_db; |
||
169 | |||
170 | /** |
||
171 | * Give API Object |
||
172 | * |
||
173 | * @since 1.0 |
||
174 | * @access public |
||
175 | * |
||
176 | * @var Give_API object |
||
177 | */ |
||
178 | public $api; |
||
179 | |||
180 | /** |
||
181 | * Give Template Loader Object |
||
182 | * |
||
183 | * @since 1.0 |
||
184 | * @access public |
||
185 | * |
||
186 | * @var Give_Template_Loader object |
||
187 | */ |
||
188 | public $template_loader; |
||
189 | |||
190 | /** |
||
191 | * Give No Login Object |
||
192 | * |
||
193 | * @since 1.0 |
||
194 | * @access public |
||
195 | * |
||
196 | * @var Give_Email_Access object |
||
197 | */ |
||
198 | public $email_access; |
||
199 | |||
200 | /** |
||
201 | * Give_tooltips Object |
||
202 | * |
||
203 | * @since 1.8.9 |
||
204 | * @access public |
||
205 | * |
||
206 | * @var Give_Tooltips object |
||
207 | */ |
||
208 | public $tooltips; |
||
209 | |||
210 | /** |
||
211 | * Give notices Object |
||
212 | * |
||
213 | * @var Give_Notices $notices |
||
214 | */ |
||
215 | public $notices; |
||
216 | |||
217 | |||
218 | /** |
||
219 | * Give logging Object |
||
220 | * |
||
221 | * @var Give_Logging $logs |
||
222 | */ |
||
223 | public $logs; |
||
224 | |||
225 | /** |
||
226 | * Give log db Object |
||
227 | * |
||
228 | * @var Give_DB_Logs $log_db |
||
229 | */ |
||
230 | public $log_db; |
||
231 | |||
232 | /** |
||
233 | * Give log meta db Object |
||
234 | * |
||
235 | * @var Give_DB_Log_Meta $logmeta_db |
||
236 | */ |
||
237 | public $logmeta_db; |
||
238 | |||
239 | /** |
||
240 | * Give payment Object |
||
241 | * |
||
242 | * @var Give_DB_Payment_Meta $payment_meta |
||
243 | */ |
||
244 | public $payment_meta; |
||
245 | |||
246 | /** |
||
247 | * Give form Object |
||
248 | * |
||
249 | * @var Give_DB_Form_Meta $form_meta |
||
250 | */ |
||
251 | public $form_meta; |
||
252 | |||
253 | /** |
||
254 | * Give form Object |
||
255 | * |
||
256 | * @var Give_Async_Process $async_process |
||
257 | */ |
||
258 | public $async_process; |
||
259 | |||
260 | /** |
||
261 | * Give scripts Object. |
||
262 | * |
||
263 | * @var Give_Scripts |
||
264 | */ |
||
265 | public $scripts; |
||
266 | |||
267 | /** |
||
268 | * Give_Seq_Donation_Number Object. |
||
269 | * |
||
270 | * @var Give_Sequential_Donation_Number |
||
271 | */ |
||
272 | public $seq_donation_number; |
||
273 | |||
274 | /** |
||
275 | * Give_Comment Object |
||
276 | * |
||
277 | * @var Give_Comment |
||
278 | */ |
||
279 | public $comment; |
||
280 | |||
281 | /** |
||
282 | * Give_Stripe Object. |
||
283 | * |
||
284 | * @since 2.5.0 |
||
285 | * @access public |
||
286 | * |
||
287 | * @var Give_Stripe |
||
288 | */ |
||
289 | public $stripe; |
||
290 | |||
291 | /** |
||
292 | * Main Give Instance |
||
293 | * |
||
294 | * Ensures that only one instance of Give exists in memory at any one |
||
295 | * time. Also prevents needing to define globals all over the place. |
||
296 | * |
||
297 | * @since 1.0 |
||
298 | * @access public |
||
299 | * |
||
300 | * @static |
||
301 | * @see Give() |
||
302 | * |
||
303 | * @return Give |
||
304 | */ |
||
305 | public static function instance() { |
||
312 | |||
313 | /** |
||
314 | * Give Constructor. |
||
315 | */ |
||
316 | public function __construct() { |
||
335 | |||
336 | /** |
||
337 | * Hook into actions and filters. |
||
338 | * |
||
339 | * @since 1.8.9 |
||
340 | */ |
||
341 | private function init_hooks() { |
||
345 | |||
346 | |||
347 | /** |
||
348 | * Init Give when WordPress Initializes. |
||
349 | * |
||
350 | * @since 1.8.9 |
||
351 | */ |
||
352 | public function init() { |
||
397 | |||
398 | /** |
||
399 | * Throw error on object clone |
||
400 | * |
||
401 | * The whole idea of the singleton design pattern is that there is a single |
||
402 | * object, therefore we don't want the object to be cloned. |
||
403 | * |
||
404 | * @since 1.0 |
||
405 | * @access protected |
||
406 | * |
||
407 | * @return void |
||
408 | */ |
||
409 | public function __clone() { |
||
413 | |||
414 | /** |
||
415 | * Disable unserializing of the class |
||
416 | * |
||
417 | * @since 1.0 |
||
418 | * @access protected |
||
419 | * |
||
420 | * @return void |
||
421 | */ |
||
422 | public function __wakeup() { |
||
426 | |||
427 | /** |
||
428 | * Setup plugin constants |
||
429 | * |
||
430 | * @since 1.0 |
||
431 | * @access private |
||
432 | * |
||
433 | * @return void |
||
434 | */ |
||
435 | private function setup_constants() { |
||
467 | |||
468 | /** |
||
469 | * Include required files |
||
470 | * |
||
471 | * @since 1.0 |
||
472 | * @access private |
||
473 | * |
||
474 | * @return void |
||
475 | */ |
||
476 | private function includes() { |
||
623 | |||
624 | /** |
||
625 | * Loads the plugin language files. |
||
626 | * |
||
627 | * @since 1.0 |
||
628 | * @access public |
||
629 | * |
||
630 | * @return void |
||
631 | */ |
||
632 | public function load_textdomain() { |
||
647 | |||
648 | |||
649 | /** |
||
650 | * Show minimum PHP version notice. |
||
651 | * |
||
652 | * @since 1.8.12 |
||
653 | * @access public |
||
654 | */ |
||
655 | public function minimum_phpversion_notice() { |
||
674 | |||
675 | /** |
||
676 | * What type of request is this? |
||
677 | * |
||
678 | * @since 2.4.0 |
||
679 | * |
||
680 | * @param string $type admin, ajax, cron or frontend. |
||
681 | * @return bool |
||
682 | */ |
||
683 | private function is_request( $type ) { |
||
697 | |||
698 | } |
||
699 | |||
721 |