Complex classes like Coordinate 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 Coordinate, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Coordinate implements CoordinateInterface, \JsonSerializable |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * The latitude of the coordinate. |
||
| 26 | * |
||
| 27 | * @var double |
||
| 28 | */ |
||
| 29 | protected $latitude; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The longitude of the coordinate. |
||
| 33 | * |
||
| 34 | * @var double |
||
| 35 | */ |
||
| 36 | protected $longitude; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The selected ellipsoid. |
||
| 40 | * |
||
| 41 | * @var Ellipsoid |
||
| 42 | */ |
||
| 43 | protected $ellipsoid; |
||
| 44 | |||
| 45 | |||
| 46 | /** |
||
| 47 | * The precision to use to compare big numbers |
||
| 48 | * |
||
| 49 | * @var integer |
||
| 50 | */ |
||
| 51 | private $precision = 8; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Set the latitude and the longitude of the coordinates into an selected ellipsoid. |
||
| 55 | * |
||
| 56 | * @param Location|array|string $coordinates The coordinates. |
||
| 57 | * @param Ellipsoid $ellipsoid The selected ellipsoid (WGS84 by default). |
||
| 58 | * |
||
| 59 | * @throws InvalidArgumentException |
||
| 60 | */ |
||
| 61 | 250 | public function __construct($coordinates, Ellipsoid $ellipsoid = null) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritDoc} |
||
| 84 | */ |
||
| 85 | 199 | public function normalizeLatitude($latitude) |
|
| 91 | |||
| 92 | /** |
||
| 93 | 205 | * {@inheritDoc} |
|
| 94 | */ |
||
| 95 | 205 | public function normalizeLongitude($longitude) |
|
| 107 | |||
| 108 | 196 | /** |
|
| 109 | * {@inheritDoc} |
||
| 110 | 196 | */ |
|
| 111 | 196 | public function setLatitude($latitude) |
|
| 115 | |||
| 116 | 195 | /** |
|
| 117 | * {@inheritDoc} |
||
| 118 | 195 | */ |
|
| 119 | public function getLatitude() |
||
| 123 | |||
| 124 | 196 | /** |
|
| 125 | * {@inheritDoc} |
||
| 126 | 196 | */ |
|
| 127 | 196 | public function setLongitude($longitude) |
|
| 131 | |||
| 132 | 195 | /** |
|
| 133 | * {@inheritDoc} |
||
| 134 | 195 | */ |
|
| 135 | public function getLongitude() |
||
| 139 | |||
| 140 | 106 | /** |
|
| 141 | * {@inheritDoc} |
||
| 142 | 106 | */ |
|
| 143 | public function getEllipsoid() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Creates a valid and acceptable geographic coordinates. |
||
| 150 | * |
||
| 151 | * @param string $coordinates |
||
| 152 | 157 | * |
|
| 153 | * @throws InvalidArgumentException |
||
| 154 | 157 | */ |
|
| 155 | 1 | public function setFromString($coordinates) |
|
| 169 | |||
| 170 | 3 | /** |
|
| 171 | * @return integer |
||
| 172 | 3 | */ |
|
| 173 | public function getPrecision() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param integer $precision |
||
| 180 | * @return $this |
||
| 181 | */ |
||
| 182 | public function setPrecision($precision) |
||
| 188 | |||
| 189 | |||
| 190 | /** |
||
| 191 | * Converts a valid and acceptable geographic coordinates to decimal degrees coordinate. |
||
| 192 | * |
||
| 193 | * @param string $coordinates A valid and acceptable geographic coordinates. |
||
| 194 | * |
||
| 195 | * @return array An array of coordinate in decimal degree. |
||
| 196 | * |
||
| 197 | * @throws InvalidArgumentException |
||
| 198 | 156 | * |
|
| 199 | * @see http://en.wikipedia.org/wiki/Geographic_coordinate_conversion |
||
| 200 | */ |
||
| 201 | 156 | private function toDecimalDegrees($coordinates) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * {@inheritDoc} |
||
| 264 | */ |
||
| 265 | public function jsonSerialize() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Returns a boolean determining coordinates equality |
||
| 272 | 3 | * @param Coordinate $coordinate |
|
| 273 | 3 | * @return boolean |
|
| 274 | */ |
||
| 275 | public function isEqual(Coordinate $coordinate) { |
||
| 278 | } |
||
| 279 |
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.