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_Stripe_Apple_Pay_Registration 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_Stripe_Apple_Pay_Registration, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class WC_Stripe_Apple_Pay_Registration { |
||
| 13 | /** |
||
| 14 | * Enabled. |
||
| 15 | * |
||
| 16 | * @var |
||
| 17 | */ |
||
| 18 | public $stripe_settings; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Main Stripe Enabled. |
||
| 22 | * |
||
| 23 | * @var bool |
||
| 24 | */ |
||
| 25 | public $stripe_enabled; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Do we accept Payment Request? |
||
| 29 | * |
||
| 30 | * @var bool |
||
| 31 | */ |
||
| 32 | public $payment_request; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Apple Pay Domain Set. |
||
| 36 | * |
||
| 37 | * @var bool |
||
| 38 | */ |
||
| 39 | public $apple_pay_domain_set; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Secret Key. |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | public $secret_key; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Stores Apple Pay domain verification issues. |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | public $apple_pay_verify_notice; |
||
| 54 | |||
| 55 | public function __construct() { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Gets the Stripe settings. |
||
| 77 | * |
||
| 78 | * @since 4.0.6 |
||
| 79 | * @param string $setting |
||
| 80 | * @param string default |
||
| 81 | * @return string $setting_value |
||
| 82 | */ |
||
| 83 | public function get_option( $setting = '', $default = '' ) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Gets the Stripe secret key for the current mode. |
||
| 97 | * |
||
| 98 | * @since 4.5.3 |
||
| 99 | * @return string Secret key. |
||
| 100 | */ |
||
| 101 | private function get_secret_key() { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Initializes Apple Pay process on settings page. |
||
| 108 | * |
||
| 109 | * @since 3.1.0 |
||
| 110 | * @version 3.1.0 |
||
| 111 | */ |
||
| 112 | public function init_apple_pay() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Registers the domain with Stripe/Apple Pay |
||
| 126 | * |
||
| 127 | * @since 3.1.0 |
||
| 128 | * @version 3.1.0 |
||
| 129 | * @param string $secret_key |
||
| 130 | */ |
||
| 131 | private function register_domain_with_apple( $secret_key = '' ) { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Updates the Apple Pay domain association file. |
||
| 172 | * |
||
| 173 | * @param bool $force True to create the file if it didn't exist, false for just updating the file if needed. |
||
| 174 | * |
||
| 175 | * @version 4.3.0 |
||
| 176 | * @since 4.3.0 |
||
| 177 | * @return bool True on success, false on failure. |
||
| 178 | */ |
||
| 179 | public function update_domain_association_file( $force = false ) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Processes the Apple Pay domain verification. |
||
| 209 | * |
||
| 210 | * @since 3.1.0 |
||
| 211 | * @version 3.1.0 |
||
| 212 | */ |
||
| 213 | public function verify_domain() { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Conditionally process the Apple Pay domain verification after a new secret key is set. |
||
| 244 | * |
||
| 245 | * @since 4.5.3 |
||
| 246 | * @version 4.5.3 |
||
| 247 | */ |
||
| 248 | public function verify_domain_on_new_secret_key( $prev_settings, $settings ) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Process the Apple Pay domain verification if not already done - otherwise just update the file. |
||
| 262 | * |
||
| 263 | * @since 4.5.3 |
||
| 264 | * @version 4.5.3 |
||
| 265 | */ |
||
| 266 | public function verify_domain_if_needed() { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Display any admin notices to the user. |
||
| 276 | * |
||
| 277 | * @since 4.0.6 |
||
| 278 | */ |
||
| 279 | public function admin_notices() { |
||
| 309 | } |
||
| 310 | |||
| 312 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.