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 Short Country of the location |
||
| 64 | */ |
||
| 65 | private $shortCountry = ''; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string Short Locality of the location |
||
| 69 | */ |
||
| 70 | private $shortLocality = ''; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var string Short District of the location |
||
| 74 | */ |
||
| 75 | private $shortDistrict = ''; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string Short Town of the location |
||
| 79 | */ |
||
| 80 | private $shortTown = ''; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var string Short Street address |
||
| 84 | */ |
||
| 85 | private $shortStreetAddress = ''; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var boolean Whether the location is valid or not |
||
| 89 | */ |
||
| 90 | private $isValid = true; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Create a new Location object |
||
| 94 | * |
||
| 95 | * @param string $address Address whose detail it is |
||
| 96 | * @param \stdClass $dataFromService The data retrieved from the Geocoding service |
||
| 97 | */ |
||
| 98 | 3 | public function __construct($address, \stdClass $dataFromService) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Checks whether the data passed to the class was valid |
||
| 106 | * |
||
| 107 | * @return boolean True if the data is valid and false otherwise |
||
| 108 | */ |
||
| 109 | 3 | public function isValid() |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Populates the object with the detail from the service |
||
| 116 | * |
||
| 117 | * @param \stdClass $locationDetail The address detail i.e. which was retrieved from the API |
||
| 118 | * |
||
| 119 | * @return boolean True if successfully populated the detail and false otherwise |
||
| 120 | */ |
||
| 121 | 3 | private function populateDetail(\stdClass $locationDetail) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Gets the address |
||
| 164 | * |
||
| 165 | * @param string $default |
||
| 166 | * |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | 2 | public function getAddress($default = '') |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Gets the latitude of the location |
||
| 176 | * |
||
| 177 | * @param string $default |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function getLatitude($default = '') |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Gets the longitude of the location |
||
| 188 | * |
||
| 189 | * @param string $default |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public function getLongitude($default = '') |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Gets the country of the location |
||
| 200 | * |
||
| 201 | * @param string $default |
||
| 202 | * |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | 2 | public function getCountry($default = '') |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Gets the short country of the location |
||
| 212 | * |
||
| 213 | * @param string $default |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | 2 | public function getShortCountry($default = '') |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Gets the locality of the location |
||
| 224 | * |
||
| 225 | * @param string $default |
||
| 226 | * |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | 2 | public function getLocality($default = '') |
|
| 233 | /** |
||
| 234 | * Gets the short locality of the location |
||
| 235 | * |
||
| 236 | * @param string $default |
||
| 237 | * |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | 2 | public function getShortLocality($default = '') |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Gets the district of the location |
||
| 247 | * |
||
| 248 | * @param string $default |
||
| 249 | * |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | 2 | public function getDistrict($default = '') |
|
| 256 | /** |
||
| 257 | * Gets the short district of the location |
||
| 258 | * |
||
| 259 | * @param string $default |
||
| 260 | * |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | 2 | public function getShortDistrict($default = '') |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Gets the post code for the location |
||
| 270 | * |
||
| 271 | * @param string $default |
||
| 272 | * |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | 2 | public function getPostcode($default = '') |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Gets the town for the location |
||
| 282 | * |
||
| 283 | * @param string $default |
||
| 284 | * |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | 1 | public function getTown($default = '') |
|
| 291 | /** |
||
| 292 | * Gets the short town of the location |
||
| 293 | * |
||
| 294 | * @param string $default |
||
| 295 | * |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | public function getShortTown($default = '') |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Gets the street number for the location |
||
| 305 | * |
||
| 306 | * @param string $default |
||
| 307 | * |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | 2 | public function getStreetNumber($default = '') |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Gets the street address |
||
| 317 | * |
||
| 318 | * @param string $default |
||
| 319 | * |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | 1 | public function getStreetAddress($default = '') |
|
| 326 | /** |
||
| 327 | * Gets the short street address of the location |
||
| 328 | * |
||
| 329 | * @param string $default |
||
| 330 | * |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | 1 | public function getShortStreetAddress($default = '') |
|
| 337 | } |
||
| 338 |