Complex classes like WC_Gateway_Stripe_Multibanco 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_Stripe_Multibanco, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class WC_Gateway_Stripe_Multibanco extends WC_Stripe_Payment_Gateway { |
||
14 | /** |
||
15 | * Notices (array) |
||
16 | * @var array |
||
17 | */ |
||
18 | public $notices = array(); |
||
19 | |||
20 | /** |
||
21 | * Is test mode active? |
||
22 | * |
||
23 | * @var bool |
||
24 | */ |
||
25 | public $testmode; |
||
26 | |||
27 | /** |
||
28 | * Alternate credit card statement name |
||
29 | * |
||
30 | * @var bool |
||
31 | */ |
||
32 | public $statement_descriptor; |
||
33 | |||
34 | /** |
||
35 | * API access secret key |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | public $secret_key; |
||
40 | |||
41 | /** |
||
42 | * Api access publishable key |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | public $publishable_key; |
||
47 | |||
48 | /** |
||
49 | * Should we store the users credit cards? |
||
50 | * |
||
51 | * @var bool |
||
52 | */ |
||
53 | public $saved_cards; |
||
54 | |||
55 | /** |
||
56 | * Constructor |
||
57 | */ |
||
58 | public function __construct() { |
||
96 | |||
97 | /** |
||
98 | * Returns all supported currencies for this payment method. |
||
99 | * |
||
100 | * @since 4.1.0 |
||
101 | * @version 4.1.0 |
||
102 | * @return array |
||
103 | */ |
||
104 | public function get_supported_currency() { |
||
112 | |||
113 | /** |
||
114 | * Checks to see if all criteria is met before showing payment method. |
||
115 | * |
||
116 | * @since 4.1.0 |
||
117 | * @version 4.1.0 |
||
118 | * @return bool |
||
119 | */ |
||
120 | public function is_available() { |
||
127 | |||
128 | /** |
||
129 | * Get_icon function. |
||
130 | * |
||
131 | * @since 1.0.0 |
||
132 | * @version 4.1.0 |
||
133 | * @return string |
||
134 | */ |
||
135 | public function get_icon() { |
||
136 | $icons = $this->payment_icons(); |
||
137 | |||
138 | $icons_str = ''; |
||
139 | |||
140 | $icons_str .= isset( $icons['multibanco'] ) ? $icons['multibanco'] : ''; |
||
141 | |||
142 | return apply_filters( 'woocommerce_gateway_icon', $icons_str, $this->id ); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * payment_scripts function. |
||
147 | * |
||
148 | * Outputs scripts used for stripe payment |
||
149 | * |
||
150 | * @access public |
||
151 | */ |
||
152 | public function payment_scripts() { |
||
160 | |||
161 | /** |
||
162 | * Initialize Gateway Settings Form Fields. |
||
163 | */ |
||
164 | public function init_form_fields() { |
||
167 | |||
168 | /** |
||
169 | * Payment form on checkout page |
||
170 | */ |
||
171 | public function payment_fields() { |
||
201 | |||
202 | /** |
||
203 | * Output for the order received page. |
||
204 | * |
||
205 | * @param int $order_id |
||
206 | */ |
||
207 | public function thankyou_page( $order_id ) { |
||
210 | |||
211 | /** |
||
212 | * Add content to the WC emails. |
||
213 | * |
||
214 | * @since 4.1.0 |
||
215 | * @version 4.1.0 |
||
216 | * @param WC_Order $order |
||
217 | * @param bool $sent_to_admin |
||
218 | * @param bool $plain_text |
||
219 | */ |
||
220 | public function email_instructions( $order, $sent_to_admin, $plain_text = false ) { |
||
221 | $order_id = $order->get_id(); |
||
222 | |||
223 | $payment_method = $order->get_payment_method(); |
||
224 | |||
225 | if ( ! $sent_to_admin && 'stripe_multibanco' === $payment_method && $order->has_status( 'on-hold' ) ) { |
||
226 | WC_Stripe_Logger::log( 'Sending multibanco email for order #' . $order_id ); |
||
227 | |||
228 | $this->get_instructions( $order_id, $plain_text ); |
||
229 | } |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Gets the Multibanco instructions for customer to pay. |
||
234 | * |
||
235 | * @since 4.1.0 |
||
236 | * @version 4.1.0 |
||
237 | * @param int $order_id |
||
238 | */ |
||
239 | public function get_instructions( $order_id, $plain_text = false ) { |
||
273 | |||
274 | /** |
||
275 | * Saves Multibanco information to the order meta for later use. |
||
276 | * |
||
277 | * @since 4.1.0 |
||
278 | * @version 4.1.0 |
||
279 | * @param object $order |
||
280 | * @param object $source_object |
||
281 | */ |
||
282 | public function save_instructions( $order, $source_object ) { |
||
293 | |||
294 | /** |
||
295 | * Creates the source for charge. |
||
296 | * |
||
297 | * @since 4.1.0 |
||
298 | * @version 4.1.0 |
||
299 | * @param object $order |
||
300 | * @return mixed |
||
301 | */ |
||
302 | public function create_source( $order ) { |
||
320 | |||
321 | /** |
||
322 | * Process the payment |
||
323 | * |
||
324 | * @param int $order_id Reference. |
||
325 | * @param bool $retry Should we retry on fail. |
||
326 | * @param bool $force_save_source Force payment source to be saved. |
||
327 | * |
||
328 | * @throws Exception If payment will not be accepted. |
||
329 | * |
||
330 | * @return array|void |
||
331 | */ |
||
332 | public function process_payment( $order_id, $retry = true, $force_save_source = false ) { |
||
392 | } |
||
393 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.