Complex classes like Location 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 Location, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Location |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var string Address to which the detail belong |
||
| 14 | */ |
||
| 15 | private $address = ''; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var string Latitude of the location |
||
| 19 | */ |
||
| 20 | private $latitude = ''; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string Longitude of the location |
||
| 24 | */ |
||
| 25 | private $longitude = ''; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string Country of the location |
||
| 29 | */ |
||
| 30 | private $country = ''; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string Locality of the location |
||
| 34 | */ |
||
| 35 | private $locality = ''; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string District of the location |
||
| 39 | */ |
||
| 40 | private $district = ''; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string Postal code of the location |
||
| 44 | */ |
||
| 45 | private $postcode = ''; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string Town of the location |
||
| 49 | */ |
||
| 50 | private $town = ''; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string Street number |
||
| 54 | */ |
||
| 55 | private $streetNumber = ''; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var string Street address |
||
| 59 | */ |
||
| 60 | private $streetAddress = ''; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string Neighborhood address |
||
| 64 | */ |
||
| 65 | private $neighborhood = ''; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string Short Country of the location |
||
| 69 | */ |
||
| 70 | private $shortCountry = ''; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var string Short Locality of the location |
||
| 74 | */ |
||
| 75 | private $shortLocality = ''; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string Short District of the location |
||
| 79 | */ |
||
| 80 | private $shortDistrict = ''; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var string Short Town of the location |
||
| 84 | */ |
||
| 85 | private $shortTown = ''; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var string Short Street address |
||
| 89 | */ |
||
| 90 | private $shortStreetAddress = ''; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var string Short Neighborhood address |
||
| 94 | */ |
||
| 95 | private $shortNeighborhood = ''; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var boolean Whether the location is valid or not |
||
| 99 | */ |
||
| 100 | private $isValid = true; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Create a new Location object |
||
| 104 | * |
||
| 105 | * @param string $address Address whose detail it is |
||
| 106 | * @param \stdClass $dataFromService The data retrieved from the Geocoding service |
||
| 107 | */ |
||
| 108 | 3 | public function __construct($address, \stdClass $dataFromService) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Checks whether the data passed to the class was valid |
||
| 116 | * |
||
| 117 | * @return boolean True if the data is valid and false otherwise |
||
| 118 | */ |
||
| 119 | 3 | public function isValid() |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Populates the object with the detail from the service |
||
| 126 | * |
||
| 127 | * @param \stdClass $locationDetail The address detail i.e. which was retrieved from the API |
||
| 128 | * |
||
| 129 | * @return boolean True if successfully populated the detail and false otherwise |
||
| 130 | */ |
||
| 131 | 3 | private function populateDetail(\stdClass $locationDetail) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Gets the address |
||
| 177 | * |
||
| 178 | * @param string $default |
||
| 179 | * |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | 1 | public function getAddress($default = '') |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Gets the latitude of the location |
||
| 189 | * |
||
| 190 | * @param string $default |
||
| 191 | * |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | public function getLatitude($default = '') |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Gets the longitude of the location |
||
| 201 | * |
||
| 202 | * @param string $default |
||
| 203 | * |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function getLongitude($default = '') |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Gets the country of the location |
||
| 213 | * |
||
| 214 | * @param string $default |
||
| 215 | * |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | 1 | public function getCountry($default = '') |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Gets the short country of the location |
||
| 225 | * |
||
| 226 | * @param string $default |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | 1 | public function getShortCountry($default = '') |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Gets the locality of the location |
||
| 237 | * |
||
| 238 | * @param string $default |
||
| 239 | * |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | 1 | public function getLocality($default = '') |
|
| 246 | /** |
||
| 247 | * Gets the short locality of the location |
||
| 248 | * |
||
| 249 | * @param string $default |
||
| 250 | * |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | 1 | public function getShortLocality($default = '') |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Gets the district of the location |
||
| 260 | * |
||
| 261 | * @param string $default |
||
| 262 | * |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | 1 | public function getDistrict($default = '') |
|
| 269 | /** |
||
| 270 | * Gets the short district of the location |
||
| 271 | * |
||
| 272 | * @param string $default |
||
| 273 | * |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | 1 | public function getShortDistrict($default = '') |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Gets the post code for the location |
||
| 283 | * |
||
| 284 | * @param string $default |
||
| 285 | * |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | 1 | public function getPostcode($default = '') |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Gets the town for the location |
||
| 295 | * |
||
| 296 | * @param string $default |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | 1 | public function getTown($default = '') |
|
| 304 | /** |
||
| 305 | * Gets the short town of the location |
||
| 306 | * |
||
| 307 | * @param string $default |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | public function getShortTown($default = '') |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Gets the street number for the location |
||
| 318 | * |
||
| 319 | * @param string $default |
||
| 320 | * |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | 1 | public function getStreetNumber($default = '') |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Gets the street address |
||
| 330 | * |
||
| 331 | * @param string $default |
||
| 332 | * |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | public function getStreetAddress($default = '') |
||
| 336 | { |
||
| 337 | return $this->streetAddress ?: $default; |
||
| 338 | } |
||
| 339 | /** |
||
| 340 | * Gets the short street address of the location |
||
| 341 | * |
||
| 342 | * @param string $default |
||
| 343 | * |
||
| 344 | * @return string |
||
| 345 | */ |
||
| 346 | public function getShortStreetAddress($default = '') |
||
| 347 | { |
||
| 348 | return $this->shortStreetAddress ?: $default; |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Gets the neighborhood address |
||
| 353 | * |
||
| 354 | * @param string $default |
||
| 355 | * |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | public function getNeighborhood($default = '') |
||
| 362 | /** |
||
| 363 | * Gets the short neighborhood address of the location |
||
| 364 | * |
||
| 365 | * @param string $default |
||
| 366 | * |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | public function getShortNeighborhood($default = '') |
||
| 373 | } |
||
| 374 |