Complex classes like WC_Gateway_Stripe_Alipay 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_Alipay, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class WC_Gateway_Stripe_Alipay 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() { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Checks to make sure environment is setup correctly to use this payment method. |
||
| 97 | * |
||
| 98 | * @since 4.0.0 |
||
| 99 | * @version 4.0.0 |
||
| 100 | */ |
||
| 101 | public function check_environment() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Checks the environment for compatibility problems. Returns a string with the first incompatibility |
||
| 121 | * found or false if the environment has no problems. |
||
| 122 | * |
||
| 123 | * @since 4.0.0 |
||
| 124 | * @version 4.0.0 |
||
| 125 | */ |
||
| 126 | public function get_environment_warning() { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Returns all supported currencies for this payment method. |
||
| 141 | * |
||
| 142 | * @since 4.0.0 |
||
| 143 | * @version 4.0.0 |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | public function get_supported_currency() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Checks to see if all criteria is met before showing payment method. |
||
| 162 | * |
||
| 163 | * @since 4.0.0 |
||
| 164 | * @version 4.0.0 |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | public function is_available() { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * All payment icons that work with Stripe. |
||
| 177 | * |
||
| 178 | * @since 4.0.0 |
||
| 179 | * @version 4.0.0 |
||
| 180 | * @return array |
||
| 181 | */ |
||
| 182 | public function payment_icons() { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get_icon function. |
||
| 190 | * |
||
| 191 | * @since 1.0.0 |
||
| 192 | * @version 4.0.0 |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | public function get_icon() { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Payment_scripts function. |
||
| 207 | * |
||
| 208 | * @since 4.0.0 |
||
| 209 | * @version 4.0.0 |
||
| 210 | */ |
||
| 211 | public function payment_scripts() { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Initialize Gateway Settings Form Fields. |
||
| 222 | */ |
||
| 223 | public function init_form_fields() { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Payment form on checkout page |
||
| 229 | */ |
||
| 230 | public function payment_fields() { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Creates the source for charge. |
||
| 261 | * |
||
| 262 | * @since 4.0.0 |
||
| 263 | * @version 4.0.0 |
||
| 264 | * @param object $order |
||
| 265 | * @return mixed |
||
| 266 | */ |
||
| 267 | public function create_source( $order ) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Process the payment |
||
| 289 | * |
||
| 290 | * @param int $order_id Reference. |
||
| 291 | * @param bool $retry Should we retry on fail. |
||
| 292 | * @param bool $force_save_source Force payment source to be saved. |
||
| 293 | * |
||
| 294 | * @throws Exception If payment will not be accepted. |
||
| 295 | * |
||
| 296 | * @return array|void |
||
| 297 | */ |
||
| 298 | public function process_payment( $order_id, $retry = true, $force_save_save = false ) { |
||
| 353 | } |
||
| 354 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: