| Conditions | 20 |
| Paths | 7 |
| Total Lines | 44 |
| 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 |
||
| 49 | public function geocodeQuery(GeocodeQuery $query): Collection |
||
| 50 | { |
||
| 51 | $address = $query->getText(); |
||
| 52 | if (!filter_var($address, FILTER_VALIDATE_IP)) { |
||
| 53 | throw new UnsupportedOperation('The FreeGeoIp provider does not support street addresses.'); |
||
| 54 | } |
||
| 55 | |||
| 56 | if (in_array($address, ['127.0.0.1', '::1'])) { |
||
| 57 | return new AddressCollection([$this->getLocationForLocalhost()]); |
||
| 58 | } |
||
| 59 | |||
| 60 | $content = $this->getUrlContents(sprintf($this->baseUrl, $address)); |
||
| 61 | $data = json_decode($content, true); |
||
| 62 | |||
| 63 | // Return empty collection if address was not found |
||
| 64 | if ('' === $data['region_name'] |
||
| 65 | && '' === $data['region_code'] |
||
| 66 | && 0 === $data['latitude'] |
||
| 67 | && 0 === $data['longitude'] |
||
| 68 | && '' === $data['city'] |
||
| 69 | && '' === $data['zip_code'] |
||
| 70 | && '' === $data['country_name'] |
||
| 71 | && '' === $data['country_code'] |
||
| 72 | && '' === $data['time_zone']) { |
||
| 73 | return new AddressCollection([]); |
||
| 74 | } |
||
| 75 | |||
| 76 | $builder = new AddressBuilder($this->getName()); |
||
| 77 | |||
| 78 | if (!empty($data['region_name'])) { |
||
| 79 | $builder->addAdminLevel(1, $data['region_name'], $data['region_code'] ?? null); |
||
| 80 | } |
||
| 81 | |||
| 82 | if (0 !== $data['latitude'] || 0 !== $data['longitude']) { |
||
| 83 | $builder->setCoordinates($data['latitude'] ?? null, $data['longitude'] ?? null); |
||
| 84 | } |
||
| 85 | $builder->setLocality(empty($data['city']) ? null : $data['city']); |
||
| 86 | $builder->setPostalCode(empty($data['zip_code']) ? null : $data['zip_code']); |
||
| 87 | $builder->setCountry(empty($data['country_name']) ? null : $data['country_name']); |
||
| 88 | $builder->setCountryCode(empty($data['country_code']) ? null : $data['country_code']); |
||
| 89 | $builder->setTimezone(empty($data['time_zone']) ? null : $data['time_zone']); |
||
| 90 | |||
| 91 | return new AddressCollection([$builder->build()]); |
||
| 92 | } |
||
| 93 | |||
| 110 |