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 Shipping_Zone 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 Shipping_Zone, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Shipping_Zone { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var WC_Shipping_Zone |
||
| 15 | */ |
||
| 16 | private $wc_shipping_zone; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var Shipping_Method[] |
||
| 20 | */ |
||
| 21 | private $methods; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | private $country_code; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string[] |
||
| 30 | */ |
||
| 31 | private $regions = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string[] |
||
| 35 | */ |
||
| 36 | private $postal_codes = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string[] |
||
| 40 | */ |
||
| 41 | private $postal_code_prefixes = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string[] |
||
| 45 | */ |
||
| 46 | private $postal_code_ranges = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Shipping_Zone constructor. |
||
| 50 | * |
||
| 51 | * @param WC_Shipping_Zone $wc_shipping_zone |
||
| 52 | * @param string $country_code |
||
| 53 | */ |
||
| 54 | public function __construct( $wc_shipping_zone, $country_code ) { |
||
| 62 | |||
| 63 | private function load_zone_locations() { |
||
| 100 | |||
| 101 | private function load_methods() { |
||
| 107 | |||
| 108 | public function add_available_delivery_method( &$jsonld ) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param array $jsonld |
||
| 120 | * @param Product $product |
||
| 121 | */ |
||
| 122 | public function add_offer_shipping_details( &$jsonld, $product ) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param array $jsonld |
||
| 145 | * @param Product $product |
||
| 146 | */ |
||
| 147 | // private function add_shipping_details_when_no_shipping_methods( &$jsonld, $product ) { |
||
| 148 | // |
||
| 149 | // $offer_shipping_details = array( '@type' => 'OfferShippingDetails', ); |
||
| 150 | // |
||
| 151 | // $this->add_shipping_destination( $offer_shipping_details ); |
||
| 152 | // |
||
| 153 | // /* |
||
| 154 | // * Use Case UC004 |
||
| 155 | // */ |
||
| 156 | // $shipping_delivery_time = array( '@type' => 'ShippingDeliveryTime', ); |
||
| 157 | // $product->add_handling_time( $offer_shipping_details['deliveryTime'] ); |
||
| 158 | // |
||
| 159 | // $this->add_cutoff_time( $shipping_delivery_time ); |
||
| 160 | // |
||
| 161 | // if ( 1 < count( $shipping_delivery_time ) ) { |
||
| 162 | // $offer_shipping_details['shippingDeliveryTime'] = $shipping_delivery_time; |
||
| 163 | // } |
||
| 164 | // |
||
| 165 | // $jsonld['shippingDetails'][] = $offer_shipping_details; |
||
| 166 | // |
||
| 167 | // } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param array $jsonld |
||
| 171 | * @param Product $product |
||
| 172 | * @param Shipping_Method $method |
||
| 173 | */ |
||
| 174 | private function add_shipping_details_with_shipping_method( &$jsonld, $product, $method = null ) { |
||
| 205 | |||
| 206 | private function make_sure_shipping_details_exists_and_it_is_an_array( &$jsonld ) { |
||
| 218 | |||
| 219 | private function add_shipping_destination( &$shipping_details ) { |
||
| 239 | |||
| 240 | private function add_address_region( &$shipping_destination ) { |
||
| 249 | |||
| 250 | private function add_postal_code( &$shipping_destination ) { |
||
| 259 | |||
| 260 | private function add_postal_code_prefix( &$shipping_destination ) { |
||
| 271 | |||
| 272 | private function add_postal_code_range( &$shipping_destination ) { |
||
| 289 | |||
| 290 | public static function from_wc_shipping_zone( $wc_shipping_zone, $country_code = null ) { |
||
| 294 | |||
| 295 | private function add_cutoff_time( &$shipping_delivery_time ) { |
||
| 313 | |||
| 314 | private function add_business_days( &$shipping_delivery_time ) { |
||
| 341 | |||
| 342 | } |
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.