| Conditions | 12 |
| Paths | 24 |
| Total Lines | 41 |
| Code Lines | 32 |
| 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 |
||
| 113 | protected static function buildShipment($array) |
||
| 114 | { |
||
| 115 | if (!empty($array['TrackInfo']['TrackDetail'])) { |
||
| 116 | $trackDetails = is_numeric(key($array['TrackInfo']['TrackDetail'])) |
||
| 117 | ? $array['TrackInfo']['TrackDetail'] |
||
| 118 | : [$array['TrackInfo']['TrackDetail']]; |
||
| 119 | } else { |
||
| 120 | $trackDetails = []; |
||
| 121 | } |
||
| 122 | //The track summary is also valid |
||
| 123 | if (isset($array['TrackInfo']['TrackSummary'])) { |
||
| 124 | array_unshift($trackDetails, $array['TrackInfo']['TrackSummary']); |
||
| 125 | } |
||
| 126 | $events = array_map(function($eventData){ |
||
| 127 | $time = empty($eventData['EventTime']) ? '' : $eventData['EventTime']; |
||
| 128 | $day = empty($eventData['EventDate']) ? '' : $eventData['EventDate']; |
||
| 129 | $country = empty($eventData['EventCountry']) ? '' : $eventData['EventCountry']; |
||
| 130 | $state = empty($eventData['EventState']) ? '' : $eventData['EventState']; |
||
| 131 | $city = empty($eventData['EventCity']) ? '' : $eventData['EventCity']; |
||
| 132 | $zipCode = empty($eventData['EventZIPCode']) ? '' : $eventData['EventZIPCode']; |
||
| 133 | return ShipmentEvent::fromArray([ |
||
| 134 | 'day' => $day, |
||
| 135 | 'city' => $city, |
||
| 136 | 'state' => $state, |
||
| 137 | 'country' => $country, |
||
| 138 | 'date' => Carbon::parse("{$day} {$time}"), |
||
| 139 | 'location' => new Location($country, $state, $city), |
||
| 140 | 'description' => $eventData['Event'], |
||
| 141 | 'zipCode' => $zipCode |
||
| 142 | ]); |
||
| 143 | }, array_reverse($trackDetails)); |
||
| 144 | |||
| 145 | $shipment = new Shipment($events); |
||
| 146 | if (isset($array['TrackInfo']['TrackSummary']['DeliveryAttributeCode'])) { |
||
| 147 | $shipment->setIsDelivered($array['TrackInfo']['TrackSummary']['DeliveryAttributeCode'] == '01'); |
||
| 148 | } |
||
| 149 | if ($firstEvent = reset($events)) { |
||
| 150 | $shipment->setDeliveredAt($firstEvent->getDate()); |
||
| 151 | } |
||
| 152 | return $shipment; |
||
| 153 | } |
||
| 154 | } |