| Conditions | 20 |
| Paths | 20 |
| Total Lines | 69 |
| Code Lines | 46 |
| 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 |
||
| 104 | private function executeQuery(string $url): AddressCollection |
||
| 105 | { |
||
| 106 | $content = $this->getUrlContents($url); |
||
| 107 | $json = json_decode($content, true); |
||
| 108 | |||
| 109 | // See https://mapzen.com/documentation/search/api-keys-rate-limits/ |
||
| 110 | if (isset($json['meta'])) { |
||
| 111 | switch ($json['meta']['status_code']) { |
||
| 112 | case 403: |
||
| 113 | throw new InvalidCredentials('Invalid or missing api key.'); |
||
| 114 | case 429: |
||
| 115 | throw new QuotaExceeded('Valid request but quota exceeded.'); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | if (!isset($json['type']) || 'FeatureCollection' !== $json['type'] || !isset($json['features']) || 0 === count($json['features'])) { |
||
| 120 | return new AddressCollection([]); |
||
| 121 | } |
||
| 122 | |||
| 123 | $locations = $json['features']; |
||
| 124 | |||
| 125 | if (empty($locations)) { |
||
| 126 | return new AddressCollection([]); |
||
| 127 | } |
||
| 128 | |||
| 129 | $results = []; |
||
| 130 | foreach ($locations as $location) { |
||
| 131 | $bounds = [ |
||
| 132 | 'south' => null, |
||
| 133 | 'west' => null, |
||
| 134 | 'north' => null, |
||
| 135 | 'east' => null, |
||
| 136 | ]; |
||
| 137 | if (isset($location['bbox'])) { |
||
| 138 | $bounds = [ |
||
| 139 | 'south' => $location['bbox'][3], |
||
| 140 | 'west' => $location['bbox'][2], |
||
| 141 | 'north' => $location['bbox'][1], |
||
| 142 | 'east' => $location['bbox'][0], |
||
| 143 | ]; |
||
| 144 | } |
||
| 145 | |||
| 146 | $props = $location['properties']; |
||
| 147 | |||
| 148 | $adminLevels = []; |
||
| 149 | foreach (['region', 'locality', 'macroregion', 'country'] as $i => $component) { |
||
| 150 | if (isset($props[$component])) { |
||
| 151 | $adminLevels[] = ['name' => $props[$component], 'level' => $i + 1]; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | $results[] = Address::createFromArray([ |
||
| 156 | 'providedBy' => $this->getName(), |
||
| 157 | 'latitude' => $location['geometry']['coordinates'][1], |
||
| 158 | 'longitude' => $location['geometry']['coordinates'][0], |
||
| 159 | 'bounds' => $bounds, |
||
| 160 | 'streetNumber' => isset($props['housenumber']) ? $props['housenumber'] : null, |
||
| 161 | 'streetName' => isset($props['street']) ? $props['street'] : null, |
||
| 162 | 'subLocality' => isset($props['neighbourhood']) ? $props['neighbourhood'] : null, |
||
| 163 | 'locality' => isset($props['locality']) ? $props['locality'] : null, |
||
| 164 | 'postalCode' => isset($props['postalcode']) ? $props['postalcode'] : null, |
||
| 165 | 'adminLevels' => $adminLevels, |
||
| 166 | 'country' => isset($props['country']) ? $props['country'] : null, |
||
| 167 | 'countryCode' => isset($props['country_a']) ? strtoupper($props['country_a']) : null, |
||
| 168 | ]); |
||
| 169 | } |
||
| 170 | |||
| 171 | return new AddressCollection($results); |
||
| 172 | } |
||
| 173 | |||
| 227 |