| Conditions | 19 |
| Paths | 10 |
| Total Lines | 44 |
| Code Lines | 28 |
| 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 |
||
| 47 | public function geocodeQuery(GeocodeQuery $query): Collection |
||
| 48 | { |
||
| 49 | $address = $query->getText(); |
||
| 50 | $locale = $query->getLocale() ?: 'en'; // Default to English |
||
| 51 | if (!filter_var($address, FILTER_VALIDATE_IP)) { |
||
| 52 | throw new UnsupportedOperation('The GeoIP2 provider does not support street addresses, only IP addresses.'); |
||
| 53 | } |
||
| 54 | |||
| 55 | if ('127.0.0.1' === $address) { |
||
| 56 | return new AddressCollection([$this->getLocationForLocalhost()]); |
||
| 57 | } |
||
| 58 | |||
| 59 | $result = json_decode($this->executeQuery($address)); |
||
| 60 | |||
| 61 | if (null === $result) { |
||
| 62 | return new AddressCollection([]); |
||
| 63 | } |
||
| 64 | |||
| 65 | $adminLevels = []; |
||
| 66 | if (isset($result->subdivisions) && is_array($result->subdivisions)) { |
||
| 67 | foreach ($result->subdivisions as $i => $subdivision) { |
||
| 68 | $name = (isset($subdivision->names->{$locale}) ? $subdivision->names->{$locale} : null); |
||
| 69 | $code = (isset($subdivision->iso_code) ? $subdivision->iso_code : null); |
||
| 70 | |||
| 71 | if (null !== $name || null !== $code) { |
||
| 72 | $adminLevels[] = ['name' => $name, 'code' => $code, 'level' => $i + 1]; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | return new AddressCollection([ |
||
| 78 | Address::createFromArray([ |
||
| 79 | 'providedBy' => $this->getName(), |
||
| 80 | 'countryCode' => (isset($result->country->iso_code) ? $result->country->iso_code : null), |
||
| 81 | 'country' => (isset($result->country->names->{$locale}) ? $result->country->names->{$locale} : null), |
||
| 82 | 'locality' => (isset($result->city->names->{$locale}) ? $result->city->names->{$locale} : null), |
||
| 83 | 'latitude' => (isset($result->location->latitude) ? $result->location->latitude : null), |
||
| 84 | 'longitude' => (isset($result->location->longitude) ? $result->location->longitude : null), |
||
| 85 | 'timezone' => (isset($result->location->time_zone) ? $result->location->time_zone : null), |
||
| 86 | 'postalCode' => (isset($result->postal->code) ? $result->postal->code : null), |
||
| 87 | 'adminLevels' => $adminLevels, |
||
| 88 | ]), |
||
| 89 | ]); |
||
| 90 | } |
||
| 91 | |||
| 136 |