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 | /** |
||
120 | * @param null|string $id |
||
121 | * |
||
122 | * @return GoogleAddress |
||
123 | */ |
||
124 | 17 | public function withId(string $id = null) |
|
131 | |||
132 | /** |
||
133 | * @see https://developers.google.com/places/place-id |
||
134 | * |
||
135 | * @return null|string |
||
136 | */ |
||
137 | 4 | public function getId() |
|
141 | |||
142 | /** |
||
143 | * @param null|string $locationType |
||
144 | * |
||
145 | * @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 | /** |
||
172 | * @param array $resultType |
||
173 | * |
||
174 | * @return GoogleAddress |
||
175 | */ |
||
176 | 17 | public function withResultType(array $resultType) |
|
183 | |||
184 | /** |
||
185 | * @return null|string |
||
186 | */ |
||
187 | public function getFormattedAddress() |
||
191 | |||
192 | /** |
||
193 | * @param string|null $formattedAddress |
||
194 | * |
||
195 | * @return GoogleAddress |
||
196 | */ |
||
197 | 17 | public function withFormattedAddress(string $formattedAddress = null) |
|
204 | |||
205 | /** |
||
206 | * @return null|string |
||
207 | */ |
||
208 | 1 | public function getAirport() |
|
212 | |||
213 | /** |
||
214 | * @param string|null $airport |
||
215 | * |
||
216 | * @return GoogleAddress |
||
217 | */ |
||
218 | 17 | public function withAirport(string $airport = null) |
|
225 | |||
226 | /** |
||
227 | * @return null|string |
||
228 | */ |
||
229 | 1 | public function getColloquialArea() |
|
233 | |||
234 | /** |
||
235 | * @param string|null $colloquialArea |
||
236 | * |
||
237 | * @return GoogleAddress |
||
238 | */ |
||
239 | 17 | public function withColloquialArea(string $colloquialArea = null) |
|
246 | |||
247 | /** |
||
248 | * @return null|string |
||
249 | */ |
||
250 | public function getIntersection() |
||
254 | |||
255 | /** |
||
256 | * @param string|null $intersection |
||
257 | * |
||
258 | * @return GoogleAddress |
||
259 | */ |
||
260 | 17 | public function withIntersection(string $intersection = null) |
|
267 | |||
268 | /** |
||
269 | * @return null|string |
||
270 | */ |
||
271 | 1 | public function getNaturalFeature() |
|
275 | |||
276 | /** |
||
277 | * @param string|null $naturalFeature |
||
278 | * |
||
279 | * @return GoogleAddress |
||
280 | */ |
||
281 | 17 | public function withNaturalFeature(string $naturalFeature = null) |
|
288 | |||
289 | /** |
||
290 | * @return null|string |
||
291 | */ |
||
292 | 1 | public function getNeighborhood() |
|
296 | |||
297 | /** |
||
298 | * @param string|null $neighborhood |
||
299 | * |
||
300 | * @return GoogleAddress |
||
301 | */ |
||
302 | 17 | public function withNeighborhood(string $neighborhood = null) |
|
309 | |||
310 | /** |
||
311 | * @return null|string |
||
312 | */ |
||
313 | 1 | public function getPark() |
|
317 | |||
318 | /** |
||
319 | * @param string|null $park |
||
320 | * |
||
321 | * @return GoogleAddress |
||
322 | */ |
||
323 | 17 | public function withPark(string $park = null) |
|
330 | |||
331 | /** |
||
332 | * @return null|string |
||
333 | */ |
||
334 | 2 | public function getPointOfInterest() |
|
338 | |||
339 | /** |
||
340 | * @param string|null $pointOfInterest |
||
341 | * |
||
342 | * @return GoogleAddress |
||
343 | */ |
||
344 | 17 | public function withPointOfInterest(string $pointOfInterest = null) |
|
351 | |||
352 | /** |
||
353 | * @return null|string |
||
354 | */ |
||
355 | 1 | public function getPolitical() |
|
359 | |||
360 | /** |
||
361 | * @param string|null $political |
||
362 | * |
||
363 | * @return GoogleAddress |
||
364 | */ |
||
365 | 17 | public function withPolitical(string $political = null) |
|
372 | |||
373 | /** |
||
374 | * @return null|string |
||
375 | */ |
||
376 | 1 | public function getPremise() |
|
380 | |||
381 | /** |
||
382 | * @param string $premise |
||
383 | * |
||
384 | * @return GoogleAddress |
||
385 | */ |
||
386 | 17 | public function withPremise(string $premise = null) |
|
393 | |||
394 | /** |
||
395 | * @return null|string |
||
396 | */ |
||
397 | public function getStreetAddress() |
||
401 | |||
402 | /** |
||
403 | * @param string|null $streetAddress |
||
404 | * |
||
405 | * @return GoogleAddress |
||
406 | */ |
||
407 | 17 | public function withStreetAddress(string $streetAddress = null) |
|
414 | |||
415 | /** |
||
416 | * @return null|string |
||
417 | */ |
||
418 | 1 | public function getSubpremise() |
|
422 | |||
423 | /** |
||
424 | * @param string|null $subpremise |
||
425 | * |
||
426 | * @return GoogleAddress |
||
427 | */ |
||
428 | 17 | public function withSubpremise(string $subpremise = null) |
|
435 | |||
436 | /** |
||
437 | * @return null|string |
||
438 | */ |
||
439 | public function getWard() |
||
440 | { |
||
441 | return $this->ward; |
||
442 | } |
||
443 | |||
444 | /** |
||
445 | * @param string|null $ward |
||
446 | * |
||
447 | * @return GoogleAddress |
||
448 | */ |
||
449 | 17 | public function withWard(string $ward = null) |
|
456 | |||
457 | /** |
||
458 | * @return null|string |
||
459 | */ |
||
460 | 1 | public function getEstablishment() |
|
464 | |||
465 | /** |
||
466 | * @param string|null $establishment |
||
467 | * |
||
468 | * @return GoogleAddress |
||
469 | */ |
||
470 | 17 | public function withEstablishment(string $establishment = null) |
|
477 | |||
478 | /** |
||
479 | * @return AdminLevelCollection |
||
480 | */ |
||
481 | 1 | public function getSubLocalityLevels() |
|
485 | |||
486 | /** |
||
487 | * @param array $subLocalityLevel |
||
488 | * |
||
489 | * @return $this |
||
490 | */ |
||
491 | 17 | public function withSubLocalityLevels(array $subLocalityLevel) |
|
514 | |||
515 | /** |
||
516 | * @return bool |
||
517 | */ |
||
518 | 15 | public function isPartialMatch() |
|
522 | |||
523 | /** |
||
524 | * @param bool $partialMatch |
||
525 | * |
||
526 | * @return $this |
||
527 | */ |
||
528 | 17 | public function withPartialMatch(bool $partialMatch) |
|
535 | } |
||
536 |