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 Give_Stripe_Gateway 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_Stripe_Gateway, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Give_Stripe_Gateway { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Default Gateway ID. |
||
| 29 | * |
||
| 30 | * @since 2.5.0 |
||
| 31 | * @access public |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | public $id = ''; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Set Latest Stripe Version. |
||
| 39 | * |
||
| 40 | * @since 2.5.0 |
||
| 41 | * @access public |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | public $api_version = '2019-03-14'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Secret API Key. |
||
| 49 | * |
||
| 50 | * @access private |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private $secret_key = ''; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Payment Intent. |
||
| 58 | * |
||
| 59 | * @since 2.5.0 |
||
| 60 | * @access public |
||
| 61 | * |
||
| 62 | * @var \Stripe\PaymentIntent |
||
| 63 | */ |
||
| 64 | public $payment_intent; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Give_Stripe_Gateway constructor. |
||
| 68 | * |
||
| 69 | * @since 2.5.0 |
||
| 70 | * @access public |
||
| 71 | * |
||
| 72 | * @return bool|void |
||
|
|
|||
| 73 | */ |
||
| 74 | public function __construct() { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * This function will help to set the latest Stripe API version. |
||
| 104 | * |
||
| 105 | * @since 2.5.0 |
||
| 106 | * @access public |
||
| 107 | * |
||
| 108 | * @return void |
||
| 109 | */ |
||
| 110 | View Code Duplication | public function set_api_version() { |
|
| 142 | |||
| 143 | /** |
||
| 144 | * This function will help you to set API Key and its related errors are shown. |
||
| 145 | * |
||
| 146 | * @since 2.5.0 |
||
| 147 | * @access public |
||
| 148 | * |
||
| 149 | * @return void |
||
| 150 | */ |
||
| 151 | public function set_api_key() { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Send back to checkout based on the gateway id. |
||
| 157 | * |
||
| 158 | * @since 2.5.0 |
||
| 159 | * @access public |
||
| 160 | * |
||
| 161 | * @return void |
||
| 162 | */ |
||
| 163 | public function send_back_to_checkout() { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * This function will be used to fetch token details from token id. |
||
| 169 | * |
||
| 170 | * @param string $id Stripe Token ID. |
||
| 171 | * @param array $args Additional arguments. |
||
| 172 | * |
||
| 173 | * @since 2.5.0 |
||
| 174 | * @access public |
||
| 175 | * |
||
| 176 | * @return \Stripe\Token |
||
| 177 | */ |
||
| 178 | public function get_token_details( $id, $args = array() ) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * This function will be used to fetch source details from source id. |
||
| 212 | * |
||
| 213 | * @param string $id Stripe Source ID. |
||
| 214 | * |
||
| 215 | * @since 2.5.0 |
||
| 216 | * @access public |
||
| 217 | * |
||
| 218 | * @return \Stripe\Source |
||
| 219 | */ |
||
| 220 | View Code Duplication | public function get_source_details( $id ) { |
|
| 249 | |||
| 250 | /** |
||
| 251 | * This function will prepare source based on the parameters provided. |
||
| 252 | * |
||
| 253 | * @param array $args List of arguments \Stripe\Source::create() supports. |
||
| 254 | * |
||
| 255 | * @since 2.5.0 |
||
| 256 | * @access public |
||
| 257 | * |
||
| 258 | * @return \Stripe\Source |
||
| 259 | */ |
||
| 260 | View Code Duplication | public function prepare_source( $args ) { |
|
| 289 | |||
| 290 | /** |
||
| 291 | * This function will add hidden source field. |
||
| 292 | * |
||
| 293 | * @param int $form_id Donation Form ID. |
||
| 294 | * @param array $args List of arguments. |
||
| 295 | * |
||
| 296 | * @since 2.5.0 |
||
| 297 | * @access public |
||
| 298 | */ |
||
| 299 | public function add_hidden_source_field( $form_id, $args ) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Get Customer's card. |
||
| 315 | * |
||
| 316 | * @param \Stripe\Customer $stripe_customer Stripe Customer Object. |
||
| 317 | * @param string $id Source or Token ID. |
||
| 318 | * |
||
| 319 | * @since 2.5.0 |
||
| 320 | * |
||
| 321 | * @return \Stripe\Source|bool |
||
| 322 | */ |
||
| 323 | public function get_customer_card( $stripe_customer, $id ) { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Save Stripe Customer ID. |
||
| 388 | * |
||
| 389 | * @param string $stripe_customer_id Customer ID. |
||
| 390 | * @param int $payment_id Payment ID. |
||
| 391 | * |
||
| 392 | * @since 2.5.0 |
||
| 393 | */ |
||
| 394 | public function save_stripe_customer_id( $stripe_customer_id, $payment_id ) { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Log a Stripe Error. |
||
| 418 | * |
||
| 419 | * Logs in the Give db the error and also displays the error message to the donor. |
||
| 420 | * |
||
| 421 | * @param \Stripe\Error\Base|\Stripe\Error\Card $exception Exception. |
||
| 422 | * |
||
| 423 | * @since 2.5.0 |
||
| 424 | * |
||
| 425 | * @return bool |
||
| 426 | */ |
||
| 427 | public function log_error( $exception ) { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Format currency for Stripe. |
||
| 467 | * |
||
| 468 | * @see https://support.stripe.com/questions/which-zero-decimal-currencies-does-stripe-support |
||
| 469 | * |
||
| 470 | * @param float $amount Donation amount. |
||
| 471 | * |
||
| 472 | * @return mixed |
||
| 473 | */ |
||
| 474 | public function format_amount( $amount ) { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Verify Payment. |
||
| 486 | * |
||
| 487 | * @param int $payment_id Payment ID. |
||
| 488 | * @param string $stripe_customer_id Customer ID. |
||
| 489 | * @param \Stripe\Charge $charge Stripe Charge Object. |
||
| 490 | */ |
||
| 491 | public function verify_payment( $payment_id, $stripe_customer_id, $charge ) { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * This function will prepare metadata to send to Stripe. |
||
| 529 | * |
||
| 530 | * @param int $donation_id Donation ID. |
||
| 531 | * |
||
| 532 | * @since 2.5.0 |
||
| 533 | * @access public |
||
| 534 | * |
||
| 535 | * @return array |
||
| 536 | */ |
||
| 537 | public function prepare_metadata( $donation_id = 0 ) { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * This function will help to charge with Stripe. |
||
| 576 | * |
||
| 577 | * @param int $donation_id Donation ID with pending status. |
||
| 578 | * @param array $charge_args List of charge arguments. |
||
| 579 | * |
||
| 580 | * @since 2.5.0 |
||
| 581 | * @access public |
||
| 582 | * |
||
| 583 | * @return \Stripe\Charge |
||
| 584 | */ |
||
| 585 | public function create_charge( $donation_id, $charge_args ) { |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Create Source for Stripe 3D Secure Payments. |
||
| 635 | * |
||
| 636 | * @param int $donation_id Donation ID. |
||
| 637 | * @param int $source_id Source ID/Object. |
||
| 638 | * |
||
| 639 | * @since 1.6 |
||
| 640 | * @access public |
||
| 641 | * |
||
| 642 | * @return bool|\Stripe\Source |
||
| 643 | */ |
||
| 644 | public function create_3d_secure_source( $donation_id, $source_id ) { |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Is 3D secure payment required? |
||
| 684 | * |
||
| 685 | * @param \Stripe\Source $source_object Stripe Source Object. |
||
| 686 | * |
||
| 687 | * @since 1.6 |
||
| 688 | * @access public |
||
| 689 | * |
||
| 690 | * @return bool |
||
| 691 | */ |
||
| 692 | public function is_3d_secure_required( $source_object ) { |
||
| 710 | } |
||
| 711 | } |
||
| 712 |
Adding a
@returnannotation 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.