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 WC_Stripe_Order_Handler 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 WC_Stripe_Order_Handler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class WC_Stripe_Order_Handler extends WC_Stripe_Payment_Gateway { |
||
12 | private static $_this; |
||
13 | public $retry_interval; |
||
14 | |||
15 | /** |
||
16 | * Constructor. |
||
17 | * |
||
18 | * @since 4.0.0 |
||
19 | * @version 4.0.0 |
||
20 | */ |
||
21 | public function __construct() { |
||
32 | |||
33 | /** |
||
34 | * Public access to instance object. |
||
35 | * |
||
36 | * @since 4.0.0 |
||
37 | * @version 4.0.0 |
||
38 | */ |
||
39 | public static function get_instance() { |
||
42 | |||
43 | /** |
||
44 | * Processes payments. |
||
45 | * Note at this time the original source has already been |
||
46 | * saved to a customer card (if applicable) from process_payment. |
||
47 | * |
||
48 | * @since 4.0.0 |
||
49 | * @since 4.1.8 Add $previous_error parameter. |
||
50 | * @param int $order_id |
||
51 | * @param bool $retry |
||
52 | * @param mix $previous_error Any error message from previous request. |
||
53 | */ |
||
54 | public function process_redirect_payment( $order_id, $retry = true, $previous_error = false ) { |
||
201 | |||
202 | /** |
||
203 | * Processses the orders that are redirected. |
||
204 | * |
||
205 | * @since 4.0.0 |
||
206 | * @version 4.0.0 |
||
207 | */ |
||
208 | public function maybe_process_redirect_order() { |
||
217 | |||
218 | /** |
||
219 | * Capture payment when the order is changed from on-hold to complete or processing. |
||
220 | * |
||
221 | * @since 3.1.0 |
||
222 | * @version 4.0.0 |
||
223 | * @param int $order_id |
||
224 | */ |
||
225 | public function capture_payment( $order_id ) { |
||
226 | $order = wc_get_order( $order_id ); |
||
227 | |||
228 | if ( 'stripe' === ( WC_Stripe_Helper::is_wc_lt( '3.0' ) ? $order->payment_method : $order->get_payment_method() ) ) { |
||
229 | $charge = WC_Stripe_Helper::is_wc_lt( '3.0' ) ? get_post_meta( $order_id, '_transaction_id', true ) : $order->get_transaction_id(); |
||
230 | $captured = WC_Stripe_Helper::is_wc_lt( '3.0' ) ? get_post_meta( $order_id, '_stripe_charge_captured', true ) : $order->get_meta( '_stripe_charge_captured', true ); |
||
231 | $is_stripe_captured = false; |
||
232 | |||
233 | if ( $charge && 'no' === $captured ) { |
||
234 | $order_total = $order->get_total(); |
||
235 | |||
236 | if ( 0 < $order->get_total_refunded() ) { |
||
237 | $order_total = $order_total - $order->get_total_refunded(); |
||
238 | } |
||
239 | |||
240 | // First retrieve charge to see if it has been captured. |
||
241 | $result = WC_Stripe_API::retrieve( 'charges/' . $charge ); |
||
242 | |||
243 | if ( ! empty( $result->error ) ) { |
||
244 | /* translators: error message */ |
||
245 | $order->add_order_note( sprintf( __( 'Unable to capture charge! %s', 'woocommerce-gateway-stripe' ), $result->error->message ) ); |
||
246 | } elseif ( false === $result->captured ) { |
||
247 | $result = WC_Stripe_API::request( array( |
||
248 | 'amount' => WC_Stripe_Helper::get_stripe_amount( $order_total ), |
||
249 | 'expand[]' => 'balance_transaction', |
||
250 | ), 'charges/' . $charge . '/capture' ); |
||
251 | |||
252 | if ( ! empty( $result->error ) ) { |
||
253 | /* translators: error message */ |
||
254 | $order->update_status( 'failed', sprintf( __( 'Unable to capture charge! %s', 'woocommerce-gateway-stripe' ), $result->error->message ) ); |
||
255 | } else { |
||
256 | $is_stripe_captured = true; |
||
257 | } |
||
258 | } elseif ( true === $result->captured ) { |
||
259 | $is_stripe_captured = true; |
||
260 | } |
||
261 | |||
262 | if ( $is_stripe_captured ) { |
||
263 | /* translators: transaction id */ |
||
264 | $order->add_order_note( sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $result->id ) ); |
||
265 | WC_Stripe_Helper::is_wc_lt( '3.0' ) ? update_post_meta( $order_id, '_stripe_charge_captured', 'yes' ) : $order->update_meta_data( '_stripe_charge_captured', 'yes' ); |
||
266 | |||
267 | // Store other data such as fees |
||
268 | WC_Stripe_Helper::is_wc_lt( '3.0' ) ? update_post_meta( $order_id, '_transaction_id', $result->id ) : $order->set_transaction_id( $result->id ); |
||
269 | |||
270 | $this->update_fees( $order, $result->balance_transaction ); |
||
271 | } |
||
272 | |||
273 | // This hook fires when admin manually changes order status to processing or completed. |
||
274 | do_action( 'woocommerce_stripe_process_manual_capture', $order, $result ); |
||
275 | } |
||
276 | } |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Cancel pre-auth on refund/cancellation. |
||
281 | * |
||
282 | * @since 3.1.0 |
||
283 | * @version 4.0.0 |
||
284 | * @param int $order_id |
||
285 | */ |
||
286 | public function cancel_payment( $order_id ) { |
||
296 | } |
||
297 | |||
299 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.