Passed
Push — 4.x ( e9b635...bc20e1 )
by Doug
07:44
created

Point::generalPolynomialUnitless()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 41
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 41
ccs 20
cts 20
cp 1
rs 9.3222
cc 5
nc 9
nop 11
crap 5

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * PHPCoord.
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace PHPCoord;
10
11
use DateTimeImmutable;
12
use PHPCoord\CoordinateReferenceSystem\CoordinateReferenceSystem;
13
use PHPCoord\CoordinateSystem\Axis;
14
use PHPCoord\UnitOfMeasure\Angle\Angle;
15
use PHPCoord\UnitOfMeasure\Length\Length;
16
use PHPCoord\UnitOfMeasure\Scale\Coefficient;
17
use PHPCoord\UnitOfMeasure\UnitOfMeasure;
18
use Stringable;
19
20
abstract class Point implements Stringable
21
{
22
    protected const NEWTON_RAPHSON_CONVERGENCE = 1e-16;
23
24
    abstract public function getCRS(): CoordinateReferenceSystem;
25
26
    abstract public function getCoordinateEpoch(): ?DateTimeImmutable;
27
28
    abstract public function calculateDistance(self $to): Length;
29
30 186
    protected function getAxisByName(string $name): ?Axis
31
    {
32 186
        foreach ($this->getCRS()->getCoordinateSystem()->getAxes() as $axis) {
33 186
            if ($axis->getName() === $name) {
34 186
                return $axis;
35
            }
36
        }
37
38 130
        return null;
39
    }
40
41 10
    protected static function sign(float $number): int
42
    {
43 10
        if ($number < 0) {
44
            return -1;
45
        }
46
47 10
        return 1;
48
    }
49
50
    /**
51
     * General polynomial.
52
     * @param Coefficient[] $powerCoefficients
53
     */
54 1
    protected function generalPolynomialUnitless(
55
        float $xs,
56
        float $ys,
57
        UnitOfMeasure $ordinate1OfEvaluationPointInSourceCRS,
58
        UnitOfMeasure $ordinate2OfEvaluationPointInSourceCRS,
59
        UnitOfMeasure $ordinate1OfEvaluationPointInTargetCRS,
60
        UnitOfMeasure $ordinate2OfEvaluationPointInTargetCRS,
61
        Coefficient $scalingFactorForSourceCRSCoordDifferences,
62
        Coefficient $scalingFactorForTargetCRSCoordDifferences,
63
        Coefficient $A0,
64
        Coefficient $B0,
65
        array $powerCoefficients
66
    ): array {
67 1
        $xso = $ordinate1OfEvaluationPointInSourceCRS->getValue();
68 1
        $yso = $ordinate2OfEvaluationPointInSourceCRS->getValue();
69 1
        $xto = $ordinate1OfEvaluationPointInTargetCRS->getValue();
70 1
        $yto = $ordinate2OfEvaluationPointInTargetCRS->getValue();
71
72 1
        $U = $scalingFactorForSourceCRSCoordDifferences->asUnity()->getValue() * ($xs - $xso);
73 1
        $V = $scalingFactorForSourceCRSCoordDifferences->asUnity()->getValue() * ($ys - $yso);
74
75 1
        $mTdX = $A0->getValue();
76 1
        foreach ($powerCoefficients as $coefficientName => $coefficientValue) {
77 1
            if ($coefficientName[0] === 'A') {
78 1
                sscanf($coefficientName, 'Au%dv%d', $uPower, $vPower);
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $vPower seems to be never defined.
Loading history...
79 1
                $mTdX += $coefficientValue->getValue() * $U ** $uPower * $V ** $vPower;
80
            }
81
        }
82
83 1
        $mTdY = $B0->getValue();
84 1
        foreach ($powerCoefficients as $coefficientName => $coefficientValue) {
85 1
            if ($coefficientName[0] === 'B') {
86 1
                sscanf($coefficientName, 'Bu%dv%d', $uPower, $vPower);
87 1
                $mTdY += $coefficientValue->getValue() * $U ** $uPower * $V ** $vPower;
88
            }
89
        }
90
91 1
        $xt = $xs - $xso + $xto + $mTdX / $scalingFactorForTargetCRSCoordDifferences->asUnity()->getValue();
92 1
        $yt = $ys - $yso + $yto + $mTdY / $scalingFactorForTargetCRSCoordDifferences->asUnity()->getValue();
93
94 1
        return ['xt' => $xt, 'yt' => $yt];
95
    }
96
97
    /**
98
     * Reversible polynomial.
99
     */
100 2
    protected function reversiblePolynomialUnitless(
101
        float $xs,
102
        float $ys,
103
        Angle $ordinate1OfEvaluationPoint,
104
        Angle $ordinate2OfEvaluationPoint,
105
        Coefficient $scalingFactorForCoordDifferences,
106
        Coefficient $A0,
107
        Coefficient $B0,
108
        array $powerCoefficients
109
    ): array {
110 2
        $xo = $ordinate1OfEvaluationPoint->getValue();
111 2
        $yo = $ordinate2OfEvaluationPoint->getValue();
112
113 2
        $U = $scalingFactorForCoordDifferences->asUnity()->getValue() * ($xs - $xo);
114 2
        $V = $scalingFactorForCoordDifferences->asUnity()->getValue() * ($ys - $yo);
115
116 2
        $mTdX = $A0->getValue();
117 2
        foreach ($powerCoefficients as $coefficientName => $coefficientValue) {
118 2
            if ($coefficientName[0] === 'A') {
119 2
                sscanf($coefficientName, 'Au%dv%d', $uPower, $vPower);
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $vPower seems to be never defined.
Loading history...
120 2
                $mTdX += $coefficientValue->getValue() * $U ** $uPower * $V ** $vPower;
121
            }
122
        }
123
124 2
        $mTdY = $B0->getValue();
125 2
        foreach ($powerCoefficients as $coefficientName => $coefficientValue) {
126 2
            if ($coefficientName[0] === 'B') {
127 2
                sscanf($coefficientName, 'Bu%dv%d', $uPower, $vPower);
128 2
                $mTdY += $coefficientValue->getValue() * $U ** $uPower * $V ** $vPower;
129
            }
130
        }
131
132 2
        $xt = $xs + $mTdX / $scalingFactorForCoordDifferences->asUnity()->getValue();
133 2
        $yt = $ys + $mTdY / $scalingFactorForCoordDifferences->asUnity()->getValue();
134
135 2
        return ['xt' => $xt, 'yt' => $yt];
136
    }
137
}
138