Conditions | 6 |
Paths | 8 |
Total Lines | 78 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
176 | private function inverseVincenty(Coordinate $point1, Coordinate $point2): InverseVincentyBearing |
||
177 | { |
||
178 | $φ1 = deg2rad($point1->getLat()); |
||
179 | $φ2 = deg2rad($point2->getLat()); |
||
180 | $λ1 = deg2rad($point1->getLng()); |
||
181 | $λ2 = deg2rad($point2->getLng()); |
||
182 | |||
183 | $a = $point1->getEllipsoid()->getA(); |
||
184 | $b = $point1->getEllipsoid()->getB(); |
||
185 | $f = 1 / $point1->getEllipsoid()->getF(); |
||
186 | |||
187 | $L = $λ2 - $λ1; |
||
188 | |||
189 | $tanU1 = (1 - $f) * tan($φ1); |
||
190 | $cosU1 = 1 / sqrt(1 + $tanU1 * $tanU1); |
||
191 | $sinU1 = $tanU1 * $cosU1; |
||
192 | $tanU2 = (1 - $f) * tan($φ2); |
||
193 | $cosU2 = 1 / sqrt(1 + $tanU2 * $tanU2); |
||
194 | $sinU2 = $tanU2 * $cosU2; |
||
195 | |||
196 | $λ = $L; |
||
197 | |||
198 | $iterations = 0; |
||
199 | |||
200 | do { |
||
201 | $sinλ = sin($λ); |
||
202 | $cosλ = cos($λ); |
||
203 | $sinSqσ = ($cosU2 * $sinλ) * ($cosU2 * $sinλ) |
||
204 | + ($cosU1 * $sinU2 - $sinU1 * $cosU2 * $cosλ) * ($cosU1 * $sinU2 - $sinU1 * $cosU2 * $cosλ); |
||
205 | $sinσ = sqrt($sinSqσ); |
||
206 | |||
207 | if ($sinσ == 0) { |
||
208 | new InverseVincentyBearing(0, 0, 0); |
||
209 | } |
||
210 | |||
211 | $cosσ = $sinU1 * $sinU2 + $cosU1 * $cosU2 * $cosλ; |
||
212 | $σ = atan2($sinσ, $cosσ); |
||
213 | $sinα = $cosU1 * $cosU2 * $sinλ / $sinσ; |
||
214 | $cosSqα = 1 - $sinα * $sinα; |
||
215 | |||
216 | $cos2σM = 0; |
||
217 | if ($cosSqα !== 0.0) { |
||
218 | $cos2σM = $cosσ - 2 * $sinU1 * $sinU2 / $cosSqα; |
||
219 | } |
||
220 | |||
221 | $C = $f / 16 * $cosSqα * (4 + $f * (4 - 3 * $cosSqα)); |
||
222 | $λp = $λ; |
||
223 | $λ = $L + (1 - $C) * $f * $sinα |
||
224 | * ($σ + $C * $sinσ * ($cos2σM + $C * $cosσ * (-1 + 2 * $cos2σM * $cos2σM))); |
||
225 | } while (abs($λ - $λp) > 1e-12 && ++$iterations < 200); |
||
226 | |||
227 | if ($iterations >= 200) { |
||
228 | throw new NotConvergingException('Inverse Vincenty Formula did not converge'); |
||
229 | } |
||
230 | |||
231 | $uSq = $cosSqα * ($a * $a - $b * $b) / ($b * $b); |
||
232 | $A = 1 + $uSq / 16384 * (4096 + $uSq * (-768 + $uSq * (320 - 175 * $uSq))); |
||
233 | $B = $uSq / 1024 * (256 + $uSq * (-128 + $uSq * (74 - 47 * $uSq))); |
||
234 | $Δσ = $B * $sinσ |
||
235 | * ($cos2σM + $B / 4 |
||
236 | * ($cosσ * (-1 + 2 * $cos2σM * $cos2σM) - $B / 6 |
||
237 | * $cos2σM * (-3 + 4 * $sinσ * $sinσ) |
||
238 | * (-3 + 4 * $cos2σM * $cos2σM) |
||
239 | ) |
||
240 | ); |
||
241 | |||
242 | $s = $b * $A * ($σ - $Δσ); |
||
243 | |||
244 | $α1 = atan2($cosU2 * $sinλ, $cosU1 * $sinU2 - $sinU1 * $cosU2 * $cosλ); |
||
245 | $α2 = atan2($cosU1 * $sinλ, -$sinU1 * $cosU2 + $cosU1 * $sinU2 * $cosλ); |
||
246 | |||
247 | $α1 = fmod($α1 + 2 * M_PI, 2 * M_PI); |
||
248 | $α2 = fmod($α2 + 2 * M_PI, 2 * M_PI); |
||
249 | |||
250 | $s = round($s, 3); |
||
251 | |||
252 | return new InverseVincentyBearing($s, rad2deg($α1), rad2deg($α2)); |
||
253 | } |
||
254 | } |
||
255 |