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 $postalCodeSuffix; |
||
58 | |||
59 | /** |
||
60 | * @var string|null |
||
61 | */ |
||
62 | private $political; |
||
63 | |||
64 | /** |
||
65 | * @var string|null |
||
66 | */ |
||
67 | private $colloquialArea; |
||
68 | |||
69 | /** |
||
70 | * @var string|null |
||
71 | */ |
||
72 | private $ward; |
||
73 | |||
74 | /** |
||
75 | * @var string|null |
||
76 | */ |
||
77 | private $neighborhood; |
||
78 | |||
79 | /** |
||
80 | * @var string|null |
||
81 | */ |
||
82 | private $premise; |
||
83 | |||
84 | /** |
||
85 | * @var string|null |
||
86 | */ |
||
87 | private $subpremise; |
||
88 | |||
89 | /** |
||
90 | * @var string|null |
||
91 | */ |
||
92 | private $naturalFeature; |
||
93 | |||
94 | /** |
||
95 | * @var string|null |
||
96 | */ |
||
97 | private $airport; |
||
98 | |||
99 | /** |
||
100 | * @var string|null |
||
101 | */ |
||
102 | private $park; |
||
103 | |||
104 | /** |
||
105 | * @var string|null |
||
106 | */ |
||
107 | private $pointOfInterest; |
||
108 | |||
109 | /** |
||
110 | * @var string|null |
||
111 | */ |
||
112 | private $establishment; |
||
113 | |||
114 | /** |
||
115 | * @var AdminLevelCollection |
||
116 | */ |
||
117 | private $subLocalityLevels; |
||
118 | |||
119 | /** |
||
120 | * @var bool |
||
121 | */ |
||
122 | private $partialMatch; |
||
123 | |||
124 | /** |
||
125 | * @param null|string $id |
||
126 | * |
||
127 | * @return GoogleAddress |
||
128 | */ |
||
129 | 18 | public function withId(string $id = null) |
|
136 | |||
137 | /** |
||
138 | * @see https://developers.google.com/places/place-id |
||
139 | * |
||
140 | * @return null|string |
||
141 | */ |
||
142 | 4 | public function getId() |
|
146 | |||
147 | /** |
||
148 | * @param null|string $locationType |
||
149 | * |
||
150 | * @return GoogleAddress |
||
151 | */ |
||
152 | 18 | public function withLocationType(string $locationType = null) |
|
159 | |||
160 | /** |
||
161 | * @return null|string |
||
162 | */ |
||
163 | public function getLocationType() |
||
167 | |||
168 | /** |
||
169 | * @return array |
||
170 | */ |
||
171 | public function getResultType(): array |
||
175 | |||
176 | /** |
||
177 | * @param array $resultType |
||
178 | * |
||
179 | * @return GoogleAddress |
||
180 | */ |
||
181 | 18 | public function withResultType(array $resultType) |
|
188 | |||
189 | /** |
||
190 | * @return null|string |
||
191 | */ |
||
192 | 1 | public function getFormattedAddress() |
|
196 | |||
197 | /** |
||
198 | * @param string|null $formattedAddress |
||
199 | * |
||
200 | * @return GoogleAddress |
||
201 | */ |
||
202 | 18 | public function withFormattedAddress(string $formattedAddress = null) |
|
209 | |||
210 | /** |
||
211 | * @return null|string |
||
212 | */ |
||
213 | 1 | public function getAirport() |
|
217 | |||
218 | /** |
||
219 | * @param string|null $airport |
||
220 | * |
||
221 | * @return GoogleAddress |
||
222 | */ |
||
223 | 18 | public function withAirport(string $airport = null) |
|
230 | |||
231 | /** |
||
232 | * @return null|string |
||
233 | */ |
||
234 | 1 | public function getColloquialArea() |
|
238 | |||
239 | /** |
||
240 | * @param string|null $colloquialArea |
||
241 | * |
||
242 | * @return GoogleAddress |
||
243 | */ |
||
244 | 18 | public function withColloquialArea(string $colloquialArea = null) |
|
251 | |||
252 | /** |
||
253 | * @return null|string |
||
254 | */ |
||
255 | public function getIntersection() |
||
259 | |||
260 | /** |
||
261 | * @param string|null $intersection |
||
262 | * |
||
263 | * @return GoogleAddress |
||
264 | */ |
||
265 | 18 | public function withIntersection(string $intersection = null) |
|
272 | |||
273 | /** |
||
274 | * @return null|string |
||
275 | */ |
||
276 | 2 | public function getPostalCodeSuffix() |
|
280 | |||
281 | /** |
||
282 | * @param string|null $postalCodeSuffix |
||
283 | * |
||
284 | * @return GoogleAddress |
||
285 | */ |
||
286 | 18 | public function withPostalCodeSuffix(string $postalCodeSuffix = null) |
|
293 | |||
294 | /** |
||
295 | * @return null|string |
||
296 | */ |
||
297 | 1 | public function getNaturalFeature() |
|
301 | |||
302 | /** |
||
303 | * @param string|null $naturalFeature |
||
304 | * |
||
305 | * @return GoogleAddress |
||
306 | */ |
||
307 | 18 | public function withNaturalFeature(string $naturalFeature = null) |
|
314 | |||
315 | /** |
||
316 | * @return null|string |
||
317 | */ |
||
318 | 1 | public function getNeighborhood() |
|
322 | |||
323 | /** |
||
324 | * @param string|null $neighborhood |
||
325 | * |
||
326 | * @return GoogleAddress |
||
327 | */ |
||
328 | 18 | public function withNeighborhood(string $neighborhood = null) |
|
335 | |||
336 | /** |
||
337 | * @return null|string |
||
338 | */ |
||
339 | 1 | public function getPark() |
|
343 | |||
344 | /** |
||
345 | * @param string|null $park |
||
346 | * |
||
347 | * @return GoogleAddress |
||
348 | */ |
||
349 | 18 | public function withPark(string $park = null) |
|
356 | |||
357 | /** |
||
358 | * @return null|string |
||
359 | */ |
||
360 | 2 | public function getPointOfInterest() |
|
364 | |||
365 | /** |
||
366 | * @param string|null $pointOfInterest |
||
367 | * |
||
368 | * @return GoogleAddress |
||
369 | */ |
||
370 | 18 | public function withPointOfInterest(string $pointOfInterest = null) |
|
377 | |||
378 | /** |
||
379 | * @return null|string |
||
380 | */ |
||
381 | 1 | public function getPolitical() |
|
385 | |||
386 | /** |
||
387 | * @param string|null $political |
||
388 | * |
||
389 | * @return GoogleAddress |
||
390 | */ |
||
391 | 18 | public function withPolitical(string $political = null) |
|
398 | |||
399 | /** |
||
400 | * @return null|string |
||
401 | */ |
||
402 | 1 | public function getPremise() |
|
406 | |||
407 | /** |
||
408 | * @param string $premise |
||
409 | * |
||
410 | * @return GoogleAddress |
||
411 | */ |
||
412 | 18 | public function withPremise(string $premise = null) |
|
419 | |||
420 | /** |
||
421 | * @return null|string |
||
422 | */ |
||
423 | public function getStreetAddress() |
||
427 | |||
428 | /** |
||
429 | * @param string|null $streetAddress |
||
430 | * |
||
431 | * @return GoogleAddress |
||
432 | */ |
||
433 | 18 | public function withStreetAddress(string $streetAddress = null) |
|
440 | |||
441 | /** |
||
442 | * @return null|string |
||
443 | */ |
||
444 | 1 | public function getSubpremise() |
|
448 | |||
449 | /** |
||
450 | * @param string|null $subpremise |
||
451 | * |
||
452 | * @return GoogleAddress |
||
453 | */ |
||
454 | 18 | public function withSubpremise(string $subpremise = null) |
|
461 | |||
462 | /** |
||
463 | * @return null|string |
||
464 | */ |
||
465 | public function getWard() |
||
469 | |||
470 | /** |
||
471 | * @param string|null $ward |
||
472 | * |
||
473 | * @return GoogleAddress |
||
474 | */ |
||
475 | 18 | public function withWard(string $ward = null) |
|
482 | |||
483 | /** |
||
484 | * @return null|string |
||
485 | */ |
||
486 | 1 | public function getEstablishment() |
|
490 | |||
491 | /** |
||
492 | * @param string|null $establishment |
||
493 | * |
||
494 | * @return GoogleAddress |
||
495 | */ |
||
496 | 18 | public function withEstablishment(string $establishment = null) |
|
503 | |||
504 | /** |
||
505 | * @return AdminLevelCollection |
||
506 | */ |
||
507 | 1 | public function getSubLocalityLevels() |
|
511 | |||
512 | /** |
||
513 | * @param array $subLocalityLevel |
||
514 | * |
||
515 | * @return $this |
||
516 | */ |
||
517 | 18 | public function withSubLocalityLevels(array $subLocalityLevel) |
|
540 | |||
541 | /** |
||
542 | * @return bool |
||
543 | */ |
||
544 | 16 | public function isPartialMatch() |
|
548 | |||
549 | /** |
||
550 | * @param bool $partialMatch |
||
551 | * |
||
552 | * @return $this |
||
553 | */ |
||
554 | 18 | public function withPartialMatch(bool $partialMatch) |
|
561 | } |
||
562 |