Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like LatLng 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 LatLng, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class LatLng |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Latitude |
||
| 22 | * @var float |
||
| 23 | */ |
||
| 24 | protected $lat; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Longitude |
||
| 28 | * @var float |
||
| 29 | */ |
||
| 30 | protected $lng; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Height |
||
| 34 | * @var float |
||
| 35 | */ |
||
| 36 | protected $h; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Reference ellipsoid the co-ordinates are from |
||
| 40 | * @var RefEll |
||
| 41 | */ |
||
| 42 | protected $refEll; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Create a new LatLng object from the given latitude and longitude |
||
| 46 | * @param float $lat |
||
| 47 | * @param float $lng |
||
| 48 | * @param int $height |
||
| 49 | * @param RefEll $refEll |
||
| 50 | */ |
||
| 51 | 31 | public function __construct(float $lat, float $lng, int $height, RefEll $refEll) |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Return a string representation of this LatLng object |
||
| 61 | * @return string |
||
| 62 | */ |
||
| 63 | 13 | public function __toString(): string |
|
| 67 | |||
| 68 | /** |
||
| 69 | * @return float |
||
| 70 | */ |
||
| 71 | 24 | public function getLat(): float |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @return float |
||
| 78 | */ |
||
| 79 | 24 | public function getLng(): float |
|
| 83 | |||
| 84 | /** |
||
| 85 | * @return int |
||
| 86 | */ |
||
| 87 | 10 | public function getH(): int |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @return RefEll |
||
| 94 | */ |
||
| 95 | 9 | public function getRefEll(): RefEll |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Calculate the surface distance between this LatLng object and the one |
||
| 102 | * passed in as a parameter. |
||
| 103 | * |
||
| 104 | * @param LatLng $to a LatLng object to measure the surface distance to |
||
| 105 | * @return int distance in metres |
||
| 106 | */ |
||
| 107 | 2 | public function distance(LatLng $to): int |
|
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * Convert this LatLng object to OSGB36 datum. |
||
| 129 | * Reference values for transformation are taken from OS document |
||
| 130 | * "A Guide to Coordinate Systems in Great Britain" |
||
| 131 | * @return LatLng |
||
| 132 | */ |
||
| 133 | 4 | View Code Duplication | public function toOSGB36(): LatLng |
| 151 | |||
| 152 | /** |
||
| 153 | * Convert this LatLng object to ED50 datum. |
||
| 154 | * Reference values for transformation are taken from http://www.globalmapper.com/helpv9/datum_list.htm |
||
| 155 | * @return LatLng |
||
| 156 | */ |
||
| 157 | 1 | View Code Duplication | public function toED50(): LatLng |
| 175 | |||
| 176 | /** |
||
| 177 | * Convert this LatLng object to NAD27 datum. |
||
| 178 | * Reference values for transformation are taken from Wikipedia |
||
| 179 | * @return LatLng |
||
| 180 | */ |
||
| 181 | 1 | View Code Duplication | public function toNAD27(): LatLng |
| 199 | |||
| 200 | /** |
||
| 201 | * Convert this LatLng object to Ireland 1975 datum. |
||
| 202 | * Reference values for transformation are taken from OSI document |
||
| 203 | * "Making maps compatible with GPS" |
||
| 204 | * @return LatLng |
||
| 205 | */ |
||
| 206 | 1 | View Code Duplication | public function toIreland1975(): LatLng |
| 224 | |||
| 225 | /** |
||
| 226 | * Convert this LatLng object to WGS84 datum |
||
| 227 | * @return LatLng |
||
| 228 | */ |
||
| 229 | 15 | public function toWGS84(): LatLng { |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Transform co-ordinates from one datum to another using a Helmert transformation |
||
| 295 | * @param RefEll $toRefEll |
||
| 296 | * @param float $tranX |
||
| 297 | * @param float $tranY |
||
| 298 | * @param float $tranZ |
||
| 299 | * @param float $scale |
||
| 300 | * @param float $rotX rotation about x-axis in seconds |
||
| 301 | * @param float $rotY rotation about y-axis in seconds |
||
| 302 | * @param float $rotZ rotation about z-axis in seconds |
||
| 303 | * @return LatLng |
||
| 304 | */ |
||
| 305 | 15 | public function transformDatum(RefEll $toRefEll, $tranX, $tranY, $tranZ, $scale, $rotX, $rotY, $rotZ): LatLng |
|
| 316 | |||
| 317 | |||
| 318 | /** |
||
| 319 | * Convert this LatLng object into an OSGB grid reference. Note that this |
||
| 320 | * function does not take into account the bounds of the OSGB grid - |
||
| 321 | * beyond the bounds of the OSGB grid, the resulting OSRef object has no |
||
| 322 | * meaning |
||
| 323 | * |
||
| 324 | * Reference values for transformation are taken from OS document |
||
| 325 | * "A Guide to Coordinate Systems in Great Britain" |
||
| 326 | * |
||
| 327 | * @return OSRef |
||
| 328 | */ |
||
| 329 | 3 | View Code Duplication | public function toOSRef(): OSRef |
| 344 | |||
| 345 | /** |
||
| 346 | * Convert this LatLng object into an ITM grid reference |
||
| 347 | * |
||
| 348 | * @return ITMRef |
||
| 349 | */ |
||
| 350 | 1 | View Code Duplication | public function toITMRef() |
| 365 | |||
| 366 | |||
| 367 | /** |
||
| 368 | * Convert a WGS84 latitude and longitude to an UTM reference |
||
| 369 | * |
||
| 370 | * Reference values for transformation are taken from OS document |
||
| 371 | * "A Guide to Coordinate Systems in Great Britain" |
||
| 372 | * @return UTMRef |
||
| 373 | */ |
||
| 374 | 4 | public function toUTMRef(): UTMRef |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Work out the UTM latitude zone from the latitude |
||
| 415 | * @param float $latitude |
||
| 416 | * @return string |
||
| 417 | */ |
||
| 418 | 4 | private function getUTMLatitudeZoneLetter($latitude): string |
|
| 428 | |||
| 429 | |||
| 430 | /** |
||
| 431 | * Convert a latitude and longitude to easting and northing using a Transverse Mercator projection |
||
| 432 | * Formula for transformation is taken from OS document |
||
| 433 | * "A Guide to Coordinate Systems in Great Britain" |
||
| 434 | * |
||
| 435 | * @param float $scale scale factor on central meridian |
||
| 436 | * @param float $originEasting easting of true origin |
||
| 437 | * @param float $originNorthing northing of true origin |
||
| 438 | * @param float $originLat latitude of true origin |
||
| 439 | * @param float $originLong longitude of true origin |
||
| 440 | * @return array |
||
| 441 | */ |
||
| 442 | 5 | public function toTransverseMercatorEastingNorthing( |
|
| 492 | } |
||
| 493 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.