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 |
||
| 29 | { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Module class instance |
||
| 33 | * @var \gplcart\core\Module $module |
||
| 34 | */ |
||
| 35 | protected $module; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Api model instance |
||
| 39 | * @var \gplcart\modules\shippo\models\Api $api |
||
| 40 | */ |
||
| 41 | protected $api; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Translation UI model instance |
||
| 45 | * @var \gplcart\core\models\Translation $translation |
||
| 46 | */ |
||
| 47 | protected $translation; |
||
| 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\CountryState $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 model class instance |
||
| 93 | * @var \gplcart\core\models\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 | * An array of Shippo module settings |
||
| 105 | * @var array |
||
| 106 | */ |
||
| 107 | protected $settings = array(); |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Shippo constructor. |
||
| 111 | * @param Module $module |
||
| 112 | * @param Api $api |
||
| 113 | * @param Translation $translation |
||
| 114 | * @param User $user |
||
| 115 | * @param Price $price |
||
| 116 | * @param Currency $currency |
||
| 117 | * @param Address $address |
||
| 118 | * @param Store $store |
||
| 119 | * @param CountryState $state |
||
| 120 | * @param Shipping $shipping |
||
| 121 | * @param Session $session |
||
| 122 | * @param Convertor $convertor |
||
| 123 | */ |
||
| 124 | public function __construct(Module $module, Api $api, Translation $translation, User $user, Price $price, |
||
| 125 | Currency $currency, Address $address, Store $store, CountryState $state, Shipping $shipping, |
||
| 126 | Session $session, Convertor $convertor) |
||
| 127 | { |
||
| 128 | $this->api = $api; |
||
| 129 | $this->user = $user; |
||
| 130 | $this->price = $price; |
||
| 131 | $this->state = $state; |
||
| 132 | $this->store = $store; |
||
| 133 | $this->session = $session; |
||
| 134 | $this->address = $address; |
||
| 135 | $this->currency = $currency; |
||
| 136 | $this->shipping = $shipping; |
||
| 137 | $this->convertor = $convertor; |
||
| 138 | $this->translation = $translation; |
||
| 139 | |||
| 140 | $this->module = $module; |
||
| 141 | $this->settings = $this->module->getSettings('shippo'); |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Returns an array of carrier names keyed by id |
||
| 146 | * @return array |
||
| 147 | */ |
||
| 148 | public function getCarrierNames() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Returns an array of service names keyed by id |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | public function getServiceNames() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Calculates shipping rates and sets available shipping methods on checkout page |
||
| 164 | * @param array $data |
||
| 165 | */ |
||
| 166 | public function calculate(array &$data) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Validates order shipping rates before an order is created |
||
| 175 | * @param array $order |
||
| 176 | * @param array $options |
||
| 177 | * @param array $result |
||
| 178 | * @return null|bool |
||
| 179 | */ |
||
| 180 | public function validate(array &$order, $options, array &$result) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Returns an array of cached rates |
||
| 221 | * @param array|integer $address |
||
| 222 | * @param array $cart |
||
| 223 | * @param array $order |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | public function getRates($address, array $cart, array $order) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Returns a unique cache key for a combination of recipient and sender addresses |
||
| 277 | * @param array $from |
||
| 278 | * @param array $to |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | protected function getCacheKey(array $from, $to) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Sets shipping methods available for the order shipping address |
||
| 291 | * @param array $data |
||
| 292 | * @param array $rates |
||
| 293 | */ |
||
| 294 | protected function setShippingMethodsCheckout(array &$data, array $rates) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Adjust price and label for the given shipping method |
||
| 313 | * @param array $method |
||
| 314 | * @param array $rates |
||
| 315 | * @param string $currency |
||
| 316 | * @return boolean |
||
| 317 | */ |
||
| 318 | protected function setShippingMethod(array &$method, array $rates, $currency) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Returns the default rates if unabled to calculate via Shippo API |
||
| 344 | * @return array |
||
| 345 | */ |
||
| 346 | protected function getDefaultRates() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Converts the system method ID into Shippo's service ID |
||
| 364 | * @param string $system_method_id |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | protected function getShippoServiceId($system_method_id) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Converts an address into Shippo's format |
||
| 374 | * @param array $data |
||
| 375 | * @return array |
||
| 376 | */ |
||
| 377 | protected function getShippoAddress(array $data) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Returns an array of parcel data in Shippo's format |
||
| 435 | * @param array $cart |
||
| 436 | * @param array $order |
||
| 437 | * @return array |
||
| 438 | */ |
||
| 439 | protected function getParcel(array $cart, array $order) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Returns total dimensions of all products in the order |
||
| 456 | * @param array $cart |
||
| 457 | * @param array $order |
||
| 458 | * @return array |
||
| 459 | */ |
||
| 460 | protected function getDimensions(array $cart, array $order) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Sets a source shipping address |
||
| 488 | * @param array $data |
||
| 489 | * @return array |
||
| 490 | */ |
||
| 491 | protected function getSourceAddress(array $data) |
||
| 504 | |||
| 505 | } |
||
| 506 |