| Conditions | 10 |
| Paths | 32 |
| Total Lines | 61 |
| Code Lines | 43 |
| 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 |
||
| 87 | private function searchGazetteer($query): collection |
||
| 88 | { |
||
| 89 | $key = Site::getPreference('openroute_key'); |
||
| 90 | $data = new Collection(); |
||
| 91 | $user_service = new UserService(); |
||
| 92 | |||
| 93 | if ($key === '') { |
||
| 94 | $url = self::NOMINATIM_URL; |
||
| 95 | $qry = [ |
||
| 96 | 'q' => rawurlencode($query), |
||
| 97 | 'format' => 'jsonv2', |
||
| 98 | 'limit' => static::LIMIT, |
||
| 99 | 'accept-language' => I18N::languageTag(), |
||
| 100 | 'featuretype' => 'settlement', |
||
| 101 | 'email' => rawurlencode($user_service->administrators()->first()->email()), |
||
| 102 | ]; |
||
| 103 | } else { |
||
| 104 | $url = self::OPENROUTE_URL; |
||
| 105 | $qry = [ |
||
| 106 | 'api_key' => $key, |
||
| 107 | 'text' => rawurlencode($query), |
||
| 108 | 'layers' => 'coarse', |
||
| 109 | 'size' => static::LIMIT, |
||
| 110 | ]; |
||
| 111 | } |
||
| 112 | |||
| 113 | // Read from the URL |
||
| 114 | $client = new Client(); |
||
| 115 | try { |
||
| 116 | $json = $client->get($url, array_merge(self::GUZZLE_OPTIONS, ['query' => $qry]))->getBody()->__toString(); |
||
| 117 | $results = json_decode($json, false); |
||
| 118 | if (json_last_error() !== JSON_ERROR_NONE) { |
||
| 119 | throw new Exception(I18N::translate('Geocoder: %s', json_last_error_msg())); |
||
| 120 | } |
||
| 121 | if ($key === '') { |
||
| 122 | foreach ($results as $result) { |
||
| 123 | $data->add($result->display_name); |
||
| 124 | } |
||
| 125 | } else { |
||
| 126 | $parts = explode(',', Site::getPreference('openroute_parts')); |
||
| 127 | foreach ($results->features as $result) { |
||
| 128 | if ($parts[0] === '') { |
||
| 129 | $place = $result->properties->label; |
||
| 130 | } else { |
||
| 131 | $place = ''; |
||
| 132 | foreach ($parts as $part) { |
||
| 133 | if (isset($result->properties->{$part})) { |
||
| 134 | $place .= $result->properties->{$part} . Gedcom::PLACE_SEPARATOR; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | $place = trim($place, Gedcom::PLACE_SEPARATOR); |
||
| 138 | } |
||
| 139 | $data->add($place); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | } catch (Exception | RequestException $ex) { |
||
| 143 | // Json error? Service down? Quota exceeded? |
||
| 144 | $data->add($ex->getMessage()); |
||
| 145 | } |
||
| 146 | |||
| 147 | return $data; |
||
| 148 | } |
||
| 150 |