Conditions | 21 |
Paths | 21 |
Total Lines | 74 |
Code Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
129 | protected function executeQuery(string $url): AddressCollection |
||
130 | { |
||
131 | $content = $this->getUrlContents($url); |
||
132 | $json = json_decode($content, true); |
||
133 | |||
134 | if (isset($json['meta'])) { |
||
135 | switch ($json['meta']['status_code']) { |
||
136 | case 401: |
||
137 | case 403: |
||
138 | throw new InvalidCredentials('Invalid or missing api key.'); |
||
139 | case 429: |
||
140 | throw new QuotaExceeded('Valid request but quota exceeded.'); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | if ( |
||
145 | !isset($json['type']) |
||
146 | || 'FeatureCollection' !== $json['type'] |
||
147 | || !isset($json['features']) |
||
148 | || 0 === count($json['features']) |
||
149 | ) { |
||
150 | return new AddressCollection([]); |
||
151 | } |
||
152 | |||
153 | $locations = $json['features']; |
||
154 | |||
155 | if (empty($locations)) { |
||
156 | return new AddressCollection([]); |
||
157 | } |
||
158 | |||
159 | $results = []; |
||
160 | foreach ($locations as $location) { |
||
161 | if (isset($location['bbox'])) { |
||
162 | $bounds = [ |
||
163 | 'south' => $location['bbox'][3], |
||
164 | 'west' => $location['bbox'][2], |
||
165 | 'north' => $location['bbox'][1], |
||
166 | 'east' => $location['bbox'][0], |
||
167 | ]; |
||
168 | } else { |
||
169 | $bounds = [ |
||
170 | 'south' => null, |
||
171 | 'west' => null, |
||
172 | 'north' => null, |
||
173 | 'east' => null, |
||
174 | ]; |
||
175 | } |
||
176 | |||
177 | $props = $location['properties']; |
||
178 | |||
179 | $adminLevels = []; |
||
180 | foreach (['region', 'county', 'locality', 'macroregion', 'country'] as $i => $component) { |
||
181 | if (isset($props[$component])) { |
||
182 | $adminLevels[] = ['name' => $props[$component], 'level' => $i + 1]; |
||
183 | } |
||
184 | } |
||
185 | |||
186 | $results[] = Address::createFromArray([ |
||
187 | 'providedBy' => $this->getName(), |
||
188 | 'latitude' => $location['geometry']['coordinates'][1], |
||
189 | 'longitude' => $location['geometry']['coordinates'][0], |
||
190 | 'bounds' => $bounds, |
||
191 | 'streetNumber' => isset($props['housenumber']) ? $props['housenumber'] : null, |
||
192 | 'streetName' => isset($props['street']) ? $props['street'] : null, |
||
193 | 'subLocality' => isset($props['neighbourhood']) ? $props['neighbourhood'] : null, |
||
194 | 'locality' => isset($props['locality']) ? $props['locality'] : null, |
||
195 | 'postalCode' => isset($props['postalcode']) ? $props['postalcode'] : null, |
||
196 | 'adminLevels' => $adminLevels, |
||
197 | 'country' => isset($props['country']) ? $props['country'] : null, |
||
198 | 'countryCode' => isset($props['country_a']) ? strtoupper($props['country_a']) : null, |
||
199 | ]); |
||
200 | } |
||
201 | |||
202 | return new AddressCollection($results); |
||
203 | } |
||
258 |