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:
| 1 | <?php |
||
| 11 | class WC_Stripe_Helper { |
||
| 12 | /** |
||
| 13 | * Get Stripe amount to pay |
||
| 14 | * |
||
| 15 | * @param float $total Amount due. |
||
| 16 | * @param string $currency Accepted currency. |
||
| 17 | * |
||
| 18 | * @return float|int |
||
| 19 | */ |
||
| 20 | public static function get_stripe_amount( $total, $currency = '' ) { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Localize Stripe messages based on code |
||
| 34 | * |
||
| 35 | * @since 3.0.6 |
||
| 36 | * @version 3.0.6 |
||
| 37 | * @return array |
||
| 38 | */ |
||
| 39 | public static function get_localized_messages() { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * List of currencies supported by Stripe that has no decimals. |
||
| 58 | * |
||
| 59 | * @return array $currencies |
||
| 60 | */ |
||
| 61 | public static function no_decimal_currencies() { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Stripe uses smallest denomination in currencies such as cents. |
||
| 83 | * We need to format the returned currency from Stripe into human readable form. |
||
| 84 | * The amount is not used in any calculations so returning string is sufficient. |
||
| 85 | * |
||
| 86 | * @param object $balance_transaction |
||
| 87 | * @param string $type Type of number to format |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | public static function format_balance_fee( $balance_transaction, $type = 'fee' ) { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Checks Stripe minimum order value authorized per currency |
||
| 112 | */ |
||
| 113 | public static function get_minimum_amount() { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Gets all the saved setting options from a specific method. |
||
| 153 | * If specific setting is passed, only return that. |
||
| 154 | * |
||
| 155 | * @since 4.0.0 |
||
| 156 | * @version 4.0.0 |
||
| 157 | * @param string $method The payment method to get the settings from. |
||
| 158 | * @param string $setting The name of the setting to get. |
||
| 159 | */ |
||
| 160 | public static function get_settings( $method = null, $setting = null ) { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Check if WC version is pre 3.0. |
||
| 172 | * |
||
| 173 | * @since 4.0.0 |
||
| 174 | * @version 4.0.0 |
||
| 175 | * @return bool |
||
| 176 | */ |
||
| 177 | public static function is_pre_30() { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Gets the webhook URL for Stripe triggers. Used mainly for |
||
| 183 | * asyncronous redirect payment methods in which statuses are |
||
| 184 | * not immediately chargeable. |
||
| 185 | * |
||
| 186 | * @since 4.0.0 |
||
| 187 | * @version 4.0.0 |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public static function get_webhook_url() { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Gets the order by Stripe source ID. |
||
| 196 | * |
||
| 197 | * @since 4.0.0 |
||
| 198 | * @version 4.0.0 |
||
| 199 | * @param string $source_id |
||
| 200 | */ |
||
| 201 | View Code Duplication | public static function get_order_by_source_id( $source_id ) { |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Gets the order by Stripe charge ID. |
||
| 215 | * |
||
| 216 | * @since 4.0.0 |
||
| 217 | * @version 4.0.0 |
||
| 218 | * @param string $charge_id |
||
| 219 | */ |
||
| 220 | View Code Duplication | public static function get_order_by_charge_id( $charge_id ) { |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Sanitize statement descriptor text. |
||
| 234 | * |
||
| 235 | * Stripe requires max of 22 characters and no |
||
| 236 | * special characters with ><"'. |
||
| 237 | * |
||
| 238 | * @since 4.0.0 |
||
| 239 | * @param string $statement_descriptor |
||
| 240 | * @return string $statement_descriptor Sanitized statement descriptor |
||
| 241 | */ |
||
| 242 | public static function clean_statement_descriptor( $statement_descriptor = '' ) { |
||
| 252 | } |
||
| 253 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.