Complex classes like LatLongFormatter 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 LatLongFormatter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class LatLongFormatter extends ValueFormatterBase { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Output formats for use with the self::OPT_FORMAT option. |
||
| 34 | */ |
||
| 35 | const TYPE_FLOAT = 'float'; |
||
| 36 | const TYPE_DMS = 'dms'; |
||
| 37 | const TYPE_DM = 'dm'; |
||
| 38 | const TYPE_DD = 'dd'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The symbols representing the different directions for usage in directional notation. |
||
| 42 | * @since 0.1 |
||
| 43 | */ |
||
| 44 | const OPT_NORTH_SYMBOL = 'north'; |
||
| 45 | const OPT_EAST_SYMBOL = 'east'; |
||
| 46 | const OPT_SOUTH_SYMBOL = 'south'; |
||
| 47 | const OPT_WEST_SYMBOL = 'west'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The symbols representing degrees, minutes and seconds. |
||
| 51 | * @since 0.1 |
||
| 52 | */ |
||
| 53 | const OPT_DEGREE_SYMBOL = 'degree'; |
||
| 54 | const OPT_MINUTE_SYMBOL = 'minute'; |
||
| 55 | const OPT_SECOND_SYMBOL = 'second'; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Flags for use with the self::OPT_SPACING_LEVEL option. |
||
| 59 | */ |
||
| 60 | const OPT_SPACE_LATLONG = 'latlong'; |
||
| 61 | const OPT_SPACE_DIRECTION = 'direction'; |
||
| 62 | const OPT_SPACE_COORDPARTS = 'coordparts'; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Option specifying the output format (also referred to as output type). Must be one of the |
||
| 66 | * self::TYPE_… constants. |
||
| 67 | */ |
||
| 68 | const OPT_FORMAT = 'geoformat'; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Boolean option specifying if negative coordinates should have minus signs, e.g. "-1°, -2°" |
||
| 72 | * (false) or cardinal directions, e.g. "1° S, 2° W" (true). Default is false. |
||
| 73 | */ |
||
| 74 | const OPT_DIRECTIONAL = 'directional'; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Option for the separator character between latitude and longitude. Defaults to a comma. |
||
| 78 | */ |
||
| 79 | const OPT_SEPARATOR_SYMBOL = 'separator'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Option specifying the amount and position of space characters in the output. Must be an array |
||
| 83 | * containing zero or more of the self::OPT_SPACE_… flags. |
||
| 84 | */ |
||
| 85 | const OPT_SPACING_LEVEL = 'spacing'; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Option specifying the precision in fractional degrees. Must be a number or numeric string. |
||
| 89 | */ |
||
| 90 | const OPT_PRECISION = 'precision'; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @since 0.1 |
||
| 94 | * |
||
| 95 | * @param FormatterOptions|null $options |
||
| 96 | */ |
||
| 97 | public function __construct( FormatterOptions $options = null ) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @see ValueFormatter::format |
||
| 123 | * |
||
| 124 | * Calls formatLatLongValue() using OPT_PRECISION for the $precision parameter. |
||
| 125 | * |
||
| 126 | * @param LatLongValue $value |
||
| 127 | * |
||
| 128 | * @return string Plain text |
||
| 129 | * @throws InvalidArgumentException |
||
| 130 | */ |
||
| 131 | public function format( $value ) { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Formats a LatLongValue with the desired precision. |
||
| 143 | * |
||
| 144 | * @since 0.5 |
||
| 145 | * |
||
| 146 | * @param LatLongValue $value |
||
| 147 | * @param float|int $precision The desired precision, given as fractional degrees. |
||
| 148 | * |
||
| 149 | * @return string Plain text |
||
| 150 | * @throws InvalidArgumentException |
||
| 151 | */ |
||
| 152 | public function formatLatLongValue( LatLongValue $value, $precision ) { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param string $spacingLevel One of the self::OPT_SPACE_… constants |
||
| 170 | * |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | private function getSpacing( $spacingLevel ) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param float $latitude |
||
| 182 | * @param float|int $precision |
||
| 183 | * |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | private function formatLatitude( $latitude, $precision ) { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param float $longitude |
||
| 196 | * @param float|int $precision |
||
| 197 | * |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | private function formatLongitude( $longitude, $precision ) { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param string $coordinate |
||
| 210 | * @param string $positiveSymbol |
||
| 211 | * @param string $negativeSymbol |
||
| 212 | * |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | private function makeDirectionalIfNeeded( $coordinate, $positiveSymbol, $negativeSymbol ) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param string $coordinate |
||
| 225 | * @param string $positiveSymbol |
||
| 226 | * @param string $negativeSymbol |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | private function makeDirectional( $coordinate, $positiveSymbol, $negativeSymbol ) { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param float $degrees |
||
| 244 | * @param float|int $precision |
||
| 245 | * |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | private function formatCoordinate( $degrees, $precision ) { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Round degrees according to OPT_PRECISION |
||
| 280 | * |
||
| 281 | * @param float $degrees |
||
| 282 | * @param float|int $precision |
||
| 283 | * |
||
| 284 | * @return float |
||
| 285 | */ |
||
| 286 | private function roundDegrees( $degrees, $precision ) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param float $floatDegrees |
||
| 296 | * |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | private function getInFloatFormat( $floatDegrees ) { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param float $floatDegrees |
||
| 312 | * @param float|int $precision |
||
| 313 | * |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | private function getInDecimalDegreeFormat( $floatDegrees, $precision ) { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param float $floatDegrees |
||
| 325 | * @param float|int $precision |
||
| 326 | * |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | private function getInDegreeMinuteSecondFormat( $floatDegrees, $precision ) { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @param float $floatDegrees |
||
| 359 | * @param float|int $precision |
||
| 360 | * |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | private function getInDecimalMinuteFormat( $floatDegrees, $precision ) { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param float|int $unitsPerDegree The number of target units per degree |
||
| 388 | * (60 for minutes, 3600 for seconds) |
||
| 389 | * @param float|int $degreePrecision |
||
| 390 | * |
||
| 391 | * @return int The number of digits to show after the decimal point |
||
| 392 | * (resp. before, if the result is negative). |
||
| 393 | */ |
||
| 394 | private function getSignificantDigits( $unitsPerDegree, $degreePrecision ) { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param float $number |
||
| 400 | * @param int $digits The number of digits after the decimal point. |
||
| 401 | * |
||
| 402 | * @return string |
||
| 403 | */ |
||
| 404 | private function formatNumber( $number, $digits = 0 ) { |
||
| 408 | |||
| 409 | } |
||
| 410 |