Complex classes like GeocoderResult 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 GeocoderResult, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class GeocoderResult |
||
20 | { |
||
21 | /** |
||
22 | * @var string|null |
||
23 | */ |
||
24 | private $placeId; |
||
25 | |||
26 | /** |
||
27 | * @var GeocoderAddress[] |
||
28 | */ |
||
29 | private $addresses = []; |
||
30 | |||
31 | /** |
||
32 | * @var string|null |
||
33 | */ |
||
34 | private $formattedAddress; |
||
35 | |||
36 | /** |
||
37 | * @var string[] |
||
38 | */ |
||
39 | private $postcodeLocalities = []; |
||
40 | |||
41 | /** |
||
42 | * @var GeocoderGeometry|null |
||
43 | */ |
||
44 | private $geometry; |
||
45 | |||
46 | /** |
||
47 | * @var bool|null |
||
48 | */ |
||
49 | private $partialMatch; |
||
50 | |||
51 | /** |
||
52 | * @var string[] |
||
53 | */ |
||
54 | private $types = []; |
||
55 | |||
56 | /** |
||
57 | * @return bool |
||
58 | */ |
||
59 | public function hasPlaceId() |
||
63 | |||
64 | /** |
||
65 | * @return string|null |
||
66 | */ |
||
67 | public function getPlaceId() |
||
71 | |||
72 | /** |
||
73 | * @param string|null $placeId |
||
74 | */ |
||
75 | public function setPlaceId($placeId) |
||
79 | |||
80 | /** |
||
81 | * @param string|null $type |
||
82 | * |
||
83 | * @return bool |
||
84 | */ |
||
85 | public function hasAddresses($type = null) |
||
91 | |||
92 | /** |
||
93 | * @param string|null $type |
||
94 | * |
||
95 | * @return GeocoderAddress[] |
||
96 | */ |
||
97 | public function getAddresses($type = null) |
||
113 | |||
114 | /** |
||
115 | * @param GeocoderAddress[] $addresses |
||
116 | */ |
||
117 | public function setAddresses(array $addresses) |
||
122 | |||
123 | /** |
||
124 | * @param GeocoderAddress[] $addresses |
||
125 | */ |
||
126 | public function addAddresses(array $addresses) |
||
132 | |||
133 | /** |
||
134 | * @param GeocoderAddress $address |
||
135 | * |
||
136 | * @return bool |
||
137 | */ |
||
138 | public function hasAddress(GeocoderAddress $address) |
||
142 | |||
143 | /** |
||
144 | * @param GeocoderAddress $address |
||
145 | */ |
||
146 | public function addAddress(GeocoderAddress $address) |
||
152 | |||
153 | /** |
||
154 | * @param GeocoderAddress $address |
||
155 | */ |
||
156 | public function removeAddress(GeocoderAddress $address) |
||
161 | |||
162 | /** |
||
163 | * @return bool |
||
164 | */ |
||
165 | public function hasFormattedAddress() |
||
169 | |||
170 | /** |
||
171 | * @return string|null |
||
172 | */ |
||
173 | public function getFormattedAddress() |
||
177 | |||
178 | /** |
||
179 | * @param string|null $formattedAddress |
||
180 | */ |
||
181 | public function setFormattedAddress($formattedAddress = null) |
||
185 | |||
186 | /** |
||
187 | * @return bool |
||
188 | */ |
||
189 | public function hasPostcodeLocalities() |
||
193 | |||
194 | /** |
||
195 | * @return string[] |
||
196 | */ |
||
197 | public function getPostcodeLocalities() |
||
201 | |||
202 | /** |
||
203 | * @param string[] $postcodeLocalities |
||
204 | */ |
||
205 | public function setPostcodeLocalities(array $postcodeLocalities) |
||
210 | |||
211 | /** |
||
212 | * @param string[] $postcodeLocalities |
||
213 | */ |
||
214 | public function addPostcodeLocalities(array $postcodeLocalities) |
||
220 | |||
221 | /** |
||
222 | * @param string $postcodeLocality |
||
223 | * |
||
224 | * @return bool |
||
225 | */ |
||
226 | public function hasPostcodeLocality($postcodeLocality) |
||
230 | |||
231 | /** |
||
232 | * @param string $postcodeLocality |
||
233 | */ |
||
234 | public function addPostcodeLocality($postcodeLocality) |
||
240 | |||
241 | /** |
||
242 | * @param string $postcodeLocality |
||
243 | */ |
||
244 | public function removePostcodeLocality($postcodeLocality) |
||
249 | |||
250 | /** |
||
251 | * @return bool |
||
252 | */ |
||
253 | public function hasGeometry() |
||
257 | |||
258 | /** |
||
259 | * @return GeocoderGeometry|null |
||
260 | */ |
||
261 | public function getGeometry() |
||
265 | |||
266 | /** |
||
267 | * @param GeocoderGeometry|null $geometry |
||
268 | */ |
||
269 | public function setGeometry(GeocoderGeometry $geometry = null) |
||
273 | |||
274 | /** |
||
275 | * @return bool |
||
276 | */ |
||
277 | public function hasPartialMatch() |
||
281 | |||
282 | /** |
||
283 | * @return bool |
||
284 | */ |
||
285 | public function isPartialMatch() |
||
289 | |||
290 | /** |
||
291 | * @param bool|null $partialMatch |
||
292 | */ |
||
293 | public function setPartialMatch($partialMatch = null) |
||
297 | |||
298 | /** |
||
299 | * @return bool |
||
300 | */ |
||
301 | public function hasTypes() |
||
305 | |||
306 | /** |
||
307 | * @return string[] |
||
308 | */ |
||
309 | public function getTypes() |
||
313 | |||
314 | /** |
||
315 | * @param string[] $types |
||
316 | */ |
||
317 | public function setTypes(array $types) |
||
322 | |||
323 | /** |
||
324 | * @param string[] $types |
||
325 | */ |
||
326 | public function addTypes(array $types) |
||
332 | |||
333 | /** |
||
334 | * @param string $type |
||
335 | * |
||
336 | * @return bool |
||
337 | */ |
||
338 | public function hasType($type) |
||
342 | |||
343 | /** |
||
344 | * @param string $type |
||
345 | */ |
||
346 | public function addType($type) |
||
352 | |||
353 | /** |
||
354 | * @param string $type |
||
355 | */ |
||
356 | public function removeType($type) |
||
361 | } |
||
362 |