Complex classes like EE_Payment_Method 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 EE_Payment_Method, and based on these observations, apply Extract Interface, too.
1 | <?php if (!defined('EVENT_ESPRESSO_VERSION')) {exit('No direct script access allowed');} |
||
26 | class EE_Payment_Method extends EE_Base_Class{ |
||
27 | |||
28 | /** |
||
29 | * Payment Method type object, which has all the info about this type of payment method, |
||
30 | * including functions for processing payments, to get settings forms, etc. |
||
31 | * @var EE_PMT_Base |
||
32 | */ |
||
33 | protected $_type_obj; |
||
34 | |||
35 | |||
36 | |||
37 | /** |
||
38 | * @param array $props_n_values |
||
39 | * @return EE_Payment_Method |
||
40 | * @throws \EE_Error |
||
41 | */ |
||
42 | public static function new_instance( $props_n_values = array()) { |
||
46 | |||
47 | |||
48 | |||
49 | /** |
||
50 | * @param array $props_n_values |
||
51 | * @return EE_Payment_Method |
||
52 | * @throws \EE_Error |
||
53 | */ |
||
54 | public static function new_instance_from_db ( $props_n_values = array()) { |
||
57 | |||
58 | |||
59 | |||
60 | /** |
||
61 | * Checks if there is a payment method class of the given 'PMD_type', and if so returns the classname. |
||
62 | * Otherwise returns a normal EE_Payment_Method |
||
63 | * @param array $props_n_values where 'PMD_type' is a gateway name like 'Paypal_Standard','Invoice',etc (basically |
||
64 | * the classname minus 'EEPM_') |
||
65 | * @return string |
||
66 | */ |
||
67 | // private static function _payment_method_type($props_n_values){ |
||
68 | // EE_Registry::instance()->load_lib('Payment_Method_Manager'); |
||
69 | // $type_string = isset($props_n_values['PMD_type']) ? $props_n_values['PMD_type'] : NULL; |
||
70 | // if(EE_Payment_Method_Manager::instance()->payment_method_type_exists($type_string)){ |
||
71 | // return 'EEPM_'.$type_string; |
||
72 | // }else{ |
||
73 | // return __CLASS__; |
||
74 | // } |
||
75 | // } |
||
76 | |||
77 | |||
78 | |||
79 | /** |
||
80 | * Gets whether this payment method can be used anywhere at all (ie frontend cart, admin, etc) |
||
81 | * @return boolean |
||
82 | */ |
||
83 | public function active() { |
||
86 | |||
87 | |||
88 | |||
89 | /** |
||
90 | * Sets this PM as active by making it usable within the CART scope. Offline gateways |
||
91 | * are also usable from the admin-scope as well. DOES NOT SAVE it |
||
92 | * |
||
93 | * @throws \EE_Error |
||
94 | */ |
||
95 | public function set_active(){ |
||
103 | |||
104 | |||
105 | |||
106 | /** |
||
107 | * Makes this payment method apply to NO scopes at all. DOES NOT SAVE it. |
||
108 | */ |
||
109 | public function deactivate(){ |
||
112 | |||
113 | |||
114 | |||
115 | /** |
||
116 | * Gets button_url |
||
117 | * @return string |
||
118 | */ |
||
119 | public function button_url() { |
||
122 | |||
123 | |||
124 | |||
125 | /** |
||
126 | * Sets button_url |
||
127 | * @param string $button_url |
||
128 | */ |
||
129 | public function set_button_url($button_url) { |
||
132 | |||
133 | |||
134 | |||
135 | /** |
||
136 | * Gets debug_mode |
||
137 | * @return boolean |
||
138 | */ |
||
139 | public function debug_mode() { |
||
142 | |||
143 | |||
144 | |||
145 | /** |
||
146 | * Sets debug_mode |
||
147 | * @param boolean $debug_mode |
||
148 | */ |
||
149 | public function set_debug_mode($debug_mode) { |
||
152 | |||
153 | |||
154 | |||
155 | /** |
||
156 | * Gets description |
||
157 | * @return string |
||
158 | */ |
||
159 | public function description() { |
||
162 | |||
163 | |||
164 | |||
165 | /** |
||
166 | * Sets description |
||
167 | * @param string $description |
||
168 | */ |
||
169 | public function set_description($description) { |
||
172 | |||
173 | |||
174 | |||
175 | /** |
||
176 | * Gets name |
||
177 | * @return string |
||
178 | */ |
||
179 | public function name() { |
||
182 | |||
183 | |||
184 | |||
185 | /** |
||
186 | * Sets name |
||
187 | * @param string $name |
||
188 | */ |
||
189 | public function set_name($name) { |
||
192 | |||
193 | |||
194 | |||
195 | /** |
||
196 | * Gets open_by_default |
||
197 | * @return boolean |
||
198 | */ |
||
199 | public function open_by_default() { |
||
202 | |||
203 | |||
204 | |||
205 | /** |
||
206 | * Sets open_by_default |
||
207 | * @param boolean $open_by_default |
||
208 | */ |
||
209 | public function set_open_by_default($open_by_default) { |
||
212 | |||
213 | |||
214 | |||
215 | /** |
||
216 | * Gets order |
||
217 | * @return int |
||
218 | */ |
||
219 | public function order() { |
||
222 | |||
223 | |||
224 | |||
225 | /** |
||
226 | * Sets order |
||
227 | * @param int $order |
||
228 | */ |
||
229 | public function set_order($order) { |
||
232 | |||
233 | |||
234 | |||
235 | /** |
||
236 | * Gets slug |
||
237 | * @return string |
||
238 | */ |
||
239 | public function slug() { |
||
242 | |||
243 | |||
244 | |||
245 | /** |
||
246 | * Sets slug |
||
247 | * @param string $slug |
||
248 | */ |
||
249 | public function set_slug($slug) { |
||
252 | |||
253 | |||
254 | |||
255 | /** |
||
256 | * Gets type |
||
257 | * @return string |
||
258 | */ |
||
259 | public function type() { |
||
262 | |||
263 | |||
264 | |||
265 | /** |
||
266 | * Sets type |
||
267 | * @param string $type |
||
268 | */ |
||
269 | public function set_type($type) { |
||
272 | |||
273 | |||
274 | |||
275 | /** |
||
276 | * Gets wp_user |
||
277 | * @return int |
||
278 | */ |
||
279 | public function wp_user() { |
||
282 | |||
283 | |||
284 | |||
285 | |||
286 | /** |
||
287 | * Sets wp_user |
||
288 | * @param int $wp_user_id |
||
289 | */ |
||
290 | public function set_wp_user($wp_user_id) { |
||
293 | |||
294 | /** |
||
295 | * Overrides parent so when PMD_type is changed we refresh the _type_obj |
||
296 | * @param string $field_name |
||
297 | * @param mixed $field_value |
||
298 | * @param boolean $use_default |
||
299 | */ |
||
300 | public function set( $field_name, $field_value, $use_default = FALSE ){ |
||
307 | |||
308 | |||
309 | |||
310 | /** |
||
311 | * Gets admin_name |
||
312 | * @return string |
||
313 | */ |
||
314 | public function admin_name() { |
||
317 | |||
318 | |||
319 | |||
320 | /** |
||
321 | * Sets admin_name |
||
322 | * @param string $admin_name |
||
323 | */ |
||
324 | public function set_admin_name($admin_name) { |
||
327 | |||
328 | |||
329 | |||
330 | /** |
||
331 | * Gets admin_desc |
||
332 | * @return string |
||
333 | */ |
||
334 | public function admin_desc() { |
||
337 | |||
338 | |||
339 | |||
340 | /** |
||
341 | * Sets admin_desc |
||
342 | * @param string $admin_desc |
||
343 | */ |
||
344 | public function set_admin_desc($admin_desc) { |
||
347 | |||
348 | |||
349 | |||
350 | /** |
||
351 | * Gets scope |
||
352 | * @return array |
||
353 | */ |
||
354 | public function scope() { |
||
357 | |||
358 | |||
359 | |||
360 | /** |
||
361 | * Sets scope |
||
362 | * @param array $scope |
||
363 | */ |
||
364 | public function set_scope($scope) { |
||
367 | |||
368 | |||
369 | |||
370 | /** |
||
371 | * Gets the payment method type for this payment method instance |
||
372 | * @return EE_PMT_Base |
||
373 | * @throws EE_Error |
||
374 | */ |
||
375 | public function type_obj(){ |
||
399 | |||
400 | |||
401 | |||
402 | /** |
||
403 | * Returns a simple array of key-value pairs combining the payment method's fields (without the 'PMD_' prefix) |
||
404 | * and the extra meta. Mostly used for passing off ot gateways. * |
||
405 | * @return array |
||
406 | */ |
||
407 | public function settings_array(){ |
||
421 | |||
422 | |||
423 | |||
424 | /** |
||
425 | * Gets the HTML for displaying the payment method on a page. |
||
426 | * |
||
427 | * @param string $url |
||
428 | * @param string $css_class |
||
429 | * @return string of HTML for displaying the button |
||
430 | * @throws \EE_Error |
||
431 | */ |
||
432 | public function button_html( $url = '', $css_class = '' ){ |
||
442 | |||
443 | |||
444 | |||
445 | /** |
||
446 | * Gets all the currencies which are an option for this payment method |
||
447 | * (as defined by the gateway and the currently active currencies) |
||
448 | * |
||
449 | * @return EE_Currency[] |
||
450 | * @throws \EE_Error |
||
451 | */ |
||
452 | public function get_all_usable_currencies(){ |
||
455 | |||
456 | |||
457 | |||
458 | /** |
||
459 | * Reports whether or not this payment method can be used for this payment method |
||
460 | * |
||
461 | * @param string $currency_code currency ID (code) |
||
462 | * @return boolean |
||
463 | * @throws \EE_Error |
||
464 | */ |
||
465 | public function usable_for_currency( $currency_code ) { |
||
473 | |||
474 | |||
475 | |||
476 | /** |
||
477 | * Returns TRUE if this payment method's gateway is an instance of EE_Onsite_Gateway |
||
478 | * |
||
479 | * @return bool |
||
480 | * @throws \EE_Error |
||
481 | */ |
||
482 | public function is_on_site(){ |
||
485 | |||
486 | |||
487 | |||
488 | /** |
||
489 | * Returns TRUE if this payment method's gateway is an instance of EE_Offsite_Gateway |
||
490 | * |
||
491 | * @return bool |
||
492 | * @throws \EE_Error |
||
493 | */ |
||
494 | public function is_off_site(){ |
||
497 | |||
498 | |||
499 | |||
500 | /** |
||
501 | * Returns TRUE if this payment method does not utilize a gateway |
||
502 | * |
||
503 | * @return bool |
||
504 | * @throws \EE_Error |
||
505 | */ |
||
506 | public function is_off_line(){ |
||
509 | |||
510 | /** |
||
511 | * Overrides default __sleep so the object type is NOT cached. |
||
512 | * This way we can rely on the normal EE_Payment_Method::type_obj() logic |
||
513 | * to load the required classes, and don't need them at the time of unserialization |
||
514 | * @return array |
||
515 | */ |
||
516 | public function __sleep(){ |
||
521 | |||
522 | |||
523 | |||
524 | /** |
||
525 | * Overrides parent to add some logging for when payment methods get deactivated |
||
526 | * |
||
527 | * @param array $set_cols_n_values |
||
528 | * @return int @see EE_Base_Class::save() |
||
529 | * @throws \EE_Error |
||
530 | */ |
||
531 | public function save( $set_cols_n_values = array() ) { |
||
549 | |||
550 | } |
||
551 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.