Complex classes like GoogleAddress 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 GoogleAddress, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | final class GoogleAddress extends Address |
||
23 | { |
||
24 | /** |
||
25 | * @var string|null |
||
26 | */ |
||
27 | private $id; |
||
28 | |||
29 | /** |
||
30 | * @var string|null |
||
31 | */ |
||
32 | private $locationType; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | private $resultType = []; |
||
38 | |||
39 | /** |
||
40 | * @var string|null |
||
41 | */ |
||
42 | private $formattedAddress; |
||
43 | |||
44 | /** |
||
45 | * @var string|null |
||
46 | */ |
||
47 | private $streetAddress; |
||
48 | |||
49 | /** |
||
50 | * @var string|null |
||
51 | */ |
||
52 | private $intersection; |
||
53 | |||
54 | /** |
||
55 | * @var string|null |
||
56 | */ |
||
57 | private $political; |
||
58 | |||
59 | /** |
||
60 | * @var string|null |
||
61 | */ |
||
62 | private $colloquialArea; |
||
63 | |||
64 | /** |
||
65 | * @var string|null |
||
66 | */ |
||
67 | private $ward; |
||
68 | |||
69 | /** |
||
70 | * @var string|null |
||
71 | */ |
||
72 | private $neighborhood; |
||
73 | |||
74 | /** |
||
75 | * @var string|null |
||
76 | */ |
||
77 | private $premise; |
||
78 | |||
79 | /** |
||
80 | * @var string|null |
||
81 | */ |
||
82 | private $subpremise; |
||
83 | |||
84 | /** |
||
85 | * @var string|null |
||
86 | */ |
||
87 | private $naturalFeature; |
||
88 | |||
89 | /** |
||
90 | * @var string|null |
||
91 | */ |
||
92 | private $airport; |
||
93 | |||
94 | /** |
||
95 | * @var string|null |
||
96 | */ |
||
97 | private $park; |
||
98 | |||
99 | /** |
||
100 | * @var string|null |
||
101 | */ |
||
102 | private $pointOfInterest; |
||
103 | |||
104 | /** |
||
105 | * @var string|null |
||
106 | */ |
||
107 | private $establishment; |
||
108 | |||
109 | /** |
||
110 | * @var AdminLevelCollection |
||
111 | */ |
||
112 | private $subLocalityLevels; |
||
113 | |||
114 | /** |
||
115 | * @var bool |
||
116 | */ |
||
117 | private $partialMatch; |
||
118 | |||
119 | 17 | /** |
|
120 | * @param null|string $id |
||
121 | 17 | * |
|
122 | 17 | * @return GoogleAddress |
|
123 | */ |
||
124 | 17 | public function withId(string $id = null) |
|
131 | |||
132 | 3 | /** |
|
133 | * @see https://developers.google.com/places/place-id |
||
134 | 3 | * |
|
135 | * @return null|string |
||
136 | */ |
||
137 | public function getId() |
||
141 | |||
142 | 17 | /** |
|
143 | * @param null|string $locationType |
||
144 | 17 | * |
|
145 | 17 | * @return GoogleAddress |
|
146 | */ |
||
147 | 17 | public function withLocationType(string $locationType = null) |
|
154 | |||
155 | /** |
||
156 | * @return null|string |
||
157 | */ |
||
158 | public function getLocationType() |
||
162 | |||
163 | /** |
||
164 | * @return array |
||
165 | */ |
||
166 | public function getResultType(): array |
||
170 | |||
171 | 17 | /** |
|
172 | * @param array $resultType |
||
173 | 17 | * |
|
174 | 17 | * @return GoogleAddress |
|
175 | */ |
||
176 | 17 | public function withResultType(array $resultType) |
|
183 | |||
184 | /** |
||
185 | * @return null|string |
||
186 | */ |
||
187 | public function getFormattedAddress() |
||
191 | |||
192 | 17 | /** |
|
193 | * @param string|null $formattedAddress |
||
194 | 17 | * |
|
195 | 17 | * @return GoogleAddress |
|
196 | */ |
||
197 | 17 | public function withFormattedAddress(string $formattedAddress = null) |
|
204 | |||
205 | 1 | /** |
|
206 | * @return null|string |
||
207 | */ |
||
208 | public function getAirport() |
||
212 | |||
213 | 17 | /** |
|
214 | * @param string|null $airport |
||
215 | 17 | * |
|
216 | 17 | * @return GoogleAddress |
|
217 | */ |
||
218 | 17 | public function withAirport(string $airport = null) |
|
225 | |||
226 | 1 | /** |
|
227 | * @return null|string |
||
228 | */ |
||
229 | public function getColloquialArea() |
||
233 | |||
234 | 17 | /** |
|
235 | * @param string|null $colloquialArea |
||
236 | 17 | * |
|
237 | 17 | * @return GoogleAddress |
|
238 | */ |
||
239 | 17 | public function withColloquialArea(string $colloquialArea = null) |
|
246 | |||
247 | /** |
||
248 | * @return null|string |
||
249 | */ |
||
250 | public function getIntersection() |
||
254 | |||
255 | 17 | /** |
|
256 | * @param string|null $intersection |
||
257 | 17 | * |
|
258 | 17 | * @return GoogleAddress |
|
259 | */ |
||
260 | 17 | public function withIntersection(string $intersection = null) |
|
267 | |||
268 | 1 | /** |
|
269 | * @return null|string |
||
270 | */ |
||
271 | public function getNaturalFeature() |
||
275 | |||
276 | 17 | /** |
|
277 | * @param string|null $naturalFeature |
||
278 | 17 | * |
|
279 | 17 | * @return GoogleAddress |
|
280 | */ |
||
281 | 17 | public function withNaturalFeature(string $naturalFeature = null) |
|
288 | |||
289 | 1 | /** |
|
290 | * @return null|string |
||
291 | */ |
||
292 | public function getNeighborhood() |
||
296 | |||
297 | 17 | /** |
|
298 | * @param string|null $neighborhood |
||
299 | 17 | * |
|
300 | 17 | * @return GoogleAddress |
|
301 | */ |
||
302 | 17 | public function withNeighborhood(string $neighborhood = null) |
|
309 | |||
310 | 1 | /** |
|
311 | * @return null|string |
||
312 | */ |
||
313 | public function getPark() |
||
317 | |||
318 | 17 | /** |
|
319 | * @param string|null $park |
||
320 | 17 | * |
|
321 | 17 | * @return GoogleAddress |
|
322 | */ |
||
323 | 17 | public function withPark(string $park = null) |
|
330 | |||
331 | 2 | /** |
|
332 | * @return null|string |
||
333 | */ |
||
334 | public function getPointOfInterest() |
||
338 | |||
339 | 17 | /** |
|
340 | * @param string|null $pointOfInterest |
||
341 | 17 | * |
|
342 | 17 | * @return GoogleAddress |
|
343 | */ |
||
344 | 17 | public function withPointOfInterest(string $pointOfInterest = null) |
|
351 | |||
352 | 1 | /** |
|
353 | * @return null|string |
||
354 | */ |
||
355 | public function getPolitical() |
||
359 | |||
360 | 17 | /** |
|
361 | * @param string|null $political |
||
362 | 17 | * |
|
363 | 17 | * @return GoogleAddress |
|
364 | */ |
||
365 | 17 | public function withPolitical(string $political = null) |
|
372 | |||
373 | 1 | /** |
|
374 | * @return null|string |
||
375 | */ |
||
376 | public function getPremise() |
||
380 | |||
381 | 17 | /** |
|
382 | * @param string $premise |
||
383 | 17 | * |
|
384 | 17 | * @return GoogleAddress |
|
385 | */ |
||
386 | 17 | public function withPremise(string $premise = null) |
|
393 | |||
394 | /** |
||
395 | * @return null|string |
||
396 | */ |
||
397 | public function getStreetAddress() |
||
401 | |||
402 | 17 | /** |
|
403 | * @param string|null $streetAddress |
||
404 | 17 | * |
|
405 | 17 | * @return GoogleAddress |
|
406 | */ |
||
407 | 17 | public function withStreetAddress(string $streetAddress = null) |
|
414 | |||
415 | 1 | /** |
|
416 | * @return null|string |
||
417 | */ |
||
418 | public function getSubpremise() |
||
422 | |||
423 | 17 | /** |
|
424 | * @param string|null $subpremise |
||
425 | 17 | * |
|
426 | 17 | * @return GoogleAddress |
|
427 | */ |
||
428 | 17 | public function withSubpremise(string $subpremise = null) |
|
435 | |||
436 | 1 | /** |
|
437 | * @return null|string |
||
438 | */ |
||
439 | public function getWard() |
||
443 | |||
444 | 17 | /** |
|
445 | * @param string|null $ward |
||
446 | 17 | * |
|
447 | 17 | * @return GoogleAddress |
|
448 | */ |
||
449 | 17 | public function withWard(string $ward = null) |
|
456 | |||
457 | 1 | /** |
|
458 | * @return null|string |
||
459 | */ |
||
460 | public function getEstablishment() |
||
464 | |||
465 | 17 | /** |
|
466 | * @param string|null $establishment |
||
467 | 17 | * |
|
468 | 17 | * @return GoogleAddress |
|
469 | */ |
||
470 | 17 | public function withEstablishment(string $establishment = null) |
|
477 | |||
478 | 1 | /** |
|
479 | * @return AdminLevelCollection |
||
480 | */ |
||
481 | public function getSubLocalityLevels() |
||
485 | |||
486 | 17 | /** |
|
487 | * @param array $subLocalityLevel |
||
488 | 17 | * |
|
489 | 17 | * @return $this |
|
490 | 6 | */ |
|
491 | public function withSubLocalityLevels(array $subLocalityLevel) |
||
512 | |||
513 | /** |
||
514 | * @return bool |
||
515 | */ |
||
516 | public function isPartialMatch() |
||
520 | |||
521 | /** |
||
522 | * @param bool $partialMatch |
||
523 | * |
||
524 | * @return $this |
||
525 | */ |
||
526 | public function withPartialMatch(bool $partialMatch) |
||
533 | } |
||
534 |