| Conditions | 10 | 
| Paths | 50 | 
| Total Lines | 61 | 
| Code Lines | 39 | 
| 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 | // Use Nominatim | ||
| 123 |                 foreach ($results as $result) { | ||
| 124 | $data->add($result->display_name); | ||
| 125 | } | ||
| 126 |             } else { | ||
| 127 | // Use Openroutesearch | ||
| 128 |                 $place_elements = array_filter(explode(',', Site::getPreference('openroute_layers'))); | ||
| 129 |                 foreach ($results->features as $result) { | ||
| 130 | $place_parts = []; | ||
| 131 |                     foreach ($place_elements as $place_element) { | ||
| 132 |                         if (isset($result->properties->{$place_element})) { | ||
| 133 |                             $place_parts[] = $result->properties->{$place_element}; | ||
| 134 | } | ||
| 135 | } | ||
| 136 | // If no elements are selected in the control panel | ||
| 137 | // or none of the selected elements are present in the place | ||
| 138 | // then default to the label which is always present (I think) | ||
| 139 | $data->add(implode(Gedcom::PLACE_SEPARATOR, $place_parts) ?: $result->properties->label); | ||
| 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 |