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_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() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Returns all supported currencies for this payment method. |
||
| 154 | * |
||
| 155 | * @since 4.0.0 |
||
| 156 | * @version 4.0.0 |
||
| 157 | * @return array |
||
| 158 | */ |
||
| 159 | public function get_supported_currency() { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Checks to see if all criteria is met before showing payment method. |
||
| 167 | * |
||
| 168 | * @since 4.0.0 |
||
| 169 | * @version 4.0.0 |
||
| 170 | * @return bool |
||
| 171 | */ |
||
| 172 | public function is_available() { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get_icon function. |
||
| 182 | * |
||
| 183 | * @since 1.0.0 |
||
| 184 | * @version 4.0.0 |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function get_icon() { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * payment_scripts function. |
||
| 199 | * |
||
| 200 | * Outputs scripts used for stripe payment |
||
| 201 | * |
||
| 202 | * @access public |
||
| 203 | */ |
||
| 204 | public function payment_scripts() { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Initialize Gateway Settings Form Fields. |
||
| 215 | */ |
||
| 216 | public function init_form_fields() { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Payment form on checkout page |
||
| 222 | */ |
||
| 223 | public function payment_fields() { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Output for the order received page. |
||
| 254 | * |
||
| 255 | * @param int $order_id |
||
| 256 | */ |
||
| 257 | public function thankyou_page( $order_id ) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Add content to the WC emails. |
||
| 263 | * |
||
| 264 | * @since 4.0.0 |
||
| 265 | * @version 4.0.0 |
||
| 266 | * @param WC_Order $order |
||
| 267 | * @param bool $sent_to_admin |
||
| 268 | * @param bool $plain_text |
||
| 269 | */ |
||
| 270 | public function email_instructions( $order, $sent_to_admin, $plain_text = false ) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Gets the Bitcoin instructions for customer to pay. |
||
| 282 | * |
||
| 283 | * @since 4.0.0 |
||
| 284 | * @version 4.0.0 |
||
| 285 | * @param int $order_id |
||
| 286 | */ |
||
| 287 | public function get_instructions( $order_id, $plain_text = false ) { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Saves Bitcoin information to the order meta for later use. |
||
| 329 | * |
||
| 330 | * @since 4.0.0 |
||
| 331 | * @version 4.0.0 |
||
| 332 | * @param object $order |
||
| 333 | * @param object $source_object |
||
| 334 | */ |
||
| 335 | public function save_instructions( $order, $source_object ) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Process the payment |
||
| 349 | * |
||
| 350 | * @param int $order_id Reference. |
||
| 351 | * @param bool $retry Should we retry on fail. |
||
| 352 | * @param bool $force_save_source Force save the payment source. |
||
| 353 | * |
||
| 354 | * @throws Exception If payment will not be accepted. |
||
| 355 | * |
||
| 356 | * @return array|void |
||
| 357 | */ |
||
| 358 | public function process_payment( $order_id, $retry = true, $force_save_source = false ) { |
||
| 414 | } |
||
| 415 |
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.