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 |
||
| 32 | class LatLongFormatter extends ValueFormatterBase { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Output formats for use with the self::OPT_FORMAT option. |
||
| 36 | */ |
||
| 37 | public const TYPE_FLOAT = 'float'; |
||
| 38 | public const TYPE_DMS = 'dms'; |
||
| 39 | public const TYPE_DM = 'dm'; |
||
| 40 | public const TYPE_DD = 'dd'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The symbols representing the different directions for usage in directional notation. |
||
| 44 | * @since 0.1 |
||
| 45 | */ |
||
| 46 | public const OPT_NORTH_SYMBOL = 'north'; |
||
| 47 | public const OPT_EAST_SYMBOL = 'east'; |
||
| 48 | public const OPT_SOUTH_SYMBOL = 'south'; |
||
| 49 | public const OPT_WEST_SYMBOL = 'west'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The symbols representing degrees, minutes and seconds. |
||
| 53 | * @since 0.1 |
||
| 54 | */ |
||
| 55 | public const OPT_DEGREE_SYMBOL = 'degree'; |
||
| 56 | public const OPT_MINUTE_SYMBOL = 'minute'; |
||
| 57 | public const OPT_SECOND_SYMBOL = 'second'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Flags for use with the self::OPT_SPACING_LEVEL option. |
||
| 61 | */ |
||
| 62 | public const OPT_SPACE_LATLONG = 'latlong'; |
||
| 63 | public const OPT_SPACE_DIRECTION = 'direction'; |
||
| 64 | public const OPT_SPACE_COORDPARTS = 'coordparts'; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Option specifying the output format (also referred to as output type). Must be one of the |
||
| 68 | * self::TYPE_… constants. |
||
| 69 | */ |
||
| 70 | public const OPT_FORMAT = 'geoformat'; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Boolean option specifying if negative coordinates should have minus signs, e.g. "-1°, -2°" |
||
| 74 | * (false) or cardinal directions, e.g. "1° S, 2° W" (true). Default is false. |
||
| 75 | */ |
||
| 76 | public const OPT_DIRECTIONAL = 'directional'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Option for the separator character between latitude and longitude. Defaults to a comma. |
||
| 80 | */ |
||
| 81 | public const OPT_SEPARATOR_SYMBOL = 'separator'; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Option specifying the amount and position of space characters in the output. Must be an array |
||
| 85 | * containing zero or more of the self::OPT_SPACE_… flags. |
||
| 86 | */ |
||
| 87 | public const OPT_SPACING_LEVEL = 'spacing'; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Option specifying the precision in fractional degrees. Must be a number or numeric string. |
||
| 91 | */ |
||
| 92 | public const OPT_PRECISION = 'precision'; |
||
| 93 | |||
| 94 | private const DEFAULT_PRECISION = 1 / 3600; |
||
| 95 | |||
| 96 | 154 | public function __construct( FormatterOptions $options = null ) { |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @see ValueFormatter::format |
||
| 122 | * |
||
| 123 | * Calls formatLatLongValue() using OPT_PRECISION for the $precision parameter. |
||
| 124 | * |
||
| 125 | * @param LatLongValue $value |
||
| 126 | * |
||
| 127 | * @return string Plain text |
||
| 128 | * @throws InvalidArgumentException |
||
| 129 | */ |
||
| 130 | 150 | public function format( $value ): string { |
|
| 137 | |||
| 138 | 149 | private function getPrecisionFromOptions(): float { |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Formats a LatLongValue with the desired precision. |
||
| 154 | * |
||
| 155 | * @since 0.5 |
||
| 156 | * |
||
| 157 | * @param LatLongValue $value |
||
| 158 | * @param float|int $precision The desired precision, given as fractional degrees. |
||
| 159 | * |
||
| 160 | * @return string Plain text |
||
| 161 | * @throws InvalidArgumentException |
||
| 162 | */ |
||
| 163 | 153 | public function formatLatLongValue( LatLongValue $value, ?float $precision ): string { |
|
| 178 | |||
| 179 | /** |
||
| 180 | * @param string $spacingLevel One of the self::OPT_SPACE_… constants |
||
| 181 | * |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | 153 | private function getSpacing( string $spacingLevel ): string { |
|
| 190 | |||
| 191 | 153 | private function formatLatitude( float $latitude, float $precision ): string { |
|
| 198 | |||
| 199 | 152 | private function formatLongitude( float $longitude, float $precision ): string { |
|
| 206 | |||
| 207 | 152 | private function makeDirectionalIfNeeded( string $coordinate, string $positiveSymbol, |
|
| 216 | |||
| 217 | 4 | private function makeDirectional( string $coordinate, string $positiveSymbol, |
|
| 230 | |||
| 231 | 153 | private function formatCoordinate( float $degrees, float $precision ): string { |
|
| 260 | |||
| 261 | 153 | private function roundDegrees( float $degrees, float $precision ): float { |
|
| 268 | |||
| 269 | 34 | private function getInFloatFormat( float $floatDegrees ): string { |
|
| 279 | |||
| 280 | 42 | private function getInDecimalDegreeFormat( float $floatDegrees, float $precision ): string { |
|
| 286 | |||
| 287 | 34 | private function getInDegreeMinuteSecondFormat( float $floatDegrees, float $precision ): string { |
|
| 314 | |||
| 315 | 42 | private function getInDecimalMinuteFormat( float $floatDegrees, float $precision ): string { |
|
| 337 | |||
| 338 | /** |
||
| 339 | * @param float|int $unitsPerDegree The number of target units per degree |
||
| 340 | * (60 for minutes, 3600 for seconds) |
||
| 341 | * @param float|int $degreePrecision |
||
| 342 | * |
||
| 343 | * @return int The number of digits to show after the decimal point |
||
| 344 | * (resp. before, if the result is negative). |
||
| 345 | */ |
||
| 346 | 118 | private function getSignificantDigits( float $unitsPerDegree, float $degreePrecision ): int { |
|
| 349 | |||
| 350 | /** |
||
| 351 | * @param float $number |
||
| 352 | * @param int $digits The number of digits after the decimal point. |
||
| 353 | * |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | 118 | private function formatNumber( float $number, int $digits = 0 ): string { |
|
| 360 | |||
| 361 | } |
||
| 362 |