| Conditions | 19 |
| Paths | 42 |
| Total Lines | 76 |
| Code Lines | 51 |
| 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 |
||
| 106 | private function executeQuery(string $url, string $locale = null): AddressCollection |
||
| 107 | { |
||
| 108 | if (null !== $locale) { |
||
| 109 | $url = sprintf('%s&language=%s', $url, $locale); |
||
| 110 | } |
||
| 111 | |||
| 112 | $content = $this->getUrlContents($url); |
||
| 113 | $json = json_decode($content, true); |
||
| 114 | |||
| 115 | // https://geocoder.opencagedata.com/api#codes |
||
| 116 | if (isset($json['status'])) { |
||
| 117 | switch ($json['status']['code']) { |
||
| 118 | case 400: |
||
| 119 | throw new InvalidArgument('Invalid request (a required parameter is missing).'); |
||
| 120 | case 402: |
||
| 121 | throw new QuotaExceeded('Valid request but quota exceeded.'); |
||
| 122 | case 403: |
||
| 123 | throw new InvalidCredentials('Invalid or missing api key.'); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | if (!isset($json['total_results']) || 0 == $json['total_results']) { |
||
| 128 | return new AddressCollection([]); |
||
| 129 | } |
||
| 130 | |||
| 131 | $locations = $json['results']; |
||
| 132 | |||
| 133 | if (empty($locations)) { |
||
| 134 | return new AddressCollection([]); |
||
| 135 | } |
||
| 136 | |||
| 137 | $results = []; |
||
| 138 | foreach ($locations as $location) { |
||
| 139 | $bounds = [ |
||
| 140 | 'south' => null, |
||
| 141 | 'west' => null, |
||
| 142 | 'north' => null, |
||
| 143 | 'east' => null, |
||
| 144 | ]; |
||
| 145 | if (isset($location['bounds'])) { |
||
| 146 | $bounds = [ |
||
| 147 | 'south' => $location['bounds']['southwest']['lat'], |
||
| 148 | 'west' => $location['bounds']['southwest']['lng'], |
||
| 149 | 'north' => $location['bounds']['northeast']['lat'], |
||
| 150 | 'east' => $location['bounds']['northeast']['lng'], |
||
| 151 | ]; |
||
| 152 | } |
||
| 153 | |||
| 154 | $comp = $location['components']; |
||
| 155 | |||
| 156 | $adminLevels = []; |
||
| 157 | foreach (['state', 'county'] as $i => $component) { |
||
| 158 | if (isset($comp[$component])) { |
||
| 159 | $adminLevels[] = ['name' => $comp[$component], 'level' => $i + 1]; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | $results[] = Address::createFromArray([ |
||
| 164 | 'providedBy' => $this->getName(), |
||
| 165 | 'latitude' => $location['geometry']['lat'], |
||
| 166 | 'longitude' => $location['geometry']['lng'], |
||
| 167 | 'bounds' => $bounds ?: [], |
||
| 168 | 'streetNumber' => isset($comp['house_number']) ? $comp['house_number'] : null, |
||
| 169 | 'streetName' => $this->guessStreetName($comp), |
||
| 170 | 'subLocality' => $this->guessSubLocality($comp), |
||
| 171 | 'locality' => $this->guessLocality($comp), |
||
| 172 | 'postalCode' => isset($comp['postcode']) ? $comp['postcode'] : null, |
||
| 173 | 'adminLevels' => $adminLevels, |
||
| 174 | 'country' => isset($comp['country']) ? $comp['country'] : null, |
||
| 175 | 'countryCode' => isset($comp['country_code']) ? strtoupper($comp['country_code']) : null, |
||
| 176 | 'timezone' => isset($location['annotations']['timezone']['name']) ? $location['annotations']['timezone']['name'] : null, |
||
| 177 | ]); |
||
| 178 | } |
||
| 179 | |||
| 180 | return new AddressCollection($results); |
||
| 181 | } |
||
| 182 | |||
| 236 |