Complex classes like WC_Legacy_Coupon 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_Legacy_Coupon, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | abstract class WC_Legacy_Coupon extends WC_Data { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Magic __isset method for backwards compatibility. Legacy properties which could be accessed directly in the past. |
||
| 22 | * @param string $key |
||
| 23 | * @return bool |
||
| 24 | */ |
||
| 25 | public function __isset( $key ) { |
||
| 26 | $legacy_keys = array( |
||
| 27 | 'id', |
||
| 28 | 'exists', |
||
| 29 | 'coupon_custom_fields', |
||
| 30 | 'type', |
||
| 31 | 'discount_type', |
||
| 32 | 'amount', |
||
| 33 | 'code', |
||
| 34 | 'individual_use', |
||
| 35 | 'product_ids', |
||
| 36 | 'exclude_product_ids', |
||
| 37 | 'usage_limit', |
||
| 38 | 'usage_limit_per_user', |
||
| 39 | 'limit_usage_to_x_items', |
||
| 40 | 'usage_count', |
||
| 41 | 'expiry_date', |
||
| 42 | 'product_categories', |
||
| 43 | 'exclude_product_categories', |
||
| 44 | 'minimum_amount', |
||
| 45 | 'maximum_amount', |
||
| 46 | 'customer_email', |
||
| 47 | ); |
||
| 48 | if ( in_array( $key, $legacy_keys ) ) { |
||
| 49 | return true; |
||
| 50 | } |
||
| 51 | return false; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Magic __get method for backwards compatibility. Maps legacy vars to new getters. |
||
| 56 | * @param string $key |
||
| 57 | * @return mixed |
||
| 58 | */ |
||
| 59 | public function __get( $key ) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Format loaded data as array. |
||
| 139 | * @param string|array $array |
||
| 140 | * @return array |
||
| 141 | */ |
||
| 142 | public function format_array( $array ) { |
||
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * Check if coupon needs applying before tax. |
||
| 157 | * |
||
| 158 | * @return bool |
||
| 159 | */ |
||
| 160 | public function apply_before_tax() { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Check if a coupon enables free shipping. |
||
| 167 | * |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | public function enable_free_shipping() { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Check if a coupon excludes sale items. |
||
| 177 | * |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | public function exclude_sale_items() { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Get discount amount for a cart item. |
||
| 187 | * |
||
| 188 | * @param float $discounting_amount Amount the coupon is being applied to |
||
| 189 | * @param array|null $cart_item Cart item being discounted if applicable |
||
| 190 | * @param boolean $single True if discounting a single qty item, false if its the line |
||
| 191 | * @return float Amount this coupon has discounted |
||
| 192 | */ |
||
| 193 | public function get_discount_amount( $discounting_amount, $cart_item = null, $single = false ) { |
||
| 245 | } |
||
| 246 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.