Complex classes like Shippo 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 Shippo, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class Shippo extends Model |
||
| 29 | { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * An array of Shippo module settings |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $settings = array(); |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Api model instance |
||
| 39 | * @var \gplcart\modules\shippo\models\Api $api |
||
| 40 | */ |
||
| 41 | protected $api; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Language model instance |
||
| 45 | * @var \gplcart\core\models\Language $language |
||
| 46 | */ |
||
| 47 | protected $language; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * User model class instance |
||
| 51 | * @var \gplcart\core\models\User $user |
||
| 52 | */ |
||
| 53 | protected $user; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Price model class instance |
||
| 57 | * @var \gplcart\core\models\Price $price |
||
| 58 | */ |
||
| 59 | protected $price; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Shipping model class instance |
||
| 63 | * @var \gplcart\core\models\Shipping $shipping |
||
| 64 | */ |
||
| 65 | protected $shipping; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * State model class instance |
||
| 69 | * @var \gplcart\core\models\State $state |
||
| 70 | */ |
||
| 71 | protected $state; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Currency model class instance |
||
| 75 | * @var \gplcart\core\models\Currency $currency |
||
| 76 | */ |
||
| 77 | protected $currency; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Address model class instance |
||
| 81 | * @var \gplcart\core\models\Address $address |
||
| 82 | */ |
||
| 83 | protected $address; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Store model instance |
||
| 87 | * @var \gplcart\core\models\Store $store |
||
| 88 | */ |
||
| 89 | protected $store; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Convertor class instance |
||
| 93 | * @var \gplcart\core\helpers\Convertor $convertor |
||
| 94 | */ |
||
| 95 | protected $convertor; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Session class instance |
||
| 99 | * @var \gplcart\core\helpers\Session $session |
||
| 100 | */ |
||
| 101 | protected $session; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param ShippoApiModel $api |
||
| 105 | * @param LanguageModel $language |
||
| 106 | * @param UserModel $user |
||
| 107 | * @param PriceModel $price |
||
| 108 | * @param CurrencyModel $currency |
||
| 109 | * @param AddressModel $address |
||
| 110 | * @param StoreModel $store |
||
| 111 | * @param StateModel $state |
||
| 112 | * @param ShippingModel $shipping |
||
| 113 | * @param SessionHelper $session |
||
| 114 | * @param ConvertorHelper $convertor |
||
| 115 | */ |
||
| 116 | public function __construct(ShippoApiModel $api, LanguageModel $language, |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Returns an array of carrier names keyed by id |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | public function getCarrierNames() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Returns an array of service names keyed by id |
||
| 150 | * @return array |
||
| 151 | */ |
||
| 152 | public function getServiceNames() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Calculates shipping rates and sets available shipping methods on checkout page |
||
| 159 | * @param array $data |
||
| 160 | */ |
||
| 161 | public function calculate(array &$data) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Validates order shipping rates before an order is created |
||
| 172 | * @param array $order |
||
| 173 | * @param array $options |
||
| 174 | * @param array $result |
||
| 175 | */ |
||
| 176 | public function validate(array &$order, $options, array &$result) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Returns an array of cached rates |
||
| 214 | * @param array|integer $address |
||
| 215 | * @param array $cart |
||
| 216 | * @param array $order |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | public function getRates($address, array $cart, array $order) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Returns a unique cache key for a combination of recipient and sender addresses |
||
| 263 | * @param array $from |
||
| 264 | * @param array $to |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | protected function getCacheKey(array $from, $to) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Sets shipping methods available for the order shipping address |
||
| 277 | * @param array $data |
||
| 278 | * @param array $rates |
||
| 279 | */ |
||
| 280 | protected function setShippingMethodsCheckout(array &$data, array $rates) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Adjust price and label for the given shipping method |
||
| 299 | * @param array $method |
||
| 300 | * @param array $rates |
||
| 301 | * @param string $currency |
||
| 302 | * @return boolean |
||
| 303 | */ |
||
| 304 | protected function setShippingMethod(array &$method, array $rates, $currency) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Returns the default rates if unabled to calculate via Shippo API |
||
| 329 | * @return array |
||
| 330 | */ |
||
| 331 | protected function getDefaultRates() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Converts the system method ID into Shippo's service ID |
||
| 349 | * @param string $system_method_id |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | protected function getShippoServiceId($system_method_id) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Converts an address into Shippo's format |
||
| 359 | * @param array $data |
||
| 360 | * @return array |
||
| 361 | */ |
||
| 362 | protected function getShippoAddress(array $data) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Returns an array of parcel data in Shippo's format |
||
| 417 | * @param array $cart |
||
| 418 | * @param array $order |
||
| 419 | * @return array |
||
| 420 | */ |
||
| 421 | protected function getParcel(array $cart, array $order) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Returns total dimensions of all products in the order |
||
| 438 | * @param array $cart |
||
| 439 | * @param array $order |
||
| 440 | * @return array |
||
| 441 | */ |
||
| 442 | protected function getDimensions(array $cart, array $order) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Sets a source shipping address |
||
| 470 | * @param array $data |
||
| 471 | * @return array |
||
| 472 | */ |
||
| 473 | protected function getSourceAddress(array $data) |
||
| 486 | |||
| 487 | } |
||
| 488 |