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

Rate::makeUnit()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 30
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 24
c 0
b 0
f 0
dl 0
loc 30
ccs 23
cts 23
cp 1
rs 6.9666
cc 12
nc 12
nop 2
crap 12

How to fix   Complexity   

Long Method

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:

1
<?php
2
/**
3
 * PHPCoord.
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace PHPCoord\UnitOfMeasure;
10
11
use PHPCoord\Exception\InvalidRateException;
12
use PHPCoord\Exception\UnknownUnitOfMeasureException;
13
use PHPCoord\UnitOfMeasure\Angle\Angle;
14
use PHPCoord\UnitOfMeasure\Angle\ArcSecond;
15
use PHPCoord\UnitOfMeasure\Angle\Radian;
16
use PHPCoord\UnitOfMeasure\Length\Length;
17
use PHPCoord\UnitOfMeasure\Length\Metre;
18
use PHPCoord\UnitOfMeasure\Scale\PartsPerBillion;
19
use PHPCoord\UnitOfMeasure\Scale\PartsPerMillion;
20
use PHPCoord\UnitOfMeasure\Scale\Scale;
21
use PHPCoord\UnitOfMeasure\Scale\Unity;
22
use PHPCoord\UnitOfMeasure\Time\Second;
23
use PHPCoord\UnitOfMeasure\Time\Time;
24
use PHPCoord\UnitOfMeasure\Time\Year;
25
26
class Rate implements UnitOfMeasure
27
{
28
    /**
29
     * arc-seconds per year
30
     * =((pi/180) / 3600) radians per year. Year taken to be IUGS definition of 31556925.445 seconds; see UoM code
31
     * 1029.
32
     */
33
    public const EPSG_ARC_SECONDS_PER_YEAR = 'urn:ogc:def:uom:EPSG::1043';
34
35
    /**
36
     * centimetres per year
37
     * Year taken to be IUGS definition of 31556925.445 seconds; see UoM code 1029.
38
     */
39
    public const EPSG_CENTIMETRES_PER_YEAR = 'urn:ogc:def:uom:EPSG::1034';
40
41
    /**
42
     * metre per second
43
     * SI coherent derived unit (standard unit) for velocity.
44
     */
45
    public const EPSG_METRE_PER_SECOND = 'urn:ogc:def:uom:EPSG::1026';
46
47
    /**
48
     * metres per year
49
     * Year taken to be IUGS definition of 31556925.445 seconds; see UoM code 1029.
50
     */
51
    public const EPSG_METRES_PER_YEAR = 'urn:ogc:def:uom:EPSG::1042';
52
53
    /**
54
     * milliarc-seconds per year
55
     * = ((pi/180) / 3600 / 1000) radians per year. Year taken to be IUGS definition of 31556925.445 seconds; see UoM
56
     * code 1029.
57
     */
58
    public const EPSG_MILLIARC_SECONDS_PER_YEAR = 'urn:ogc:def:uom:EPSG::1032';
59
60
    /**
61
     * millimetres per year
62
     * Year taken to be IUGS definition of 31556925.445 seconds; see UoM code 1029.
63
     */
64
    public const EPSG_MILLIMETRES_PER_YEAR = 'urn:ogc:def:uom:EPSG::1027';
65
66
    /**
67
     * parts per billion per year
68
     * Year taken to be IUGS definition of 31556925.445 seconds; see UoM code 1029. Billion is internationally
69
     * ambiguous, in different languages being 1E+9 and 1E+12. One billion taken here to be 1E+9.
70
     */
71
    public const EPSG_PARTS_PER_BILLION_PER_YEAR = 'urn:ogc:def:uom:EPSG::1030';
72
73
    /**
74
     * parts per million per year
75
     * Year taken to be IUGS definition of 31556925.445 seconds; see UoM code 1029.
76
     */
77
    public const EPSG_PARTS_PER_MILLION_PER_YEAR = 'urn:ogc:def:uom:EPSG::1041';
78
79
    /**
80
     * radian per second
81
     * SI coherent derived unit (standard unit) for angular velocity.
82
     */
83
    public const EPSG_RADIAN_PER_SECOND = 'urn:ogc:def:uom:EPSG::1035';
84
85
    /**
86
     * unity per second
87
     * EPSG standard unit for scale rate. SI coherent derived unit (standard unit) for dimensionless quantity velocity.
88
     */
89
    public const EPSG_UNITY_PER_SECOND = 'urn:ogc:def:uom:EPSG::1036';
90
91
    protected static array $sridData = [
92
        'urn:ogc:def:uom:EPSG::1026' => [
93
            'name' => 'metre per second',
94
        ],
95
        'urn:ogc:def:uom:EPSG::1027' => [
96
            'name' => 'millimetres per year',
97
        ],
98
        'urn:ogc:def:uom:EPSG::1030' => [
99
            'name' => 'parts per billion per year',
100
        ],
101
        'urn:ogc:def:uom:EPSG::1032' => [
102
            'name' => 'milliarc-seconds per year',
103
        ],
104
        'urn:ogc:def:uom:EPSG::1034' => [
105
            'name' => 'centimetres per year',
106
        ],
107
        'urn:ogc:def:uom:EPSG::1035' => [
108
            'name' => 'radian per second',
109
        ],
110
        'urn:ogc:def:uom:EPSG::1036' => [
111
            'name' => 'unity per second',
112
        ],
113
        'urn:ogc:def:uom:EPSG::1041' => [
114
            'name' => 'parts per million per year',
115
        ],
116
        'urn:ogc:def:uom:EPSG::1042' => [
117
            'name' => 'metres per year',
118
        ],
119
        'urn:ogc:def:uom:EPSG::1043' => [
120
            'name' => 'arc-seconds per year',
121
        ],
122
    ];
123
124
    private UnitOfMeasure $change;
125
126
    private Time $time;
127
128 27
    public function __construct(UnitOfMeasure $change, Time $time)
129
    {
130 27
        if ($change instanceof Time) {
131 1
            throw new InvalidRateException('A rate is a change per unit of time, the change cannot be time');
132
        }
133
134 26
        $this->change = $change;
135 26
        $this->time = $time;
136 26
    }
137
138 1
    public function getChange(): UnitOfMeasure
139
    {
140 1
        return $this->change;
141
    }
142
143 2
    public function getChangePerYear(): UnitOfMeasure
144
    {
145 2
        return $this->change->divide($this->time->asYears()->getValue());
0 ignored issues
show
Bug introduced by
The method divide() does not exist on PHPCoord\UnitOfMeasure\UnitOfMeasure. It seems like you code against a sub-type of said class. However, the method does not exist in PHPCoord\UnitOfMeasure\Rate. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

145
        return $this->change->/** @scrutinizer ignore-call */ divide($this->time->asYears()->getValue());
Loading history...
146
    }
147
148 1
    public function getTime(): Time
149
    {
150 1
        return $this->time;
151
    }
152
153 2
    public function getValue(): float
154
    {
155 2
        return $this->change->getValue();
156
    }
157
158 1
    public function getUnitName(): string
159
    {
160 1
        return $this->change->getUnitName() . ' per ' . $this->time->getUnitName();
161
    }
162
163 1
    public function __toString(): string
164
    {
165 1
        return (string) $this->getValue();
166
    }
167
168 21
    public static function makeUnit(float $measurement, string $srid): self
169
    {
170 21
        if (!isset(static::$sridData[$srid])) {
171 1
            throw new UnknownUnitOfMeasureException($srid);
172
        }
173
174
        switch ($srid) {
175 20
            case self::EPSG_RADIAN_PER_SECOND:
176 2
                return new self(new Radian($measurement), new Second(1));
177 18
            case self::EPSG_ARC_SECONDS_PER_YEAR:
178 2
                return new self(new ArcSecond($measurement), new Year(1));
179 16
            case self::EPSG_MILLIARC_SECONDS_PER_YEAR:
180 2
                return new self(Angle::makeUnit($measurement, Angle::EPSG_MILLIARC_SECOND), new Year(1));
181 14
            case self::EPSG_METRE_PER_SECOND:
182 2
                return new self(new Metre($measurement), new Second(1));
183 12
            case self::EPSG_METRES_PER_YEAR:
184 2
                return new self(new Metre($measurement), new Year(1));
185 10
            case self::EPSG_MILLIMETRES_PER_YEAR:
186 2
                return new self(Length::makeUnit($measurement, Length::EPSG_MILLIMETRE), new Year(1));
187 8
            case self::EPSG_CENTIMETRES_PER_YEAR:
188 2
                return new self(Length::makeUnit($measurement, Length::EPSG_CENTIMETRE), new Year(1));
189 6
            case self::EPSG_UNITY_PER_SECOND:
190 2
                return new self(new Unity($measurement), new Second(1));
191 4
            case self::EPSG_PARTS_PER_BILLION_PER_YEAR:
192 2
                return new self(new PartsPerBillion($measurement), new Year(1));
193 2
            case self::EPSG_PARTS_PER_MILLION_PER_YEAR:
194 2
                return new self(new PartsPerMillion($measurement), new Year(1));
195
        }
196
197
        throw new UnknownUnitOfMeasureException($srid); //@codeCoverageIgnore
198
    }
199
200 12
    public static function getSupportedSRIDs(): array
201
    {
202 12
        return array_map(function ($sridData) {return $sridData['name']; }, static::$sridData);
203
    }
204
}
205