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 EE_PMT_Base 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_PMT_Base, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | abstract class EE_PMT_Base{ |
||
18 | |||
19 | const onsite = 'on-site'; |
||
20 | const offsite = 'off-site'; |
||
21 | const offline = 'off-line'; |
||
22 | |||
23 | /** |
||
24 | * @var EE_Payment_Method |
||
25 | */ |
||
26 | protected $_pm_instance = NULL; |
||
27 | |||
28 | /** |
||
29 | * @var boolean |
||
30 | */ |
||
31 | protected $_requires_https = FALSE; |
||
32 | |||
33 | /** |
||
34 | * @var boolean |
||
35 | */ |
||
36 | protected $_has_billing_form; |
||
37 | |||
38 | /** |
||
39 | * @var EE_Gateway |
||
40 | */ |
||
41 | protected $_gateway = NULL; |
||
42 | |||
43 | /** |
||
44 | * @var EE_Payment_Method_Form |
||
45 | */ |
||
46 | protected $_settings_form = NULL; |
||
47 | |||
48 | /** |
||
49 | * @var EE_Form_Section_Proper |
||
50 | */ |
||
51 | protected $_billing_form = NULL; |
||
52 | |||
53 | /** |
||
54 | * @var boolean |
||
55 | */ |
||
56 | protected $_cache_billing_form = TRUE; |
||
57 | |||
58 | /** |
||
59 | * String of the absolute path to the folder containing this file, with a trailing slash. |
||
60 | * eg '/public_html/wp-site/wp-content/plugins/event-espresso/payment_methods/Invoice/' |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $_file_folder = NULL; |
||
64 | |||
65 | /** |
||
66 | * String to the absolute URL to this file (useful for getting its web-accessible resources |
||
67 | * like images, js, or css) |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $_file_url = NULL; |
||
71 | |||
72 | /** |
||
73 | * Pretty name for the payment method |
||
74 | * @var string |
||
75 | */ |
||
76 | protected $_pretty_name = NULL; |
||
77 | |||
78 | /** |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | protected $_default_button_url = NULL; |
||
83 | |||
84 | /** |
||
85 | * |
||
86 | * @var string |
||
87 | */ |
||
88 | protected $_default_description = NULL; |
||
89 | |||
90 | |||
91 | /** |
||
92 | * |
||
93 | * @param EE_Payment_Method $pm_instance |
||
94 | * @throws EE_Error |
||
95 | * @return EE_PMT_Base |
||
|
|||
96 | */ |
||
97 | function __construct($pm_instance = NULL) { |
||
128 | |||
129 | |||
130 | |||
131 | /** |
||
132 | * @param boolean $has_billing_form |
||
133 | */ |
||
134 | public function set_has_billing_form( $has_billing_form ) { |
||
135 | $this->_has_billing_form = filter_var( $has_billing_form, FILTER_VALIDATE_BOOLEAN ); |
||
136 | } |
||
137 | |||
138 | |||
139 | |||
140 | /** |
||
141 | * sets the file_folder property |
||
142 | */ |
||
143 | protected function _set_file_folder(){ |
||
144 | $reflector = new ReflectionClass(get_class($this)); |
||
145 | $fn = $reflector->getFileName(); |
||
146 | $this->_file_folder = dirname($fn).DS; |
||
147 | } |
||
148 | |||
149 | |||
150 | |||
151 | /** |
||
152 | * sets the file URL with a trailing slash for this PMT |
||
153 | */ |
||
154 | protected function _set_file_url(){ |
||
155 | $plugins_dir_fixed = str_replace('\\',DS,WP_PLUGIN_DIR); |
||
156 | $file_folder_fixed = str_replace('\\',DS,$this->file_folder()); |
||
157 | $file_path = str_replace($plugins_dir_fixed,WP_PLUGIN_URL,$file_folder_fixed); |
||
158 | $this->_file_url = $file_path; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Gets the default description on all payment methods of this type |
||
163 | * @return string |
||
164 | */ |
||
165 | public function default_description(){ |
||
166 | return $this->_default_description; |
||
167 | } |
||
168 | |||
169 | |||
170 | |||
171 | /** |
||
172 | * Returns the folder containing the PMT child class, with a trailing slash |
||
173 | * @return string |
||
174 | */ |
||
175 | public function file_folder(){ |
||
181 | |||
182 | |||
183 | |||
184 | /** |
||
185 | * @return string |
||
186 | */ |
||
187 | public function file_url(){ |
||
193 | |||
194 | |||
195 | |||
196 | /** |
||
197 | * Sets the payment method instance this payment method type is for. |
||
198 | * Its important teh payment method instance is set before |
||
199 | * @param EE_Payment_Method $payment_method_instance |
||
200 | */ |
||
201 | function set_instance($payment_method_instance){ |
||
212 | |||
213 | |||
214 | |||
215 | /** |
||
216 | * Gets teh form for displaying to admins where they setup the payment method |
||
217 | * @return EE_Payment_Method_Form |
||
218 | */ |
||
219 | function settings_form(){ |
||
232 | |||
233 | |||
234 | |||
235 | /** |
||
236 | * Gets the form for all the settings related to this payment method type |
||
237 | * @return EE_Payment_Method_Form |
||
238 | */ |
||
239 | abstract function generate_new_settings_form(); |
||
240 | |||
241 | |||
242 | |||
243 | |||
244 | |||
245 | |||
246 | /** |
||
247 | * Sets the form for settings. This may be useful if we have already received |
||
248 | * a form submission and have form data it in, and want to use it anytime we're showing |
||
249 | * this payment method type's settings form later in the request |
||
250 | * @param EE_Payment_Method_Form $form |
||
251 | */ |
||
252 | public function set_settings_form($form){ |
||
255 | |||
256 | |||
257 | |||
258 | /** |
||
259 | * @return boolean |
||
260 | */ |
||
261 | public function has_billing_form() { |
||
264 | |||
265 | |||
266 | |||
267 | /** |
||
268 | * Gets the form for displaying to attendees where they can enter their billing info |
||
269 | * which will be sent to teh gateway (can be null) |
||
270 | * |
||
271 | * @param \EE_Transaction $transaction |
||
272 | * @param array $extra_args |
||
273 | * @return \EE_Billing_Attendee_Info_Form|\EE_Billing_Info_Form|null |
||
274 | */ |
||
275 | public function billing_form( EE_Transaction $transaction = NULL, $extra_args = array() ){ |
||
292 | |||
293 | |||
294 | /** |
||
295 | * Creates the billing form for this payment method type |
||
296 | * @param \EE_Transaction $transaction |
||
297 | * @return \EE_Billing_Info_Form |
||
298 | */ |
||
299 | abstract function generate_new_billing_form( EE_Transaction $transaction = NULL ); |
||
300 | |||
301 | |||
302 | |||
303 | /** |
||
304 | * apply_billing_form_debug_settings |
||
305 | * applies debug data to the form |
||
306 | * |
||
307 | * @param \EE_Billing_Info_Form $billing_form |
||
308 | * @return \EE_Billing_Info_Form |
||
309 | */ |
||
310 | public function apply_billing_form_debug_settings( EE_Billing_Info_Form $billing_form ) { |
||
313 | |||
314 | |||
315 | |||
316 | /** |
||
317 | * Sets the billing form for this payment method type. You may want to use this |
||
318 | * if you have form |
||
319 | * @param EE_Payment_Method $form |
||
320 | */ |
||
321 | public function set_billing_form($form){ |
||
324 | |||
325 | |||
326 | |||
327 | /** |
||
328 | * Returns whether or not this payment method requires HTTPS to be used |
||
329 | * @return boolean |
||
330 | */ |
||
331 | function requires_https(){ |
||
334 | |||
335 | |||
336 | |||
337 | /** |
||
338 | * |
||
339 | * @param EE_Transaction $transaction |
||
340 | * @param float $amount |
||
341 | * @param EE_Billing_Info_Form $billing_info |
||
342 | * @param string $return_url |
||
343 | * @param string $fail_url |
||
344 | * @param string $method |
||
345 | * @param bool $by_admin |
||
346 | * @return EE_Payment |
||
347 | * @throws EE_Error |
||
348 | */ |
||
349 | function process_payment( EE_Transaction $transaction, $amount = null, $billing_info = null, $return_url = null,$fail_url = '', $method = 'CART', $by_admin = false ){ |
||
438 | |||
439 | /** |
||
440 | * Gets the values we want to pass onto the gateway. Normally these |
||
441 | * are just the 'pretty' values, but there may be times the data may need |
||
442 | * a little massaging. Proper subsections will become arrays of inputs |
||
443 | * @param EE_Billing_Info_Form $billing_form |
||
444 | * @return array |
||
445 | */ |
||
446 | protected function _get_billing_values_from_form( $billing_form ){ |
||
453 | |||
454 | |||
455 | |||
456 | /** |
||
457 | * Handles an instant payment notification when the transaction is known (by default). |
||
458 | * @param array $req_data |
||
459 | * @param EE_Transaction $transaction |
||
460 | * @return EE_Payment |
||
461 | * @throws EE_Error |
||
462 | */ |
||
463 | public function handle_ipn($req_data,$transaction){ |
||
472 | |||
473 | |||
474 | |||
475 | /** |
||
476 | * Saves the billing info onto the attendee of the primary registrant on this transaction, and |
||
477 | * cleans it first. |
||
478 | * @param EE_Billing_Attendee_Info_Form $billing_form |
||
479 | * @param EE_Transaction $transaction |
||
480 | * @return boolean success |
||
481 | */ |
||
482 | protected function _save_billing_info_to_attendee($billing_form, $transaction){ |
||
500 | |||
501 | |||
502 | |||
503 | /** |
||
504 | * Gets the payment this IPN is for. Children may often want to |
||
505 | * override this to inspect the request |
||
506 | * @param EE_Transaction $transaction |
||
507 | * @param array $req_data |
||
508 | * @return EE_Payment |
||
509 | */ |
||
510 | protected function find_payment_for_ipn( EE_Transaction $transaction, $req_data = array() ){ |
||
513 | |||
514 | |||
515 | |||
516 | /** |
||
517 | * In case generic code cannot provide the payment processor with a specific payment method |
||
518 | * and transaction, it will try calling this method on each activate payment method. |
||
519 | * If the payment method is able to identify the request as being for it, it should fetch |
||
520 | * the payment its for and return it. If not, it should throw an EE_Error to indicate it cannot |
||
521 | * handle the IPN |
||
522 | * @param array $req_data |
||
523 | * @return EE_Payment only if this payment method can find the info its needs from $req_data |
||
524 | * and identifies the IPN as being for this payment method (not just fo ra payment method of this type) |
||
525 | * @throws EE_Error |
||
526 | */ |
||
527 | public function handle_unclaimed_ipn( $req_data = array() ){ |
||
530 | |||
531 | |||
532 | |||
533 | /** |
||
534 | * Logic to be accomplished when the payment attempt is complete. |
||
535 | * Most payment methods don't need to do anything at this point; but some, like Mijireh, do. |
||
536 | * (Mijireh is an offsite gateway which doesn't send an IPN. So when the user returns to EE from |
||
537 | * mijireh, this method needs to be called so the Mijireh PM can ping Mijireh to know the status |
||
538 | * of the payment). Fed a transaction because it's always assumed to be the last payment that |
||
539 | * we're dealing with. Returns that last payment (if there is one) |
||
540 | * |
||
541 | * @param EE_Transaction $transaction |
||
542 | * @return EE_Payment |
||
543 | */ |
||
544 | public function finalize_payment_for($transaction){ |
||
547 | |||
548 | |||
549 | |||
550 | /** |
||
551 | * Whether or not this payment method's gateway supports sending refund requests |
||
552 | * @return boolean |
||
553 | */ |
||
554 | public function supports_sending_refunds(){ |
||
561 | |||
562 | |||
563 | |||
564 | /** |
||
565 | * |
||
566 | * @param EE_Payment $payment |
||
567 | * @param array $refund_info |
||
568 | * @throws EE_Error |
||
569 | * @return EE_Payment |
||
570 | */ |
||
571 | public function process_refund( EE_Payment $payment, $refund_info = array()){ |
||
583 | |||
584 | |||
585 | |||
586 | /** |
||
587 | * Returns one the class's constants onsite,offsite, or offline, depending on this |
||
588 | * payment method's gateway. |
||
589 | * @return string |
||
590 | * @throws EE_Error |
||
591 | */ |
||
592 | public function payment_occurs(){ |
||
603 | |||
604 | |||
605 | |||
606 | /** |
||
607 | * For adding any html output ab ove the payment overview. |
||
608 | * Many gateways won't want ot display anything, so this function just returns an empty string. |
||
609 | * Other gateways may want to override this, such as offline gateways. |
||
610 | * @param EE_Payment $payment |
||
611 | * @return string |
||
612 | */ |
||
613 | public function payment_overview_content(EE_Payment $payment){ |
||
617 | |||
618 | |||
619 | |||
620 | /** |
||
621 | * @return array where keys are the help tab name, |
||
622 | * values are: array { |
||
623 | * @type string $title i18n name for the help tab |
||
624 | * @type string $filename name of the file located in ./help_tabs/ (ie, in a folder next to this file) |
||
625 | * @type array $template_args any arguments you want passed to the template file while rendering. |
||
626 | * Keys will be variable names and values with be their values. |
||
627 | */ |
||
628 | public function help_tabs_config(){ |
||
631 | |||
632 | |||
633 | |||
634 | /** |
||
635 | * The system name for this PMT (eg AIM, Paypal_Pro, Invoice... what gets put into |
||
636 | * the payment method's table's PMT_type column) |
||
637 | * @return string |
||
638 | */ |
||
639 | public function system_name(){ |
||
643 | |||
644 | |||
645 | |||
646 | /** |
||
647 | * A pretty i18n version of the PMT name |
||
648 | * @return string |
||
649 | */ |
||
650 | public function pretty_name(){ |
||
653 | |||
654 | |||
655 | |||
656 | /** |
||
657 | * Gets the default absolute URL to the payment method type's button |
||
658 | * @return string |
||
659 | */ |
||
660 | public function default_button_url(){ |
||
663 | |||
664 | |||
665 | |||
666 | /** |
||
667 | * Gets the gateway used by this payment method (if any) |
||
668 | * @return EE_Gateway |
||
669 | */ |
||
670 | public function get_gateway(){ |
||
673 | |||
674 | |||
675 | |||
676 | /** |
||
677 | * @return string html for the link to a help tab |
||
678 | */ |
||
679 | public function get_help_tab_link(){ |
||
683 | |||
684 | |||
685 | |||
686 | /** |
||
687 | * Returns the name of the help tab for this PMT |
||
688 | * @return string |
||
689 | */ |
||
690 | public function get_help_tab_name(){ |
||
693 | |||
694 | /** |
||
695 | * The name of the wp capability that should be associated with the usage of |
||
696 | * this PMT by an admin |
||
697 | * @return string |
||
698 | */ |
||
699 | public function cap_name(){ |
||
702 | |||
703 | /** |
||
704 | * Called by client code to tell the gateway that if it wants to change |
||
705 | * the transaction or line items or registrations related to teh payment it already |
||
706 | * processed (we think, but possibly not) that now's the time to do it. |
||
707 | * It is expected that gateways will store any info they need for this on the PAY_details, |
||
708 | * or maybe an extra meta value |
||
709 | * @param EE_Payment $payment |
||
710 | * @return void |
||
711 | */ |
||
712 | public function update_txn_based_on_payment( $payment ){ |
||
717 | |||
718 | |||
719 | } |
||
720 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.