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 implements ValueFormatter { |
||
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 | private $options; |
||
97 | |||
98 | public function __construct( FormatterOptions $options = null ) { |
||
124 | |||
125 | /** |
||
126 | * @see ValueFormatter::format |
||
127 | * |
||
128 | * Calls formatLatLongValue() using OPT_PRECISION for the $precision parameter. |
||
129 | * |
||
130 | * @param LatLongValue $value |
||
131 | * |
||
132 | * @return string Plain text |
||
133 | * @throws InvalidArgumentException |
||
134 | */ |
||
135 | public function format( $value ): string { |
||
142 | |||
143 | private function getPrecisionFromOptions(): float { |
||
156 | |||
157 | /** |
||
158 | * Formats a LatLongValue with the desired precision. |
||
159 | * |
||
160 | * @since 0.5 |
||
161 | * |
||
162 | * @param LatLongValue $value |
||
163 | * @param float|int $precision The desired precision, given as fractional degrees. |
||
164 | * |
||
165 | * @return string Plain text |
||
166 | * @throws InvalidArgumentException |
||
167 | */ |
||
168 | public function formatLatLongValue( LatLongValue $value, ?float $precision ): string { |
||
183 | |||
184 | /** |
||
185 | * @param string $spacingLevel One of the self::OPT_SPACE_… constants |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | private function getSpacing( string $spacingLevel ): string { |
||
195 | |||
196 | private function formatLatitude( float $latitude, float $precision ): string { |
||
203 | |||
204 | private function formatLongitude( float $longitude, float $precision ): string { |
||
211 | |||
212 | private function makeDirectionalIfNeeded( string $coordinate, string $positiveSymbol, |
||
221 | |||
222 | private function makeDirectional( string $coordinate, string $positiveSymbol, |
||
235 | |||
236 | private function formatCoordinate( float $degrees, float $precision ): string { |
||
261 | |||
262 | private function getUpdatedPrecision( float $precision ): float { |
||
273 | |||
274 | private function roundDegrees( float $degrees, float $precision ): float { |
||
281 | |||
282 | private function getInFloatFormat( float $floatDegrees ): string { |
||
291 | |||
292 | private function getInDecimalDegreeFormat( float $floatDegrees, float $precision ): string { |
||
298 | |||
299 | private function getInDegreeMinuteSecondFormat( float $floatDegrees, float $precision ): string { |
||
326 | |||
327 | private function getInDecimalMinuteFormat( float $floatDegrees, float $precision ): string { |
||
349 | |||
350 | /** |
||
351 | * @param float|int $unitsPerDegree The number of target units per degree |
||
352 | * (60 for minutes, 3600 for seconds) |
||
353 | * @param float|int $degreePrecision |
||
354 | * |
||
355 | * @return int The number of digits to show after the decimal point |
||
356 | * (resp. before, if the result is negative). |
||
357 | */ |
||
358 | private function getSignificantDigits( float $unitsPerDegree, float $degreePrecision ): int { |
||
361 | |||
362 | /** |
||
363 | * @param float $number |
||
364 | * @param int $digits The number of digits after the decimal point. |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | private function formatNumber( float $number, int $digits = 0 ): string { |
||
372 | |||
373 | /** |
||
374 | * @param string $option |
||
375 | * @param mixed $default |
||
376 | */ |
||
377 | private function defaultOption( $option, $default ) { |
||
380 | |||
381 | } |
||
382 |