| Conditions | 18 |
| Paths | 14 |
| Total Lines | 48 |
| Code Lines | 29 |
| 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 |
||
| 132 | private function executeQuery(string $url): AddressCollection |
||
| 133 | { |
||
| 134 | $content = $this->getUrlContents($url); |
||
| 135 | $json = json_decode($content, true); |
||
| 136 | |||
| 137 | if (!isset($json['results']) || empty($json['results'])) { |
||
| 138 | return new AddressCollection([]); |
||
| 139 | } |
||
| 140 | |||
| 141 | $locations = $json['results'][0]['locations']; |
||
| 142 | |||
| 143 | if (empty($locations)) { |
||
| 144 | return new AddressCollection([]); |
||
| 145 | } |
||
| 146 | |||
| 147 | $results = []; |
||
| 148 | foreach ($locations as $location) { |
||
| 149 | if ($location['street'] || $location['postalCode'] || $location['adminArea5'] || $location['adminArea4'] || $location['adminArea3']) { |
||
| 150 | $admins = []; |
||
| 151 | |||
| 152 | if ($location['adminArea3']) { |
||
| 153 | $admins[] = ['name' => $location['adminArea3'], 'level' => 1]; |
||
| 154 | } |
||
| 155 | |||
| 156 | if ($location['adminArea4']) { |
||
| 157 | $admins[] = ['name' => $location['adminArea4'], 'level' => 2]; |
||
| 158 | } |
||
| 159 | |||
| 160 | $results[] = Address::createFromArray([ |
||
| 161 | 'providedBy' => $this->getName(), |
||
| 162 | 'latitude' => $location['latLng']['lat'], |
||
| 163 | 'longitude' => $location['latLng']['lng'], |
||
| 164 | 'streetName' => $location['street'] ?: null, |
||
| 165 | 'locality' => $location['adminArea5'] ?: null, |
||
| 166 | 'postalCode' => $location['postalCode'] ?: null, |
||
| 167 | 'adminLevels' => $admins, |
||
| 168 | 'country' => $location['adminArea1'] ?: null, |
||
| 169 | 'countryCode' => $location['adminArea1'] ?: null, |
||
| 170 | ]); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | if (empty($results)) { |
||
| 175 | return new AddressCollection([]); |
||
| 176 | } |
||
| 177 | |||
| 178 | return new AddressCollection($results); |
||
| 179 | } |
||
| 180 | } |
||
| 181 |