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