Complex classes like WC_Gateway_Stripe_Bitcoin 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_Bitcoin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class WC_Gateway_Stripe_Bitcoin 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 | * Instructions for Bitcoin payment. |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | public $instructions; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Constructor |
||
| 64 | */ |
||
| 65 | public function __construct() { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Checks to make sure environment is setup correctly to use this payment method. |
||
| 108 | * |
||
| 109 | * @since 4.0.0 |
||
| 110 | * @version 4.0.0 |
||
| 111 | */ |
||
| 112 | public function check_environment() { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Checks the environment for compatibility problems. Returns a string with the first incompatibility |
||
| 132 | * found or false if the environment has no problems. |
||
| 133 | * |
||
| 134 | * @since 4.0.0 |
||
| 135 | * @version 4.0.0 |
||
| 136 | */ |
||
| 137 | public function get_environment_warning() { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Returns all supported currencies for this payment method. |
||
| 149 | * |
||
| 150 | * @since 4.0.0 |
||
| 151 | * @version 4.0.0 |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | 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 | * Outputs scripts used for stripe payment |
||
| 209 | * |
||
| 210 | * @access public |
||
| 211 | */ |
||
| 212 | public function payment_scripts() { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Initialize Gateway Settings Form Fields. |
||
| 223 | */ |
||
| 224 | public function init_form_fields() { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Payment form on checkout page |
||
| 230 | */ |
||
| 231 | public function payment_fields() { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Output for the order received page. |
||
| 262 | * |
||
| 263 | * @param int $order_id |
||
| 264 | */ |
||
| 265 | public function thankyou_page( $order_id ) { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Add content to the WC emails. |
||
| 271 | * |
||
| 272 | * @since 4.0.0 |
||
| 273 | * @version 4.0.0 |
||
| 274 | * @param WC_Order $order |
||
| 275 | * @param bool $sent_to_admin |
||
| 276 | * @param bool $plain_text |
||
| 277 | */ |
||
| 278 | public function email_instructions( $order, $sent_to_admin, $plain_text = false ) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Gets the Bitcoin instructions for customer to pay. |
||
| 290 | * |
||
| 291 | * @since 4.0.0 |
||
| 292 | * @version 4.0.0 |
||
| 293 | * @param int $order_id |
||
| 294 | */ |
||
| 295 | public function get_instructions( $order_id, $plain_text = false ) { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Saves Bitcoin information to the order meta for later use. |
||
| 337 | * |
||
| 338 | * @since 4.0.0 |
||
| 339 | * @version 4.0.0 |
||
| 340 | * @param object $order |
||
| 341 | * @param object $source_object |
||
| 342 | */ |
||
| 343 | public function save_instructions( $order, $source_object ) { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Process the payment |
||
| 357 | * |
||
| 358 | * @param int $order_id Reference. |
||
| 359 | * @param bool $retry Should we retry on fail. |
||
| 360 | * @param bool $force_save_source Force save the payment source. |
||
| 361 | * |
||
| 362 | * @throws Exception If payment will not be accepted. |
||
| 363 | * |
||
| 364 | * @return array|void |
||
| 365 | */ |
||
| 366 | public function process_payment( $order_id, $retry = true, $force_save_source = false ) { |
||
| 425 | } |
||
| 426 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.