Complex classes like MapsLocation 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 MapsLocation, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 15 | class MapsLocation extends MapsBaseElement { | ||
| 16 | |||
| 17 | /** | ||
| 18 | * @since 0.7.1 | ||
| 19 | * | ||
| 20 | * @var float | ||
| 21 | */ | ||
| 22 | protected $latitude; | ||
| 23 | |||
| 24 | /** | ||
| 25 | * @since 0.7.1 | ||
| 26 | * | ||
| 27 | * @var float | ||
| 28 | */ | ||
| 29 | protected $longitude; | ||
| 30 | |||
| 31 | /** | ||
| 32 | * @since 0.7.2 | ||
| 33 | * | ||
| 34 | * @var float | ||
| 35 | */ | ||
| 36 | protected $altitude = 0; | ||
| 37 | |||
| 38 | /** | ||
| 39 | * @since 0.7.1 | ||
| 40 | * | ||
| 41 | * @var string | ||
| 42 | */ | ||
| 43 | protected $address; | ||
| 44 | |||
| 45 | /** | ||
| 46 | * @since 0.7.2 | ||
| 47 | * | ||
| 48 | * @var string | ||
| 49 | */ | ||
| 50 | protected $icon = ''; | ||
| 51 | |||
| 52 | /** | ||
| 53 | * @since 2.0 | ||
| 54 | * | ||
| 55 | * @var string | ||
| 56 | */ | ||
| 57 | protected $group = ''; | ||
| 58 | |||
| 59 | /** | ||
| 60 | * @since 0.7.1 | ||
| 61 | * | ||
| 62 | * @var boolean | ||
| 63 | */ | ||
| 64 | protected $isValid = false; | ||
| 65 | |||
| 66 | |||
| 67 | /** | ||
| 68 | * @since 0.7.1 | ||
| 69 | * | ||
| 70 | * @var string Element of the Maps_COORDS_ enum | ||
| 71 | */ | ||
| 72 | protected $format; | ||
| 73 | |||
| 74 | /** | ||
| 75 | * @since 0.7.1 | ||
| 76 | * | ||
| 77 | * @var boolean | ||
| 78 | */ | ||
| 79 | protected $directional; | ||
| 80 | |||
| 81 | /** | ||
| 82 | * @since 0.7.1 | ||
| 83 | * | ||
| 84 | * @var string | ||
| 85 | */ | ||
| 86 | protected $separator; | ||
| 87 | |||
| 88 | /** | ||
| 89 | * @var string | ||
| 90 | * @since 2.0 | ||
| 91 | */ | ||
| 92 | protected $inlineLabel = ''; | ||
| 93 | |||
| 94 | /** | ||
| 95 | * @var string | ||
| 96 | * @since 2.0 | ||
| 97 | */ | ||
| 98 | protected $visitedIcon = ''; | ||
| 99 | |||
| 100 | /** | ||
| 101 | * Creates and returns a new instance of a MapsLocation from a latitude and longitude. | ||
| 102 | * | ||
| 103 | * @since 1.0 | ||
| 104 | * | ||
| 105 | * @param float $lat | ||
| 106 | * @param float $lon | ||
| 107 | * @param string $format | ||
| 108 | * | ||
| 109 | * @return MapsLocation | ||
| 110 | */ | ||
| 111 | 	public static function newFromLatLon( $lat, $lon, $format = Maps_COORDS_FLOAT ) { | ||
| 114 | |||
| 115 | /** | ||
| 116 | * Creates and returns a new instance of a MapsLocation from an address. | ||
| 117 | * | ||
| 118 | * @since 1.0 | ||
| 119 | * | ||
| 120 | * @param string $address | ||
| 121 | * @param string $format | ||
| 122 | * | ||
| 123 | * @return MapsLocation | ||
| 124 | */ | ||
| 125 | 	public static function newFromAddress( $address, $format = Maps_COORDS_FLOAT ) { | ||
| 128 | |||
| 129 | /** | ||
| 130 | * Constructor. | ||
| 131 | * | ||
| 132 | * @param mixed $coordsOrAddress string or array with lat and lon | ||
| 133 | * @param string $format | ||
| 134 | * @param boolean $directional | ||
| 135 | * @param string $separator | ||
| 136 | * | ||
| 137 | * @since 0.7.1 | ||
| 138 | */ | ||
| 139 | 	public function __construct( $coordsOrAddress = null, $format = Maps_COORDS_FLOAT, $directional = false, $separator = ',' ) { | ||
| 153 | |||
| 154 | /** | ||
| 155 | * Sets the location to a set of coordinates. You can provide a string | ||
| 156 | * of raw coordinates, an array with lat and lon values and false. | ||
| 157 | * | ||
| 158 | * @since 0.7.1 | ||
| 159 | * | ||
| 160 | * @param mixed $coordinates | ||
| 161 | * | ||
| 162 | * @return boolean Success indicator | ||
| 163 | */ | ||
| 164 | 	public function setCoordinates( $coordinates ) { | ||
| 173 | |||
| 174 | /** | ||
| 175 | * Sets the location to an address. | ||
| 176 | * | ||
| 177 | * @since 0.7.1 | ||
| 178 | * | ||
| 179 | * @param string $address | ||
| 180 | * @param boolean $asActualLocation When set to false, the location is not changed, only the address string is. | ||
| 181 | * | ||
| 182 | * @return boolean Success indicator | ||
| 183 | */ | ||
| 184 | 	public function setAddress( $address, $asActualLocation = true ) { | ||
| 193 | |||
| 194 | /** | ||
| 195 | * Returns if the location is valid. | ||
| 196 | * | ||
| 197 | * @since 0.7.1 | ||
| 198 | * | ||
| 199 | * @return boolean | ||
| 200 | */ | ||
| 201 | 	public function isValid() { | ||
| 204 | |||
| 205 | /** | ||
| 206 | * Returns the locations latitude. | ||
| 207 | * | ||
| 208 | * @since 0.7.1 | ||
| 209 | * | ||
| 210 | * @return float | ||
| 211 | */ | ||
| 212 | 	public function getLatitude() { | ||
| 218 | |||
| 219 | /** | ||
| 220 | * Returns the locations longitude. | ||
| 221 | * | ||
| 222 | * @since 0.7.1 | ||
| 223 | * | ||
| 224 | * @return float | ||
| 225 | */ | ||
| 226 | 	public function getLongitude() { | ||
| 232 | |||
| 233 | /** | ||
| 234 | * Returns the locations altitude. | ||
| 235 | * | ||
| 236 | * @since 0.7.3 | ||
| 237 | * | ||
| 238 | * @return float | ||
| 239 | */ | ||
| 240 | 	public function getAltitude() { | ||
| 246 | |||
| 247 | /** | ||
| 248 | * Returns the locations coordinates formatted in the specified notation. | ||
| 249 | * | ||
| 250 | * @since 0.7.1 | ||
| 251 | * | ||
| 252 | * @param string $format Element of the Maps_COORDS_ enum | ||
| 253 | * @param boolean $directional | ||
| 254 | * @param string $separator | ||
| 255 | * | ||
| 256 | * @return string | ||
| 257 | */ | ||
| 258 | 	public function getCoordinates( $format = null, $directional = null, $separator = null ) { | ||
| 269 | |||
| 270 | /** | ||
| 271 | * Returns the address corresponding to this location. | ||
| 272 | * If there is none, and empty sting is returned. | ||
| 273 | * | ||
| 274 | * @since 0.7.1 | ||
| 275 | * | ||
| 276 | * @param boolean $geocodeIfEmpty | ||
| 277 | * | ||
| 278 | * @return string | ||
| 279 | */ | ||
| 280 | 	public function getAddress( $geocodeIfEmpty = true ) { | ||
| 295 | |||
| 296 | |||
| 297 | /** | ||
| 298 | * Returns if there is any icon. | ||
| 299 | * | ||
| 300 | * @since 1.0 | ||
| 301 | * | ||
| 302 | * @return boolean | ||
| 303 | */ | ||
| 304 | 	public function hasIcon() { | ||
| 307 | |||
| 308 | /** | ||
| 309 | * Sets the icon | ||
| 310 | * | ||
| 311 | * @since 0.7.2 | ||
| 312 | * | ||
| 313 | * @param string $icon | ||
| 314 | */ | ||
| 315 | 	public function setIcon( $icon ) { | ||
| 318 | |||
| 319 | /** | ||
| 320 | * Sets the group | ||
| 321 | * | ||
| 322 | * @since 2.0 | ||
| 323 | * | ||
| 324 | * @param string $group | ||
| 325 | */ | ||
| 326 | 	public function setGroup( $group ) { | ||
| 329 | |||
| 330 | /** | ||
| 331 | * Returns the icon. | ||
| 332 | * | ||
| 333 | * @since 0.7.2 | ||
| 334 | * | ||
| 335 | * @return string | ||
| 336 | */ | ||
| 337 | 	public function getIcon() { | ||
| 340 | |||
| 341 | /** | ||
| 342 | * Returns the group. | ||
| 343 | * | ||
| 344 | * @since 2.0 | ||
| 345 | * | ||
| 346 | * @return string | ||
| 347 | */ | ||
| 348 | 	public function getGroup() { | ||
| 351 | |||
| 352 | /** | ||
| 353 | * Returns whether Location is asigned to a group. | ||
| 354 | * | ||
| 355 | * @since 2.0 | ||
| 356 | * | ||
| 357 | * @return string | ||
| 358 | */ | ||
| 359 | 	public function hasGroup() { | ||
| 362 | |||
| 363 | /** | ||
| 364 | * @return string | ||
| 365 | * @since 2.0 | ||
| 366 | */ | ||
| 367 | 	public function getInlineLabel(){ | ||
| 370 | |||
| 371 | /** | ||
| 372 | * @param $label | ||
| 373 | * @since 2.0 | ||
| 374 | */ | ||
| 375 | 	public function setInlineLabel($label){ | ||
| 378 | |||
| 379 | /** | ||
| 380 | * @return bool | ||
| 381 | * @since 2.0 | ||
| 382 | */ | ||
| 383 | 	public function hasInlineLabel(){ | ||
| 386 | |||
| 387 | /** | ||
| 388 | * @return string | ||
| 389 | * @since 2.0 | ||
| 390 | */ | ||
| 391 | 	public function getVisitedIcon() { | ||
| 394 | |||
| 395 | /** | ||
| 396 | * @param $visitedIcon | ||
| 397 | * @since 2.0 | ||
| 398 | */ | ||
| 399 | 	public function setVisitedIcon( $visitedIcon ) { | ||
| 402 | |||
| 403 | /** | ||
| 404 | * @return bool | ||
| 405 | * @since 2.0 | ||
| 406 | */ | ||
| 407 | 	public function hasVisitedIcon(){ | ||
| 410 | |||
| 411 | /** | ||
| 412 | * Returns an object that can directly be converted to JS using json_encode or similar. | ||
| 413 | * | ||
| 414 | * @since 1.0 | ||
| 415 | * | ||
| 416 | * @param string $defText | ||
| 417 | * @param string $defTitle | ||
| 418 | * @param string $defIconUrl | ||
| 419 | * @param string $defGroup | ||
| 420 | * @param string $defInlineLabel | ||
| 421 | * | ||
| 422 | * @return object | ||
| 423 | */ | ||
| 424 | 	public function getJSONObject( $defText = '', $defTitle = '', $defIconUrl = '', $defGroup = '', $defInlineLabel = '', $defVisitedIcon = '' ) { | ||
| 453 | |||
| 454 | } | ||
| 455 | 
This check looks for the bodies of
ifstatements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
ifbodies can be removed. If you have an empty if but statements in theelsebranch, consider inverting the condition.could be turned into
This is much more concise to read.