Complex classes like MapQuest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MapQuest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | final class MapQuest extends AbstractHttpProvider implements Provider |
||
38 | { |
||
39 | const DATA_KEY_ADDRESS = 'address'; |
||
40 | |||
41 | const KEY_API_KEY = 'key'; |
||
42 | |||
43 | const KEY_LOCATION = 'location'; |
||
44 | |||
45 | const KEY_OUT_FORMAT = 'outFormat'; |
||
46 | |||
47 | const KEY_MAX_RESULTS = 'maxResults'; |
||
48 | |||
49 | const KEY_THUMB_MAPS = 'thumbMaps'; |
||
50 | |||
51 | const KEY_INTL_MODE = 'intlMode'; |
||
52 | |||
53 | const KEY_BOUNDING_BOX = 'boundingBox'; |
||
54 | |||
55 | const KEY_LAT = 'lat'; |
||
56 | |||
57 | const KEY_LNG = 'lng'; |
||
58 | |||
59 | const MODE_5BOX = '5BOX'; |
||
60 | |||
61 | const OPEN_BASE_URL = 'https://open.mapquestapi.com/geocoding/v1/'; |
||
62 | |||
63 | const LICENSED_BASE_URL = 'https://www.mapquestapi.com/geocoding/v1/'; |
||
64 | |||
65 | const GEOCODE_ENDPOINT = 'address'; |
||
66 | |||
67 | const DEFAULT_GEOCODE_PARAMS = [ |
||
68 | self::KEY_LOCATION => '', |
||
69 | self::KEY_OUT_FORMAT => 'json', |
||
70 | self::KEY_API_KEY => '', |
||
71 | ]; |
||
72 | |||
73 | const DEFAULT_GEOCODE_OPTIONS = [ |
||
74 | self::KEY_MAX_RESULTS => 3, |
||
75 | self::KEY_THUMB_MAPS => false, |
||
76 | ]; |
||
77 | |||
78 | const REVERSE_ENDPOINT = 'reverse'; |
||
79 | |||
80 | const ADMIN_LEVEL_STATE = 1; |
||
81 | |||
82 | const ADMIN_LEVEL_COUNTY = 2; |
||
83 | |||
84 | /** |
||
85 | * MapQuest offers two geocoding endpoints one commercial (true) and one open (false) |
||
86 | * More information: http://developer.mapquest.com/web/tools/getting-started/platform/licensed-vs-open. |
||
87 | * |
||
88 | * @var bool |
||
89 | */ |
||
90 | private $licensed; |
||
91 | |||
92 | /** |
||
93 | * @var bool |
||
94 | */ |
||
95 | private $useRoadPosition; |
||
96 | |||
97 | /** |
||
98 | * @var string |
||
99 | */ |
||
100 | private $apiKey; |
||
101 | |||
102 | /** |
||
103 | * @param HttpClient $client an HTTP adapter |
||
104 | * @param string $apiKey an API key |
||
105 | * @param bool $licensed true to use MapQuest's licensed endpoints, default is false to use the open endpoints (optional) |
||
106 | * @param bool $useRoadPosition true to use nearest point on a road for the entrance, false to use map display position |
||
107 | */ |
||
108 | 28 | public function __construct(HttpClient $client, string $apiKey, bool $licensed = false, bool $useRoadPosition = false) |
|
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | 18 | public function geocodeQuery(GeocodeQuery $query): Collection |
|
169 | |||
170 | /** |
||
171 | * {@inheritdoc} |
||
172 | */ |
||
173 | 9 | public function reverseQuery(ReverseQuery $query): Collection |
|
187 | |||
188 | /** |
||
189 | * {@inheritdoc} |
||
190 | */ |
||
191 | 10 | public function getName(): string |
|
195 | |||
196 | 18 | private function extractAddressFromQuery(GeocodeQuery $query) |
|
200 | |||
201 | 23 | private function getUrl($endpoint): string |
|
211 | |||
212 | 20 | private function addGetQuery(string $url, array $params): string |
|
216 | |||
217 | 11 | private function addOptionsForGetQuery(array $params, array $options): array |
|
230 | |||
231 | 3 | private function addOptionsForPostQuery(array $params, array $options): array |
|
237 | |||
238 | 3 | private function executePostQuery(string $endpoint, array $params) |
|
254 | |||
255 | /** |
||
256 | * @param string $url |
||
|
|||
257 | * |
||
258 | * @return AddressCollection |
||
259 | */ |
||
260 | 20 | private function executeGetQuery(string $endpoint, array $params): AddressCollection |
|
269 | |||
270 | 13 | private function parseResponseContent(string $content): AddressCollection |
|
330 | |||
331 | 3 | private function mapAddressToArray(Location $address): array |
|
332 | { |
||
333 | 3 | $location = []; |
|
334 | |||
335 | $streetParts = [ |
||
336 | 3 | trim($address->getStreetNumber() ?: ''), |
|
337 | 3 | trim($address->getStreetName() ?: ''), |
|
338 | ]; |
||
339 | 3 | $street = implode(' ', array_filter($streetParts)); |
|
340 | 3 | if ($street) { |
|
341 | 1 | $location['street'] = $street; |
|
342 | } |
||
343 | |||
344 | 3 | if ($address->getSubLocality()) { |
|
345 | 1 | $location['adminArea6'] = $address->getSubLocality(); |
|
346 | 1 | $location['adminArea6Type'] = 'Neighborhood'; |
|
347 | } |
||
348 | |||
349 | 3 | if ($address->getLocality()) { |
|
350 | 3 | $location['adminArea5'] = $address->getLocality(); |
|
351 | 3 | $location['adminArea5Type'] = 'City'; |
|
352 | } |
||
353 | |||
354 | /** @var AdminLevel $adminLevel */ |
||
355 | 3 | foreach ($address->getAdminLevels() as $adminLevel) { |
|
356 | 1 | switch ($adminLevel->getLevel()) { |
|
357 | 1 | case static::ADMIN_LEVEL_STATE: |
|
358 | 1 | $state = $adminLevel->getCode(); |
|
359 | 1 | if (!$state) { |
|
360 | $state = $adminLevel->getName(); |
||
361 | } |
||
362 | 1 | $location['adminArea3'] = $state; |
|
363 | 1 | $location['adminArea3Type'] = 'State'; |
|
364 | |||
365 | 1 | break; |
|
366 | case static::ADMIN_LEVEL_COUNTY: |
||
367 | $county = $adminLevel->getName(); |
||
368 | $location['adminArea4'] = $county; |
||
369 | $location['adminArea4Type'] = 'County'; |
||
370 | } |
||
371 | } |
||
372 | |||
373 | 3 | $country = $address->getCountry(); |
|
374 | 3 | if ($country instanceof Country) { |
|
375 | 1 | $code = $country->getCode(); |
|
376 | 1 | if (!$code) { |
|
377 | $code = $country->getName(); |
||
378 | } |
||
379 | 1 | $location['adminArea1'] = $code; |
|
380 | 1 | $location['adminArea1Type'] = 'Country'; |
|
381 | } |
||
382 | |||
383 | 3 | $postalCode = $address->getPostalCode(); |
|
384 | 3 | if ($postalCode) { |
|
385 | 1 | $location['postalCode'] = $address->getPostalCode(); |
|
386 | } |
||
387 | |||
388 | 3 | return $location; |
|
389 | } |
||
390 | |||
391 | 1 | private function mapBoundsToArray(Bounds $bounds) |
|
398 | |||
399 | 3 | protected function parseHttpResponse(ResponseInterface $response, string $url): string |
|
417 | } |
||
418 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.