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 | 27 | public function __construct( |
|
169 | |||
170 | 17 | public function geocodeQuery(GeocodeQuery $query): Collection |
|
208 | |||
209 | 9 | public function reverseQuery(ReverseQuery $query): Collection |
|
231 | |||
232 | /** |
||
233 | * {@inheritdoc} |
||
234 | */ |
||
235 | 8 | public function getName(): string |
|
239 | |||
240 | /** |
||
241 | * @param string $url |
||
242 | * @param int $limit |
||
243 | * @param string|null $locale |
||
244 | * @param string|null $country |
||
245 | * |
||
246 | * @return string query with extra params |
||
247 | */ |
||
248 | 23 | private function buildQuery(string $url, int $limit, string $locale = null, string $country = null): string |
|
261 | |||
262 | /** |
||
263 | * @param string $url |
||
264 | * @param int $limit |
||
265 | * @param string|null $locale |
||
266 | * @param string|null $country |
||
267 | * |
||
268 | * @return AddressCollection |
||
269 | */ |
||
270 | 23 | private function fetchUrl(string $url, int $limit, string $locale = null, string $country = null): AddressCollection |
|
328 | |||
329 | /** |
||
330 | * Update current resultSet with given key/value. |
||
331 | * |
||
332 | * @param AddressBuilder $builder |
||
333 | * @param string $type Component type |
||
334 | * @param array $value The component value |
||
335 | */ |
||
336 | 7 | private function updateAddressComponent(AddressBuilder $builder, string $type, array $value) |
|
383 | |||
384 | /** |
||
385 | * Decode the response content and validate it to make sure it does not have any errors. |
||
386 | * |
||
387 | * @param string $url |
||
388 | * @param string $content |
||
389 | * |
||
390 | * @return array |
||
391 | */ |
||
392 | 10 | private function validateResponse(string $url, $content): array |
|
403 | |||
404 | /** |
||
405 | * Parse coordinats and bounds. |
||
406 | * |
||
407 | * @param AddressBuilder $builder |
||
408 | * @param array $result |
||
409 | */ |
||
410 | 7 | private function parseCoordinates(AddressBuilder $builder, array $result) |
|
424 | } |
||
425 |