| Conditions | 21 |
| Paths | 18 |
| Total Lines | 64 |
| Code Lines | 40 |
| 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 |
||
| 60 | public function geocodeQuery(GeocodeQuery $query): Collection |
||
| 61 | { |
||
| 62 | $address = $query->getText(); |
||
| 63 | |||
| 64 | // This API doesn't handle IPs |
||
| 65 | if (!filter_var($address, FILTER_VALIDATE_IP)) { |
||
| 66 | throw new UnsupportedOperation('The Ipstack provider does not support street addresses.'); |
||
| 67 | } |
||
| 68 | |||
| 69 | if (in_array($address, ['127.0.0.1', '::1'])) { |
||
| 70 | return new AddressCollection([$this->getLocationForLocalhost()]); |
||
| 71 | } |
||
| 72 | |||
| 73 | $url = sprintf(sprintf(self::GEOCODE_ENDPOINT_URL, $address, $this->apiKey)); |
||
| 74 | |||
| 75 | if (null !== $query->getLocale()) { |
||
| 76 | $url = sprintf('%s&language=%s', $url, $query->getLocale()); |
||
| 77 | } |
||
| 78 | |||
| 79 | $body = $this->getUrlContents($url); |
||
| 80 | $data = json_decode($body, true); |
||
| 81 | |||
| 82 | // https://ipstack.com/documentation#errors |
||
| 83 | if (isset($data['error'])) { |
||
| 84 | switch ($data['error']['code']) { |
||
| 85 | case 301: |
||
| 86 | throw new InvalidArgument( |
||
| 87 | 'Invalid request (a required parameter is missing).' |
||
| 88 | ); |
||
| 89 | case 303: |
||
| 90 | throw new InvalidArgument( |
||
| 91 | 'Bulk requests are not supported on your plan. Please upgrade your subscription.' |
||
| 92 | ); |
||
| 93 | case 104: |
||
| 94 | throw new QuotaExceeded( |
||
| 95 | 'The maximum allowed amount of monthly API requests has been reached.' |
||
| 96 | ); |
||
| 97 | case 101: |
||
| 98 | throw new InvalidCredentials( |
||
| 99 | 'No API Key was specified or an invalid API Key was specified.' |
||
| 100 | ); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | if (null === $data['latitude'] |
||
| 105 | && null === $data['longitude'] |
||
| 106 | && null === $data['city'] |
||
| 107 | && null === $data['zip'] |
||
| 108 | && null === $data['country_name'] |
||
| 109 | && null === $data['country_code']) { |
||
| 110 | return new AddressCollection([]); |
||
| 111 | } |
||
| 112 | |||
| 113 | $locations[] = Address::createFromArray([ |
||
|
|
|||
| 114 | 'providedBy' => $this->getName(), |
||
| 115 | 'latitude' => $data['latitude'] ?: null, |
||
| 116 | 'longitude' => $data['longitude'] ?: null, |
||
| 117 | 'locality' => $data['city'] ?: null, |
||
| 118 | 'postalCode' => $data['zip'] ?: null, |
||
| 119 | 'country' => $data['country_name'] ?: null, |
||
| 120 | 'countryCode' => $data['country_code'] ?: null, |
||
| 121 | ]); |
||
| 122 | |||
| 123 | return new AddressCollection($locations); |
||
| 124 | } |
||
| 142 |