Complex classes like WC_Gateway_Paypal_Request 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_Gateway_Paypal_Request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class WC_Gateway_Paypal_Request { |
||
16 | |||
17 | /** |
||
18 | * Stores line items to send to PayPal. |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $line_items = array(); |
||
23 | |||
24 | /** |
||
25 | * Pointer to gateway making the request. |
||
26 | * |
||
27 | * @var WC_Gateway_Paypal |
||
28 | */ |
||
29 | protected $gateway; |
||
30 | |||
31 | /** |
||
32 | * Endpoint for requests from PayPal. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $notify_url; |
||
37 | |||
38 | /** |
||
39 | * Endpoint for requests to PayPal. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $endpoint; |
||
44 | |||
45 | |||
46 | /** |
||
47 | * Constructor. |
||
48 | * |
||
49 | * @param WC_Gateway_Paypal $gateway Paypal gateway object. |
||
50 | */ |
||
51 | public function __construct( $gateway ) { |
||
55 | |||
56 | /** |
||
57 | * Get the PayPal request URL for an order. |
||
58 | * |
||
59 | * @param WC_Order $order Order object. |
||
60 | * @param bool $sandbox Whether to use sandbox mode or not. |
||
61 | * @return string |
||
62 | */ |
||
63 | public function get_request_url( $order, $sandbox = false ) { |
||
88 | |||
89 | /** |
||
90 | * Limit length of an arg. |
||
91 | * |
||
92 | * @param string $string Argument to limit. |
||
93 | * @param integer $limit Limit size in characters. |
||
94 | * @return string |
||
95 | */ |
||
96 | protected function limit_length( $string, $limit = 127 ) { |
||
97 | $str_limit = $limit - 3; |
||
98 | if ( function_exists( 'mb_strimwidth' ) ) { |
||
99 | if ( mb_strlen( $string ) > $limit ) { |
||
100 | $string = mb_strimwidth( $string, 0, $str_limit ) . '...'; |
||
101 | } |
||
102 | } else { |
||
103 | if ( strlen( $string ) > $limit ) { |
||
104 | $string = substr( $string, 0, $str_limit ) . '...'; |
||
105 | } |
||
106 | } |
||
107 | return $string; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Get transaction args for paypal request, except for line item args. |
||
112 | * |
||
113 | * @param WC_Order $order Order object. |
||
114 | * @return array |
||
115 | */ |
||
116 | protected function get_transaction_args( $order ) { |
||
153 | |||
154 | /** |
||
155 | * If the default request with line items is too long, generate a new one with only one line item. |
||
156 | * |
||
157 | * If URL is longer than 2,083 chars, ignore line items and send cart to Paypal as a single item. |
||
158 | * One item's name can only be 127 characters long, so the URL should not be longer than limit. |
||
159 | * URL character limit via: |
||
160 | * https://support.microsoft.com/en-us/help/208427/maximum-url-length-is-2-083-characters-in-internet-explorer. |
||
161 | * |
||
162 | * @param WC_Order $order Order to be sent to Paypal. |
||
163 | * @param array $paypal_args Arguments sent to Paypal in the request. |
||
164 | * @return array |
||
165 | */ |
||
166 | protected function fix_request_length( $order, $paypal_args ) { |
||
184 | |||
185 | /** |
||
186 | * Get PayPal Args for passing to PP. |
||
187 | * |
||
188 | * @param WC_Order $order Order object. |
||
189 | * @return array |
||
190 | */ |
||
191 | protected function get_paypal_args( $order ) { |
||
211 | |||
212 | /** |
||
213 | * Get phone number args for paypal request. |
||
214 | * |
||
215 | * @param WC_Order $order Order object. |
||
216 | * @return array |
||
217 | */ |
||
218 | protected function get_phone_number_args( $order ) { |
||
243 | |||
244 | /** |
||
245 | * Get shipping args for paypal request. |
||
246 | * |
||
247 | * @param WC_Order $order Order object. |
||
248 | * @return array |
||
249 | */ |
||
250 | protected function get_shipping_args( $order ) { |
||
271 | |||
272 | /** |
||
273 | * Get shipping cost line item args for paypal request. |
||
274 | * |
||
275 | * @param WC_Order $order Order object. |
||
276 | * @param bool $force_one_line_item Whether one line item was forced by validation or URL length. |
||
277 | * @return array |
||
278 | */ |
||
279 | protected function get_shipping_cost_line_item( $order, $force_one_line_item ) { |
||
298 | |||
299 | /** |
||
300 | * Get line item args for paypal request as a single line item. |
||
301 | * |
||
302 | * @param WC_Order $order Order object. |
||
303 | * @return array |
||
304 | */ |
||
305 | protected function get_line_item_args_single_item( $order ) { |
||
314 | |||
315 | /** |
||
316 | * Get line item args for paypal request. |
||
317 | * |
||
318 | * @param WC_Order $order Order object. |
||
319 | * @param bool $force_one_line_item Create only one item for this order. |
||
320 | * @return array |
||
321 | */ |
||
322 | protected function get_line_item_args( $order, $force_one_line_item = false ) { |
||
350 | |||
351 | /** |
||
352 | * Get order item names as a string. |
||
353 | * |
||
354 | * @param WC_Order $order Order object. |
||
355 | * @return string |
||
356 | */ |
||
357 | protected function get_order_item_names( $order ) { |
||
384 | |||
385 | /** |
||
386 | * Get order item names as a string. |
||
387 | * |
||
388 | * @param WC_Order $order Order object. |
||
389 | * @param WC_Order_Item $item Order item object. |
||
390 | * @return string |
||
391 | */ |
||
392 | protected function get_order_item_name( $order, $item ) { |
||
413 | |||
414 | /** |
||
415 | * Return all line items. |
||
416 | */ |
||
417 | protected function get_line_items() { |
||
420 | |||
421 | /** |
||
422 | * Remove all line items. |
||
423 | */ |
||
424 | protected function delete_line_items() { |
||
427 | |||
428 | /** |
||
429 | * Check if the order has valid line items to use for PayPal request. |
||
430 | * |
||
431 | * The line items are invalid in case of mismatch in totals or if any amount < 0. |
||
432 | * |
||
433 | * @param WC_Order $order Order to be examined. |
||
434 | * @return bool |
||
435 | */ |
||
436 | protected function line_items_valid( $order ) { |
||
457 | |||
458 | /** |
||
459 | * Get line items to send to paypal. |
||
460 | * |
||
461 | * @param WC_Order $order Order object. |
||
462 | */ |
||
463 | protected function prepare_line_items( $order ) { |
||
479 | |||
480 | /** |
||
481 | * Add PayPal Line Item. |
||
482 | * |
||
483 | * @param string $item_name Item name. |
||
484 | * @param int $quantity Item quantity. |
||
485 | * @param float $amount Amount. |
||
486 | * @param string $item_number Item number. |
||
487 | */ |
||
488 | protected function add_line_item( $item_name, $quantity = 1, $amount = 0.0, $item_number = '' ) { |
||
510 | |||
511 | /** |
||
512 | * Get the state to send to paypal. |
||
513 | * |
||
514 | * @param string $cc Country two letter code. |
||
515 | * @param string $state State code. |
||
516 | * @return string |
||
517 | */ |
||
518 | protected function get_paypal_state( $cc, $state ) { |
||
531 | |||
532 | /** |
||
533 | * Check if currency has decimals. |
||
534 | * |
||
535 | * @param string $currency Currency to check. |
||
536 | * @return bool |
||
537 | */ |
||
538 | protected function currency_has_decimals( $currency ) { |
||
545 | |||
546 | /** |
||
547 | * Round prices. |
||
548 | * |
||
549 | * @param double $price Price to round. |
||
550 | * @param WC_Order $order Order object. |
||
551 | * @return double |
||
552 | */ |
||
553 | protected function round( $price, $order ) { |
||
562 | |||
563 | /** |
||
564 | * Format prices. |
||
565 | * |
||
566 | * @param float|int $price Price to format. |
||
567 | * @param WC_Order $order Order object. |
||
568 | * @return string |
||
569 | */ |
||
570 | protected function number_format( $price, $order ) { |
||
579 | } |
||
580 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: