| Total Complexity | 51 |
| Total Lines | 221 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like MapCoordinates 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.
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 MapCoordinates, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class MapCoordinates |
||
| 18 | { |
||
| 19 | const DECIMAL = 'decimal'; |
||
| 20 | const DEGREES = 'degrees'; |
||
| 21 | const CODE_PLUS = 'codeplus'; |
||
| 22 | |||
| 23 | /** @var string[] Coordinate formats */ |
||
| 24 | const COORDINATE_FORMATS = [ |
||
| 25 | self::DECIMAL => 'LBL_DECIMAL', |
||
| 26 | self::DEGREES => 'LBL_DEGREES', |
||
| 27 | self::CODE_PLUS => 'LBL_CODE_PLUS' |
||
| 28 | ]; |
||
| 29 | /** @var array Coordinate format validators */ |
||
| 30 | const VALIDATORS = [ |
||
| 31 | self::DECIMAL => ['lat' => 'Double', 'lon' => 'Double'], |
||
| 32 | self::DEGREES => ['lat' => 'Text', 'lon' => 'Text'], |
||
| 33 | self::CODE_PLUS => 'Text', |
||
| 34 | 'type' => 'Standard', |
||
| 35 | ]; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Converting coordinates from formats: {@see self::COORDINATE_FORMATS}. |
||
| 39 | * |
||
| 40 | * @param string $from |
||
| 41 | * @param string $to |
||
| 42 | * @param mixed $value |
||
| 43 | * |
||
| 44 | * @return mixed |
||
| 45 | */ |
||
| 46 | public static function convert(string $from, string $to, $value) |
||
| 47 | { |
||
| 48 | if ($from === $to) { |
||
| 49 | return $value; |
||
| 50 | } |
||
| 51 | switch ($from) { |
||
| 52 | case self::DECIMAL: |
||
| 53 | ['lat' => $lat, 'lon' => $lon] = $value; |
||
| 54 | break; |
||
| 55 | case self::DEGREES: |
||
| 56 | $lat = self::degreesToDecimal($value['lat']); |
||
| 57 | $lon = self::degreesToDecimal($value['lon']); |
||
| 58 | break; |
||
| 59 | case self::CODE_PLUS: |
||
| 60 | ['lat' => $lat, 'lon' => $lon] = self::codePlusToDecimal($value); |
||
| 61 | break; |
||
| 62 | default: |
||
| 63 | throw new \App\Exceptions\AppException('ERR_NOT_ALLOWED_VALUE||' . $from); |
||
| 64 | } |
||
| 65 | switch ($to) { |
||
| 66 | case self::DECIMAL: |
||
| 67 | $return = ['lat' => $lat, 'lon' => $lon]; |
||
| 68 | break; |
||
| 69 | case self::DEGREES: |
||
| 70 | $return = ['lat' => self::decimalToDegrees($lat, 'lat'), 'lon' => self::decimalToDegrees($lon, 'lon')]; |
||
| 71 | break; |
||
| 72 | case self::CODE_PLUS: |
||
| 73 | $return = self::decimalToCodePlus($lat, $lon); |
||
| 74 | break; |
||
| 75 | default: |
||
| 76 | throw new \App\Exceptions\AppException('ERR_NOT_ALLOWED_VALUE||' . $to); |
||
| 77 | } |
||
| 78 | return $return; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Convert coordinates from decimal to degrees. |
||
| 83 | * |
||
| 84 | * @param string $coord Coordinates in decimal, e.g. 52.23155431436567 |
||
| 85 | * @param string $type Type: `lat` or `lon` |
||
| 86 | * @param int $precision Precision, default `4` |
||
| 87 | * |
||
| 88 | * @return string Coordinates in degrees, e.g. `52°13'53.5955"N` |
||
| 89 | */ |
||
| 90 | public static function decimalToDegrees(string $coord, string $type, int $precision = 4): string |
||
| 91 | { |
||
| 92 | if ('lat' === $type) { |
||
| 93 | $dir = $coord < 0 ? 'S' : 'N'; |
||
| 94 | } else { |
||
| 95 | $dir = $coord < 0 ? 'W' : 'E'; |
||
| 96 | } |
||
| 97 | $vars = explode('.', $coord, 2); |
||
| 98 | if (isset($vars[1])) { |
||
| 99 | $val = (float) ('0.' . ($vars[1] ?? 0)) * 3600; |
||
| 100 | $min = floor($val / 60); |
||
| 101 | $sec = round($val - ($min * 60), $precision); |
||
| 102 | if (0 == $sec) { |
||
| 103 | return sprintf("%s°%02d'%s", $vars[0], $min, $dir); |
||
| 104 | } |
||
| 105 | return sprintf("%s°%02d'%s\"%s", $vars[0], $min, $sec, $dir); |
||
| 106 | } |
||
| 107 | return sprintf('%s°%s', $vars[0], $dir); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Convert coordinates from degrees to decimal. |
||
| 112 | * |
||
| 113 | * @param string $coord Coordinates in degrees, e.g. `21°0'17.983"E` |
||
| 114 | * |
||
| 115 | * @return string|null Coordinates in decimal, e.g. `21.004995277778` |
||
| 116 | */ |
||
| 117 | public static function degreesToDecimal(string $coord): ?string |
||
| 118 | { |
||
| 119 | if (($dots = substr_count($coord, '.')) > 1) { |
||
| 120 | if (2 < \count(explode(' ', trim(preg_replace('/[a-zA-Z]/', '', preg_replace('/\./', ' ', $coord, $dots - 1)))))) { |
||
| 121 | $coord = preg_replace('/\./', ' ', $coord, $dots - 1); |
||
| 122 | } else { |
||
| 123 | $coord = str_replace('.', ' ', $coord); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | $coord = trim(str_replace(['º', '°', "'", '"', ' '], ' ', trim($coord))); |
||
| 127 | $coord = substr($coord, 0, 1) . str_replace('-', ' ', substr($coord, 1)); |
||
| 128 | if ($coord) { |
||
| 129 | $direction = 1; |
||
| 130 | if (preg_match('/^(-?\\d{1,3})\\s+(\\d{1,3})\\s*(\\d*(?:\\.\\d*)?)\\s*([nsewoNSEWO]?)$/', $coord, $matches)) { |
||
| 131 | // `50°12'13.1188" N` , direction at the end of the string |
||
| 132 | $deg = (int) ($matches[1]); |
||
| 133 | $min = (int) ($matches[2]); |
||
| 134 | $sec = (float) ($matches[3]); |
||
| 135 | $dir = strtoupper($matches[4]); |
||
| 136 | if ('S' === $dir || 'W' === $dir || $deg < 0) { |
||
| 137 | $direction = -1; |
||
| 138 | $deg = abs($deg); |
||
| 139 | } |
||
| 140 | $decimal = ($deg + ($min / 60) + ($sec / 3600)) * $direction; |
||
| 141 | } elseif (preg_match('/^([nsewoNSEWO]?)\\s*(\\d{1,3})\\s+(\\d{1,3})\\s*(\\d*\\.?\\d*)$/', $coord, $matches)) { |
||
| 142 | // `N 50°12'13.1188"` , direction at the start of the string |
||
| 143 | $dir = strtoupper($matches[1]); |
||
| 144 | $deg = (int) ($matches[2]); |
||
| 145 | $min = (int) ($matches[3]); |
||
| 146 | $sec = (float) ($matches[4]); |
||
| 147 | if ('S' === $dir || 'W' === $dir) { |
||
| 148 | $direction = -1; |
||
| 149 | } |
||
| 150 | $decimal = ($deg + ($min / 60) + ($sec / 3600)) * $direction; |
||
| 151 | } elseif (preg_match('/^(-?\\d+(?:\\.\\d+)?)\\s*([nsewNSEW]?)$/', $coord, $matches)) { |
||
| 152 | $dir = strtoupper($matches[2]); |
||
| 153 | if ('S' === $dir || 'W' === $dir) { |
||
| 154 | $direction = -1; |
||
| 155 | } |
||
| 156 | $decimal = $matches[1] * $direction; |
||
| 157 | } elseif (preg_match('/^([nsewNSEW]?)\\s*(\\d+(?:\\.\\d+)?)$/', $coord, $matches)) { |
||
| 158 | $dir = strtoupper($matches[1]); |
||
| 159 | if ('S' === $dir || 'W' === $dir) { |
||
| 160 | $direction = -1; |
||
| 161 | } |
||
| 162 | $decimal = $matches[2] * $direction; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | return isset($decimal) ? preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $decimal) : null; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Convert coordinates from decimal to full Open Location Code. |
||
| 170 | * |
||
| 171 | * @see https://plus.codes/ |
||
| 172 | * |
||
| 173 | * @param float $lat A latitude in signed decimal degrees. |
||
| 174 | * Will be clipped to the range -90 to 90, e.g. `52.231313` |
||
| 175 | * @param float $lon A longitude in signed decimal degrees. |
||
| 176 | * Will be normalized to the range -180 to 180, e.g. `21.004562` |
||
| 177 | * |
||
| 178 | * @return string Full Open Location Code., e.g. `9G4362J3+GR` |
||
| 179 | */ |
||
| 180 | public static function decimalToCodePlus(float $lat, float $lon): string |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Undocumented function. |
||
| 187 | * |
||
| 188 | * @see https://plus.codes/ |
||
| 189 | * |
||
| 190 | * @param string $coord Full Open Location Code., e.g. `9G4362J3+GR` |
||
| 191 | * |
||
| 192 | * @return float[] Coordinates in decimal, e.g. `[lat=>52.2313125,lon=>21.0045625]` |
||
| 193 | */ |
||
| 194 | public static function codePlusToDecimal(string $coord): array |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Update of coordinates on the map. |
||
| 202 | * |
||
| 203 | * @param Vtiger_Record_Model $recordModel |
||
|
|
|||
| 204 | * @param string $fieldName |
||
| 205 | */ |
||
| 206 | public static function updateMapCoordinates(\Vtiger_Record_Model $recordModel, $fieldName) |
||
| 242 |