| Conditions | 10 |
| Paths | 9 |
| Total Lines | 45 |
| 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 |
||
| 42 | public function geocodeQuery(GeocodeQuery $query): Collection |
||
| 43 | { |
||
| 44 | $address = $query->getText(); |
||
| 45 | |||
| 46 | if (!filter_var($address, FILTER_VALIDATE_IP)) { |
||
| 47 | throw new UnsupportedOperation('The Geoip provider does not support street addresses, only IPv4 addresses.'); |
||
| 48 | } |
||
| 49 | |||
| 50 | // This API does not support IPv6 |
||
| 51 | if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
||
| 52 | throw new UnsupportedOperation('The Geoip provider does not support IPv6 addresses, only IPv4 addresses.'); |
||
| 53 | } |
||
| 54 | |||
| 55 | if ('127.0.0.1' === $address) { |
||
| 56 | return new AddressCollection([$this->getLocationForLocalhost()]); |
||
| 57 | } |
||
| 58 | |||
| 59 | $results = @geoip_record_by_name($address); |
||
| 60 | |||
| 61 | if (!is_array($results)) { |
||
| 62 | return new AddressCollection([]); |
||
| 63 | } |
||
| 64 | |||
| 65 | if (!empty($results['region']) && !empty($results['country_code'])) { |
||
| 66 | $timezone = @geoip_time_zone_by_country_and_region($results['country_code'], $results['region']) ?: null; |
||
| 67 | $region = @geoip_region_name_by_code($results['country_code'], $results['region']) ?: $results['region']; |
||
| 68 | } else { |
||
| 69 | $timezone = null; |
||
| 70 | $region = $results['region']; |
||
| 71 | } |
||
| 72 | |||
| 73 | return new AddressCollection([ |
||
| 74 | Address::createFromArray([ |
||
| 75 | 'providedBy' => $this->getName(), |
||
| 76 | 'latitude' => $results['latitude'], |
||
| 77 | 'longitude' => $results['longitude'], |
||
| 78 | 'locality' => $results['city'], |
||
| 79 | 'postalCode' => $results['postal_code'], |
||
| 80 | 'adminLevels' => $results['region'] ? [['name' => $region, 'code' => $results['region'], 'level' => 1]] : [], |
||
| 81 | 'country' => $results['country_name'], |
||
| 82 | 'countryCode' => $results['country_code'], |
||
| 83 | 'timezone' => $timezone, |
||
| 84 | ]), |
||
| 85 | ]); |
||
| 86 | } |
||
| 87 | |||
| 104 |