| Conditions | 8 |
| Paths | 4 |
| Total Lines | 63 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 31 | protected function query($query, $params = []) |
||
| 32 | { |
||
| 33 | $url = self::API_URL; |
||
| 34 | $url = str_replace('{mode}', self::MODE_PLACES, $url); |
||
| 35 | $url = str_replace('{query}', urlencode($query), $url); |
||
| 36 | |||
| 37 | $defaultParams = [ |
||
| 38 | // 'language' => $this->getLanguage(i18n::get_locale()), |
||
| 39 | 'types' => 'address', |
||
| 40 | 'limit' => 1, |
||
| 41 | 'access_token' => Environment::getEnv('MAPBOX_API_KEY') |
||
| 42 | ]; |
||
| 43 | $params = array_merge($defaultParams, $params); |
||
| 44 | |||
| 45 | $url .= '?' . http_build_query($params); |
||
| 46 | |||
| 47 | $result = file_get_contents($url); |
||
| 48 | if (!$result) { |
||
| 49 | throw new Exception("The api returned no result"); |
||
| 50 | } |
||
| 51 | |||
| 52 | $data = json_decode($result, true); |
||
| 53 | |||
| 54 | if (!$data) { |
||
| 55 | throw new Exception("Failed to decode api results"); |
||
| 56 | } |
||
| 57 | |||
| 58 | $location = null; |
||
| 59 | $countryCode = $countryName = null; |
||
| 60 | $lat = $lon = null; |
||
| 61 | |||
| 62 | if (!empty($data['features'])) { |
||
| 63 | $feature = $data['features'][0]; |
||
| 64 | |||
| 65 | $location = [ |
||
| 66 | // A string of the house number for the returned address feature |
||
| 67 | 'streetNumber' => $feature['address'] ?? null, |
||
| 68 | 'streetName' => $feature['text'], |
||
| 69 | ]; |
||
| 70 | |||
| 71 | $lat = $feature['geometry']['coordinates'][1]; |
||
| 72 | $lon = $feature['geometry']['coordinates'][0]; |
||
| 73 | foreach ($feature['context'] as $context) { |
||
| 74 | $parts = explode('.', $context['id']); |
||
| 75 | $contextType = $parts[0]; |
||
| 76 | $contextId = $parts[1]; |
||
|
|
|||
| 77 | if ($contextType == 'postcode') { |
||
| 78 | $location['postalCode'] = $context['text']; |
||
| 79 | } |
||
| 80 | if ($contextType == 'place') { |
||
| 81 | $location['locality'] = $context['text']; |
||
| 82 | } |
||
| 83 | if ($contextType == 'country') { |
||
| 84 | $countryCode = $context['short_code']; |
||
| 85 | $countryName = $context['text']; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | $country = new Country($countryCode, $countryName); |
||
| 91 | $coordinates = new Coordinates($lat, $lon); |
||
| 92 | |||
| 93 | return new Address($location, $country, $coordinates); |
||
| 94 | } |
||
| 139 |