| Conditions | 1 |
| Paths | 1 |
| Total Lines | 75 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 8 | { |
||
| 9 | public function getShipment() |
||
| 10 | { |
||
| 11 | $shipmentConfig = $this->config()->load('Shipment', $this->config()->get('path/project')); |
||
| 12 | $this->verifyShipmentConfig($shipmentConfig); |
||
| 13 | |||
| 14 | $shipment = new Shipment(); |
||
| 15 | |||
| 16 | $shipment->setAttribute('shipDate', date(Shipment::DATE_FORMAT, strtotime('tomorrow'))); |
||
| 17 | |||
| 18 | $shipment->setAttribute('shipFrom', $shipmentConfig['attributes']['shipFrom']); |
||
| 19 | $shipment->setAttribute('shipTo', $shipmentConfig['attributes']['shipTo']); |
||
| 20 | $shipment->setAttribute('packages', $shipmentConfig['attributes']['packages']); |
||
| 21 | foreach (['useCod', 'saturdayDelivery'] as $item) { |
||
| 22 | if (isset($shipmentConfig['attributes'][$item])) { |
||
| 23 | $shipment->setAttribute($item, $shipmentConfig['attributes'][$item]); |
||
| 24 | } |
||
| 25 | } |
||
| 26 | $shipment->setService($shipmentConfig['meta']['service']); |
||
| 27 | |||
| 28 | return $shipment; |
||
| 29 | } |
||
| 30 | |||
| 31 | protected function verifyShipmentConfig($shipmentConfig) |
||
| 32 | { |
||
| 33 | if (empty($shipmentConfig) || !is_array($shipmentConfig)) { |
||
| 34 | throw new ApplicationException('Missing or invalid shipment configuration'); |
||
| 35 | } |
||
| 36 | foreach (['shipFrom', 'shipTo', 'packages'] as $item) { |
||
| 37 | if (!isset($shipmentConfig['attributes'][$item]) || !is_array($shipmentConfig['attributes'][$item])) { |
||
| 38 | throw new ApplicationException( |
||
| 39 | sprintf('Missing or invalid shipment configuration attribute: %s', $item) |
||
| 40 | ); |
||
| 41 | } |
||
| 42 | } |
||
| 43 | if (!isset($shipmentConfig['meta']['service'])) { |
||
| 44 | throw new ApplicationException('Missing or invalid shipment configuration meta: service'); |
||
| 45 | } |
||
| 46 | return true; |
||
| 47 | } |
||
| 48 | } |
||
| 49 |