| Conditions | 10 |
| Paths | 20 |
| Total Lines | 59 |
| Code Lines | 36 |
| 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 |
||
| 173 | private function executeQuery(string $url, string $locale = null): AddressCollection |
||
| 174 | { |
||
| 175 | if (null !== $locale) { |
||
| 176 | // Locale code transformation: for example from it_IT to it |
||
| 177 | $url = sprintf('%s&lang=%s', $url, substr($locale, 0, 2)); |
||
| 178 | } |
||
| 179 | |||
| 180 | $content = $this->getUrlContents($url); |
||
| 181 | if (null === $json = json_decode($content)) { |
||
| 182 | throw InvalidServerResponse::create($url); |
||
| 183 | } |
||
| 184 | |||
| 185 | if (isset($json->totalResultsCount) && empty($json->totalResultsCount)) { |
||
| 186 | return new AddressCollection([]); |
||
| 187 | } |
||
| 188 | |||
| 189 | $data = $json->geonames; |
||
| 190 | |||
| 191 | if (empty($data)) { |
||
| 192 | return new AddressCollection([]); |
||
| 193 | } |
||
| 194 | |||
| 195 | $results = []; |
||
| 196 | foreach ($data as $item) { |
||
| 197 | $builder = new AddressBuilder($this->getName()); |
||
| 198 | |||
| 199 | if (isset($item->bbox)) { |
||
| 200 | $builder->setBounds($item->bbox->south, $item->bbox->west, $item->bbox->north, $item->bbox->east); |
||
| 201 | } |
||
| 202 | |||
| 203 | for ($level = 1; $level <= AdminLevelCollection::MAX_LEVEL_DEPTH; ++$level) { |
||
| 204 | $adminNameProp = 'adminName'.$level; |
||
| 205 | $adminCodeProp = 'adminCode'.$level; |
||
| 206 | if (!empty($item->$adminNameProp)) { |
||
| 207 | $builder->addAdminLevel($level, $item->$adminNameProp, $item->$adminCodeProp ?? null); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | $builder->setCoordinates($item->lat ?? null, $item->lng ?? null); |
||
| 212 | $builder->setLocality($item->name ?? null); |
||
| 213 | $builder->setCountry($item->countryName ?? null); |
||
| 214 | $builder->setCountryCode($item->countryCode ?? null); |
||
| 215 | $builder->setTimezone($item->timezone->timeZoneId ?? null); |
||
| 216 | |||
| 217 | /** @var GeonamesAddress $address */ |
||
| 218 | $address = $builder->build(GeonamesAddress::class); |
||
| 219 | $address = $address->withName($item->name ?? null); |
||
| 220 | $address = $address->withAsciiName($item->asciiName ?? null); |
||
| 221 | $address = $address->withFclName($item->fclName ?? null); |
||
| 222 | $address = $address->withAlternateNames($item->alternateNames ?? []); |
||
| 223 | $address = $address->withPopulation($item->population ?? null); |
||
| 224 | $address = $address->withGeonameId($item->geonameId ?? null); |
||
| 225 | $address = $address->withFcode($item->fcode ?? null); |
||
| 226 | |||
| 227 | $results[] = $address; |
||
| 228 | } |
||
| 229 | |||
| 230 | return new AddressCollection($results); |
||
| 231 | } |
||
| 232 | } |
||
| 233 |