Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Mapbox 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 Mapbox, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | final class Mapbox extends AbstractHttpProvider implements Provider |
||
29 | { |
||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | const GEOCODE_ENDPOINT_URL_SSL = 'https://api.mapbox.com/geocoding/v5/%s/%s.json'; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | const REVERSE_ENDPOINT_URL_SSL = 'https://api.mapbox.com/geocoding/v5/%s/%F,%F.json'; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | const GEOCODING_MODE_PLACES = 'mapbox.places'; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | const GEOCODING_MODE_PLACES_PERMANENT = 'mapbox.places-permanent'; |
||
49 | |||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | const GEOCODING_MODES = [ |
||
54 | self::GEOCODING_MODE_PLACES, |
||
55 | self::GEOCODING_MODE_PLACES_PERMANENT, |
||
56 | ]; |
||
57 | |||
58 | /** |
||
59 | * @var string |
||
60 | */ |
||
61 | const TYPE_COUNTRY = 'country'; |
||
62 | |||
63 | /** |
||
64 | * @var string |
||
65 | */ |
||
66 | const TYPE_REGION = 'region'; |
||
67 | |||
68 | /** |
||
69 | * @var string |
||
70 | */ |
||
71 | const TYPE_POSTCODE = 'postcode'; |
||
72 | |||
73 | /** |
||
74 | * @var string |
||
75 | */ |
||
76 | const TYPE_DISTRICT = 'district'; |
||
77 | |||
78 | /** |
||
79 | * @var string |
||
80 | */ |
||
81 | const TYPE_PLACE = 'place'; |
||
82 | |||
83 | /** |
||
84 | * @var string |
||
85 | */ |
||
86 | const TYPE_LOCALITY = 'locality'; |
||
87 | |||
88 | /** |
||
89 | * @var string |
||
90 | */ |
||
91 | const TYPE_NEIGHBORHOOD = 'neighborhood'; |
||
92 | |||
93 | /** |
||
94 | * @var string |
||
95 | */ |
||
96 | const TYPE_ADDRESS = 'address'; |
||
97 | |||
98 | /** |
||
99 | * @var string |
||
100 | */ |
||
101 | const TYPE_POI = 'poi'; |
||
102 | |||
103 | /** |
||
104 | * @var string |
||
105 | */ |
||
106 | const TYPE_POI_LANDMARK = 'poi.landmark'; |
||
107 | |||
108 | /** |
||
109 | * @var array |
||
110 | */ |
||
111 | const TYPES = [ |
||
112 | self::TYPE_COUNTRY, |
||
113 | self::TYPE_REGION, |
||
114 | self::TYPE_POSTCODE, |
||
115 | self::TYPE_DISTRICT, |
||
116 | self::TYPE_PLACE, |
||
117 | self::TYPE_LOCALITY, |
||
118 | self::TYPE_NEIGHBORHOOD, |
||
119 | self::TYPE_ADDRESS, |
||
120 | self::TYPE_POI, |
||
121 | self::TYPE_POI_LANDMARK, |
||
122 | ]; |
||
123 | |||
124 | const DEFAULT_TYPE = self::TYPE_ADDRESS; |
||
125 | |||
126 | /** |
||
127 | * @var HttpClient |
||
128 | */ |
||
129 | private $client; |
||
|
|||
130 | |||
131 | /** |
||
132 | * @var string |
||
133 | */ |
||
134 | private $accessToken; |
||
135 | |||
136 | /** |
||
137 | * @var string|null |
||
138 | */ |
||
139 | private $country; |
||
140 | |||
141 | /** |
||
142 | * @var string |
||
143 | */ |
||
144 | private $geocodingMode; |
||
145 | |||
146 | /** |
||
147 | * @param HttpClient $client An HTTP adapter |
||
148 | * @param string $accessToken Your Mapbox access token |
||
149 | * @param string|null $country |
||
150 | * @param string $geocodingMode |
||
151 | */ |
||
152 | 25 | public function __construct( |
|
169 | |||
170 | 15 | public function geocodeQuery(GeocodeQuery $query): Collection |
|
171 | { |
||
172 | // Mapbox API returns invalid data if IP address given |
||
173 | // This API doesn't handle IPs |
||
174 | 15 | if (filter_var($query->getText(), FILTER_VALIDATE_IP)) { |
|
175 | 3 | throw new UnsupportedOperation('The Mapbox provider does not support IP addresses, only street addresses.'); |
|
176 | } |
||
177 | |||
178 | 12 | $url = sprintf(self::GEOCODE_ENDPOINT_URL_SSL, $this->geocodingMode, rawurlencode($query->getText())); |
|
179 | |||
180 | 12 | $urlParameters = []; |
|
181 | 12 | if ($query->getBounds()) { |
|
182 | // Format is "minLon,minLat,maxLon,maxLat" |
||
183 | 1 | $urlParameters['bbox'] = sprintf( |
|
184 | 1 | '%s,%s,%s,%s', |
|
185 | 1 | $query->getBounds()->getWest(), |
|
186 | 1 | $query->getBounds()->getSouth(), |
|
187 | 1 | $query->getBounds()->getEast(), |
|
188 | 1 | $query->getBounds()->getNorth() |
|
189 | ); |
||
190 | } |
||
191 | |||
192 | 12 | View Code Duplication | if (null !== $locationType = $query->getData('location_type')) { |
193 | 1 | $urlParameters['types'] = is_array($locationType) ? implode(',', $locationType) : $locationType; |
|
194 | } else { |
||
195 | 11 | $urlParameters['types'] = self::DEFAULT_TYPE; |
|
196 | } |
||
197 | |||
198 | 12 | if ($urlParameters) { |
|
199 | 12 | $url .= '?'.http_build_query($urlParameters); |
|
200 | } |
||
201 | |||
202 | 12 | return $this->fetchUrl($url, $query->getLimit(), $query->getLocale(), $query->getData('country', $this->country)); |
|
203 | } |
||
204 | |||
205 | 9 | public function reverseQuery(ReverseQuery $query): Collection |
|
227 | |||
228 | /** |
||
229 | * {@inheritdoc} |
||
230 | */ |
||
231 | 7 | public function getName(): string |
|
235 | |||
236 | /** |
||
237 | * @param string $url |
||
238 | * @param int $limit |
||
239 | * @param string|null $locale |
||
240 | * @param string|null $country |
||
241 | * |
||
242 | * @return string query with extra params |
||
243 | */ |
||
244 | 21 | private function buildQuery(string $url, int $limit, string $locale = null, string $country = null): string |
|
257 | |||
258 | /** |
||
259 | * @param string $url |
||
260 | * @param int $limit |
||
261 | * @param string|null $locale |
||
262 | * @param string|null $country |
||
263 | * |
||
264 | * @return AddressCollection |
||
265 | */ |
||
266 | 21 | private function fetchUrl(string $url, int $limit, string $locale = null, string $country = null): AddressCollection |
|
324 | |||
325 | /** |
||
326 | * Update current resultSet with given key/value. |
||
327 | * |
||
328 | * @param AddressBuilder $builder |
||
329 | * @param string $type Component type |
||
330 | * @param array $value The component value |
||
331 | */ |
||
332 | 6 | private function updateAddressComponent(AddressBuilder $builder, string $type, array $value) |
|
379 | |||
380 | /** |
||
381 | * Decode the response content and validate it to make sure it does not have any errors. |
||
382 | * |
||
383 | * @param string $url |
||
384 | * @param string $content |
||
385 | * |
||
386 | * @return array |
||
387 | */ |
||
388 | 8 | private function validateResponse(string $url, $content): array |
|
399 | |||
400 | /** |
||
401 | * Parse coordinats and bounds. |
||
402 | * |
||
403 | * @param AddressBuilder $builder |
||
404 | * @param array $result |
||
405 | */ |
||
406 | 6 | private function parseCoordinates(AddressBuilder $builder, array $result) |
|
420 | } |
||
421 |