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');} |
||
11 | class EE_Payment_Method extends EE_Base_Class{ |
||
12 | |||
13 | /** |
||
14 | * Payment Method type object, which has all the info about this type of payment method, |
||
15 | * including functions for processing payments, to get settings forms, etc. |
||
16 | * @var EE_PMT_Base |
||
17 | */ |
||
18 | protected $_type_obj; |
||
19 | |||
20 | |||
21 | |||
22 | /** |
||
23 | * @param array $props_n_values |
||
24 | * @return EE_Payment_Method |
||
25 | * @throws \EE_Error |
||
26 | */ |
||
27 | public static function new_instance( $props_n_values = array()) { |
||
31 | |||
32 | |||
33 | |||
34 | /** |
||
35 | * @param array $props_n_values |
||
36 | * @return EE_Payment_Method |
||
37 | * @throws \EE_Error |
||
38 | */ |
||
39 | public static function new_instance_from_db ( $props_n_values = array()) { |
||
42 | |||
43 | |||
44 | |||
45 | /** |
||
46 | * Checks if there is a payment method class of the given 'PMD_type', and if so returns the classname. |
||
47 | * Otherwise returns a normal EE_Payment_Method |
||
48 | * @param array $props_n_values where 'PMD_type' is a gateway name like 'Paypal_Standard','Invoice',etc (basically |
||
49 | * the classname minus 'EEPM_') |
||
50 | * @return string |
||
51 | */ |
||
52 | // private static function _payment_method_type($props_n_values){ |
||
53 | // EE_Registry::instance()->load_lib('Payment_Method_Manager'); |
||
54 | // $type_string = isset($props_n_values['PMD_type']) ? $props_n_values['PMD_type'] : NULL; |
||
55 | // if(EE_Payment_Method_Manager::instance()->payment_method_type_exists($type_string)){ |
||
56 | // return 'EEPM_'.$type_string; |
||
57 | // }else{ |
||
58 | // return __CLASS__; |
||
59 | // } |
||
60 | // } |
||
61 | |||
62 | |||
63 | |||
64 | /** |
||
65 | * Gets whether this payment method can be used anywhere at all (ie frontend cart, admin, etc) |
||
66 | * @return boolean |
||
67 | */ |
||
68 | public function active() { |
||
69 | return array_intersect(array_keys(EEM_Payment_Method::instance()->scopes()),$this->scope()); |
||
70 | } |
||
71 | |||
72 | |||
73 | |||
74 | /** |
||
75 | * Sets this PM as active by making it usable within the CART scope. Offline gateways |
||
76 | * are also usable from the admin-scope as well. DOES NOT SAVE it |
||
77 | * |
||
78 | * @throws \EE_Error |
||
79 | */ |
||
80 | public function set_active(){ |
||
81 | $default_scopes = array(EEM_Payment_Method::scope_cart); |
||
82 | if($this->type_obj() && |
||
83 | $this->type_obj()->payment_occurs() === EE_PMT_Base::offline){ |
||
84 | $default_scopes[] = EEM_Payment_Method::scope_admin; |
||
85 | } |
||
86 | $this->set_scope($default_scopes); |
||
87 | } |
||
88 | |||
89 | |||
90 | |||
91 | /** |
||
92 | * Makes this payment method apply to NO scopes at all. DOES NOT SAVE it. |
||
93 | */ |
||
94 | public function deactivate(){ |
||
95 | $this->set_scope(array()); |
||
96 | } |
||
97 | |||
98 | |||
99 | |||
100 | /** |
||
101 | * Gets button_url |
||
102 | * @return string |
||
103 | */ |
||
104 | public function button_url() { |
||
105 | return $this->get('PMD_button_url'); |
||
106 | } |
||
107 | |||
108 | |||
109 | |||
110 | /** |
||
111 | * Sets button_url |
||
112 | * @param string $button_url |
||
113 | */ |
||
114 | public function set_button_url($button_url) { |
||
115 | $this->set('PMD_button_url', $button_url); |
||
116 | } |
||
117 | |||
118 | |||
119 | |||
120 | /** |
||
121 | * Gets debug_mode |
||
122 | * @return boolean |
||
123 | */ |
||
124 | public function debug_mode() { |
||
125 | return $this->get('PMD_debug_mode'); |
||
126 | } |
||
127 | |||
128 | |||
129 | |||
130 | /** |
||
131 | * Sets debug_mode |
||
132 | * @param boolean $debug_mode |
||
133 | */ |
||
134 | public function set_debug_mode($debug_mode) { |
||
135 | $this->set('PMD_debug_mode', $debug_mode); |
||
136 | } |
||
137 | |||
138 | |||
139 | |||
140 | /** |
||
141 | * Gets description |
||
142 | * @return string |
||
143 | */ |
||
144 | public function description() { |
||
145 | return $this->get('PMD_desc'); |
||
146 | } |
||
147 | |||
148 | |||
149 | |||
150 | /** |
||
151 | * Sets description |
||
152 | * @param string $description |
||
153 | */ |
||
154 | public function set_description($description) { |
||
155 | $this->set('PMD_desc', $description); |
||
156 | } |
||
157 | |||
158 | |||
159 | |||
160 | /** |
||
161 | * Gets name |
||
162 | * @return string |
||
163 | */ |
||
164 | public function name() { |
||
165 | return $this->get('PMD_name'); |
||
166 | } |
||
167 | |||
168 | |||
169 | |||
170 | /** |
||
171 | * Sets name |
||
172 | * @param string $name |
||
173 | */ |
||
174 | public function set_name($name) { |
||
175 | $this->set('PMD_name', $name); |
||
176 | } |
||
177 | |||
178 | |||
179 | |||
180 | /** |
||
181 | * Gets open_by_default |
||
182 | * @return boolean |
||
183 | */ |
||
184 | public function open_by_default() { |
||
185 | return $this->get('PMD_open_by_default'); |
||
186 | } |
||
187 | |||
188 | |||
189 | |||
190 | /** |
||
191 | * Sets open_by_default |
||
192 | * @param boolean $open_by_default |
||
193 | */ |
||
194 | public function set_open_by_default($open_by_default) { |
||
195 | $this->set('PMD_open_by_default', $open_by_default); |
||
196 | } |
||
197 | |||
198 | |||
199 | |||
200 | /** |
||
201 | * Gets order |
||
202 | * @return int |
||
203 | */ |
||
204 | public function order() { |
||
205 | return $this->get('PMD_order'); |
||
206 | } |
||
207 | |||
208 | |||
209 | |||
210 | /** |
||
211 | * Sets order |
||
212 | * @param int $order |
||
213 | */ |
||
214 | public function set_order($order) { |
||
215 | $this->set('PMD_order', $order); |
||
216 | } |
||
217 | |||
218 | |||
219 | |||
220 | /** |
||
221 | * Gets slug |
||
222 | * @return string |
||
223 | */ |
||
224 | public function slug() { |
||
225 | return $this->get('PMD_slug'); |
||
226 | } |
||
227 | |||
228 | |||
229 | |||
230 | /** |
||
231 | * Sets slug |
||
232 | * @param string $slug |
||
233 | */ |
||
234 | public function set_slug($slug) { |
||
235 | $this->set('PMD_slug', $slug); |
||
236 | } |
||
237 | |||
238 | |||
239 | |||
240 | /** |
||
241 | * Gets type |
||
242 | * @return string |
||
243 | */ |
||
244 | public function type() { |
||
245 | return $this->get('PMD_type'); |
||
246 | } |
||
247 | |||
248 | |||
249 | |||
250 | /** |
||
251 | * Sets type |
||
252 | * @param string $type |
||
253 | */ |
||
254 | public function set_type($type) { |
||
255 | $this->set('PMD_type', $type); |
||
256 | } |
||
257 | |||
258 | |||
259 | |||
260 | /** |
||
261 | * Gets wp_user |
||
262 | * @return int |
||
263 | */ |
||
264 | public function wp_user() { |
||
265 | return $this->get('PMD_wp_user'); |
||
266 | } |
||
267 | |||
268 | |||
269 | |||
270 | |||
271 | /** |
||
272 | * Sets wp_user |
||
273 | * @param int $wp_user_id |
||
274 | */ |
||
275 | public function set_wp_user($wp_user_id) { |
||
276 | $this->set('PMD_wp_user', $wp_user_id); |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Overrides parent so when PMD_type is changed we refresh the _type_obj |
||
281 | * @param string $field_name |
||
282 | * @param mixed $field_value |
||
283 | * @param boolean $use_default |
||
284 | */ |
||
285 | public function set( $field_name, $field_value, $use_default = FALSE ){ |
||
286 | if( $field_name === 'PMD_type' ){ |
||
287 | //the type has probably changed, so forget about its old type object |
||
288 | $this->_type_obj = NULL; |
||
289 | } |
||
290 | parent::set($field_name, $field_value, $use_default); |
||
291 | } |
||
292 | |||
293 | |||
294 | |||
295 | /** |
||
296 | * Gets admin_name |
||
297 | * @return string |
||
298 | */ |
||
299 | public function admin_name() { |
||
300 | return $this->get('PMD_admin_name'); |
||
301 | } |
||
302 | |||
303 | |||
304 | |||
305 | /** |
||
306 | * Sets admin_name |
||
307 | * @param string $admin_name |
||
308 | */ |
||
309 | public function set_admin_name($admin_name) { |
||
310 | $this->set('PMD_admin_name', $admin_name); |
||
311 | } |
||
312 | |||
313 | |||
314 | |||
315 | /** |
||
316 | * Gets admin_desc |
||
317 | * @return string |
||
318 | */ |
||
319 | public function admin_desc() { |
||
320 | return $this->get('PMD_admin_desc'); |
||
321 | } |
||
322 | |||
323 | |||
324 | |||
325 | /** |
||
326 | * Sets admin_desc |
||
327 | * @param string $admin_desc |
||
328 | */ |
||
329 | public function set_admin_desc($admin_desc) { |
||
330 | $this->set('PMD_admin_desc', $admin_desc); |
||
331 | } |
||
332 | |||
333 | |||
334 | |||
335 | /** |
||
336 | * Gets scope |
||
337 | * @return array |
||
338 | */ |
||
339 | public function scope() { |
||
340 | return $this->get('PMD_scope'); |
||
341 | } |
||
342 | |||
343 | |||
344 | |||
345 | /** |
||
346 | * Sets scope |
||
347 | * @param array $scope |
||
348 | */ |
||
349 | public function set_scope($scope) { |
||
350 | $this->set('PMD_scope', $scope); |
||
351 | } |
||
352 | |||
353 | |||
354 | |||
355 | /** |
||
356 | * Gets the payment method type for this payment method instance |
||
357 | * @return EE_PMT_Base |
||
358 | * @throws EE_Error |
||
359 | */ |
||
360 | public function type_obj(){ |
||
361 | if( ! $this->_type_obj ) { |
||
362 | EE_Registry::instance()->load_lib( 'Payment_Method_Manager' ); |
||
363 | if ( EE_Payment_Method_Manager::instance()->payment_method_type_exists( $this->type() )) { |
||
364 | $class_name = EE_Payment_Method_Manager::instance()->payment_method_class_from_type( $this->type() ); |
||
365 | if ( ! class_exists( $class_name )) { |
||
366 | throw new EE_Error( |
||
367 | sprintf( |
||
368 | __( 'An attempt to use the "%1$s" payment method failed, so it was deactivated.%2$sWas the "%1$s" Plugin recently deactivated? It can be reactivated on the %3$sPlugins Admin Page%4$s', 'event_espresso' ), |
||
369 | $class_name, |
||
370 | '<br />', |
||
371 | '<a href="' . admin_url('plugins.php') . '">', |
||
372 | '</a>' |
||
373 | ) |
||
374 | ); |
||
375 | } |
||
376 | $r = new ReflectionClass( $class_name ); |
||
377 | $this->_type_obj = $r->newInstanceArgs( array( $this )); |
||
378 | } else { |
||
379 | throw new EE_Error( sprintf( __( 'A payment method of type "%1$s" does not exist. Only ones existing are: %2$s', 'event_espresso' ), $this->type(), implode(',', EE_Payment_Method_Manager::instance()->payment_method_type_names() ) ) ); |
||
380 | } |
||
381 | } |
||
382 | return $this->_type_obj; |
||
383 | } |
||
384 | |||
385 | |||
386 | |||
387 | /** |
||
388 | * Returns a simple array of key-value pairs combining the payment method's fields (without the 'PMD_' prefix) |
||
389 | * and the extra meta. Mostly used for passing off ot gateways. * |
||
390 | * @return array |
||
391 | */ |
||
392 | public function settings_array(){ |
||
406 | |||
407 | |||
408 | |||
409 | /** |
||
410 | * Gets the HTML for displaying the payment method on a page. |
||
411 | * |
||
412 | * @param string $url |
||
413 | * @param string $css_class |
||
414 | * @return string of HTML for displaying the button |
||
415 | * @throws \EE_Error |
||
416 | */ |
||
417 | public function button_html( $url = '', $css_class = '' ){ |
||
427 | |||
428 | |||
429 | |||
430 | /** |
||
431 | * Gets all the currencies which are an option for this payment method |
||
432 | * (as defined by the gateway and the currently active currencies) |
||
433 | * |
||
434 | * @return EE_Currency[] |
||
435 | * @throws \EE_Error |
||
436 | */ |
||
437 | public function get_all_usable_currencies(){ |
||
440 | |||
441 | |||
442 | |||
443 | /** |
||
444 | * Reports whether or not this payment method can be used for this payment method |
||
445 | * |
||
446 | * @param string $currency_code currency ID (code) |
||
447 | * @return boolean |
||
448 | * @throws \EE_Error |
||
449 | */ |
||
450 | public function usable_for_currency( $currency_code ) { |
||
458 | |||
459 | |||
460 | |||
461 | /** |
||
462 | * Returns TRUE if this payment method's gateway is an instance of EE_Onsite_Gateway |
||
463 | * |
||
464 | * @return bool |
||
465 | * @throws \EE_Error |
||
466 | */ |
||
467 | public function is_on_site(){ |
||
468 | return $this->type_obj()->payment_occurs() === EE_PMT_Base::onsite; |
||
469 | } |
||
470 | |||
471 | |||
472 | |||
473 | /** |
||
474 | * Returns TRUE if this payment method's gateway is an instance of EE_Offsite_Gateway |
||
475 | * |
||
476 | * @return bool |
||
477 | * @throws \EE_Error |
||
478 | */ |
||
479 | public function is_off_site(){ |
||
480 | return $this->type_obj()->payment_occurs() === EE_PMT_Base::offsite; |
||
481 | } |
||
482 | |||
483 | |||
484 | |||
485 | /** |
||
486 | * Returns TRUE if this payment method does not utilize a gateway |
||
487 | * |
||
488 | * @return bool |
||
489 | * @throws \EE_Error |
||
490 | */ |
||
491 | public function is_off_line(){ |
||
494 | |||
495 | /** |
||
496 | * Overrides default __sleep so the object type is NOT cached. |
||
497 | * This way we can rely on the normal EE_Payment_Method::type_obj() logic |
||
498 | * to load the required classes, and don't need them at the time of unserialization |
||
499 | * @return array |
||
500 | */ |
||
501 | public function __sleep(){ |
||
502 | $properties = get_object_vars( $this ); |
||
503 | unset( $properties[ '_type_obj' ] ); |
||
504 | return array_keys( $properties ); |
||
505 | } |
||
506 | |||
507 | |||
508 | |||
509 | /** |
||
510 | * Overrides parent to add some logging for when payment methods get deactivated |
||
511 | * |
||
512 | * @param array $set_cols_n_values |
||
513 | * @return int @see EE_Base_Class::save() |
||
514 | * @throws \EE_Error |
||
515 | */ |
||
516 | public function save( $set_cols_n_values = array() ) { |
||
517 | $results = parent::save( $set_cols_n_values ); |
||
518 | if( $this->get_original( 'PMD_scope' ) !== $this->get( 'PMD_scope' ) ) { |
||
519 | EE_Log::instance()->log( |
||
520 | __FILE__, |
||
521 | __FUNCTION__, |
||
522 | sprintf( |
||
523 | __( 'Set new scope on payment method %1$s to %2$s from %3$s on URL %4$s', 'event_espresso' ), |
||
524 | $this->name(), |
||
525 | serialize( $this->get_original( 'PMD_scope' ) ), |
||
526 | serialize( $this->get( 'PMD_scope' ) ), |
||
534 | |||
535 | } |
||
536 |
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.