Passed
Push — master ( 1c87d2...a64886 )
by Doug
04:35
created

Length::makeUnit()   D

Complexity

Conditions 21
Paths 21

Size

Total Lines 46
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 21

Importance

Changes 0
Metric Value
cc 21
eloc 42
nc 21
nop 2
dl 0
loc 46
ccs 42
cts 42
cp 1
crap 21
rs 4.1666
c 0
b 0
f 0

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\Length;
10
11
use function array_map;
12
use PHPCoord\Exception\UnknownUnitOfMeasureException;
13
use PHPCoord\UnitOfMeasure\UnitOfMeasure;
14
15
abstract class Length implements UnitOfMeasure
16
{
17
    /**
18
     * British chain (Benoit 1895 B)
19
     * Uses Benoit's 1895 British yard-metre ratio as given by Bomford as 39.370113 inches per metre.  Used in West
20
     * Malaysian mapping.
21
     */
22
    public const EPSG_BRITISH_CHAIN_BENOIT_1895_B = 'urn:ogc:def:uom:EPSG::9062';
23
24
    /**
25
     * British chain (Sears 1922 truncated)
26
     * Uses Sear's 1922 British yard-metre ratio (UoM code 9040) truncated to 6 significant figures; this truncated
27
     * ratio (0.914398, UoM code 9099) then converted to other imperial units. 1 chSe(T) = 22 ydSe(T). Used in
28
     * metrication of Malaya RSO grid.
29
     */
30
    public const EPSG_BRITISH_CHAIN_SEARS_1922_TRUNCATED = 'urn:ogc:def:uom:EPSG::9301';
31
32
    /**
33
     * British chain (Sears 1922)
34
     * Uses Sear's 1922 British yard-metre ratio as given by Bomford as 39.370147 inches per metre.  Used in East
35
     * Malaysian and older New Zealand mapping.
36
     */
37
    public const EPSG_BRITISH_CHAIN_SEARS_1922 = 'urn:ogc:def:uom:EPSG::9042';
38
39
    /**
40
     * British foot (1936)
41
     * For the 1936 retriangulation OSGB defines the relationship of 10 feet of 1796 to the International metre through
42
     * the logarithmic relationship (10^0.48401603 exactly). 1 ft = 0.3048007491…m. Also used for metric conversions
43
     * in Ireland.
44
     */
45
    public const EPSG_BRITISH_FOOT_1936 = 'urn:ogc:def:uom:EPSG::9095';
46
47
    /**
48
     * British foot (Sears 1922)
49
     * Uses Sear's 1922 British yard-metre ratio as given by Bomford as 39.370147 inches per metre.  Used in East
50
     * Malaysian and older New Zealand mapping.
51
     */
52
    public const EPSG_BRITISH_FOOT_SEARS_1922 = 'urn:ogc:def:uom:EPSG::9041';
53
54
    /**
55
     * British yard (Sears 1922)
56
     * Uses Sear's 1922 British yard-metre ratio as given by Bomford as 39.370147 inches per metre.  Used in East
57
     * Malaysian and older New Zealand mapping.
58
     */
59
    public const EPSG_BRITISH_YARD_SEARS_1922 = 'urn:ogc:def:uom:EPSG::9040';
60
61
    /**
62
     * Clarke's foot
63
     * Assumes Clarke's 1865 ratio of 1 British foot = 0.3047972654 French legal metres applies to the international
64
     * metre.   Used in older Australian, southern African & British West Indian mapping.
65
     */
66
    public const EPSG_CLARKES_FOOT = 'urn:ogc:def:uom:EPSG::9005';
67
68
    /**
69
     * Clarke's link
70
     * =1/100 Clarke's chain. Assumes Clarke's 1865 ratio of 1 British foot = 0.3047972654 French legal metres applies
71
     * to the international metre.   Used in older Australian, southern African & British West Indian mapping.
72
     */
73
    public const EPSG_CLARKES_LINK = 'urn:ogc:def:uom:EPSG::9039';
74
75
    /**
76
     * Clarke's yard
77
     * =3 Clarke's feet.  Assumes Clarke's 1865 ratio of 1 British foot = 0.3047972654 French legal metres applies to
78
     * the international metre.   Used in older Australian, southern African & British West Indian mapping.
79
     */
80
    public const EPSG_CLARKES_YARD = 'urn:ogc:def:uom:EPSG::9037';
81
82
    /**
83
     * German legal metre
84
     * Used in Namibia.
85
     */
86
    public const EPSG_GERMAN_LEGAL_METRE = 'urn:ogc:def:uom:EPSG::9031';
87
88
    /**
89
     * Gold Coast foot
90
     * Used in Ghana and some adjacent parts of British west Africa prior to metrication, except for the metrication of
91
     * projection defining parameters when British foot (Sears 1922) used.
92
     */
93
    public const EPSG_GOLD_COAST_FOOT = 'urn:ogc:def:uom:EPSG::9094';
94
95
    /**
96
     * Indian foot
97
     * Indian Foot = 0.99999566 British feet (A.R.Clarke 1865).  British yard (= 3 British feet) taken to be
98
     * J.S.Clark's 1865 value of 0.9144025 metres.
99
     */
100
    public const EPSG_INDIAN_FOOT = 'urn:ogc:def:uom:EPSG::9080';
101
102
    /**
103
     * Indian yard
104
     * Indian Foot = 0.99999566 British feet (A.R.Clarke 1865).  British yard (= 3 British feet) taken to be
105
     * J.S.Clark's 1865 value of 0.9144025 metres.
106
     */
107
    public const EPSG_INDIAN_YARD = 'urn:ogc:def:uom:EPSG::9084';
108
109
    /**
110
     * US survey foot
111
     * Used in USA.
112
     */
113
    public const EPSG_US_SURVEY_FOOT = 'urn:ogc:def:uom:EPSG::9003';
114
115
    /**
116
     * centimetre.
117
     */
118
    public const EPSG_CENTIMETRE = 'urn:ogc:def:uom:EPSG::1033';
119
120
    /**
121
     * foot.
122
     */
123
    public const EPSG_FOOT = 'urn:ogc:def:uom:EPSG::9002';
124
125
    /**
126
     * kilometre.
127
     */
128
    public const EPSG_KILOMETRE = 'urn:ogc:def:uom:EPSG::9036';
129
130
    /**
131
     * link
132
     * =1/100 international chain.
133
     */
134
    public const EPSG_LINK = 'urn:ogc:def:uom:EPSG::9098';
135
136
    /**
137
     * metre
138
     * SI base unit for length.
139
     */
140
    public const EPSG_METRE = 'urn:ogc:def:uom:EPSG::9001';
141
142
    /**
143
     * millimetre.
144
     */
145
    public const EPSG_MILLIMETRE = 'urn:ogc:def:uom:EPSG::1025';
146
147
    protected static array $sridData = [
148
        'urn:ogc:def:uom:EPSG::1025' => [
149
            'name' => 'millimetre',
150
        ],
151
        'urn:ogc:def:uom:EPSG::1033' => [
152
            'name' => 'centimetre',
153
        ],
154
        'urn:ogc:def:uom:EPSG::9001' => [
155
            'name' => 'metre',
156
        ],
157
        'urn:ogc:def:uom:EPSG::9002' => [
158
            'name' => 'foot',
159
        ],
160
        'urn:ogc:def:uom:EPSG::9003' => [
161
            'name' => 'US survey foot',
162
        ],
163
        'urn:ogc:def:uom:EPSG::9005' => [
164
            'name' => 'Clarke\'s foot',
165
        ],
166
        'urn:ogc:def:uom:EPSG::9031' => [
167
            'name' => 'German legal metre',
168
        ],
169
        'urn:ogc:def:uom:EPSG::9036' => [
170
            'name' => 'kilometre',
171
        ],
172
        'urn:ogc:def:uom:EPSG::9037' => [
173
            'name' => 'Clarke\'s yard',
174
        ],
175
        'urn:ogc:def:uom:EPSG::9039' => [
176
            'name' => 'Clarke\'s link',
177
        ],
178
        'urn:ogc:def:uom:EPSG::9040' => [
179
            'name' => 'British yard (Sears 1922)',
180
        ],
181
        'urn:ogc:def:uom:EPSG::9041' => [
182
            'name' => 'British foot (Sears 1922)',
183
        ],
184
        'urn:ogc:def:uom:EPSG::9042' => [
185
            'name' => 'British chain (Sears 1922)',
186
        ],
187
        'urn:ogc:def:uom:EPSG::9062' => [
188
            'name' => 'British chain (Benoit 1895 B)',
189
        ],
190
        'urn:ogc:def:uom:EPSG::9080' => [
191
            'name' => 'Indian foot',
192
        ],
193
        'urn:ogc:def:uom:EPSG::9084' => [
194
            'name' => 'Indian yard',
195
        ],
196
        'urn:ogc:def:uom:EPSG::9094' => [
197
            'name' => 'Gold Coast foot',
198
        ],
199
        'urn:ogc:def:uom:EPSG::9095' => [
200
            'name' => 'British foot (1936)',
201
        ],
202
        'urn:ogc:def:uom:EPSG::9098' => [
203
            'name' => 'link',
204
        ],
205
        'urn:ogc:def:uom:EPSG::9301' => [
206
            'name' => 'British chain (Sears 1922 truncated)',
207
        ],
208
    ];
209
210
    abstract public function asMetres(): Metre;
211
212 30
    public function add(self $unit): self
213
    {
214 30
        $resultAsMetres = new Metre($this->asMetres()->getValue() + $unit->asMetres()->getValue());
215 30
        $conversionRatio = (new static(1))->asMetres()->getValue();
0 ignored issues
show
Unused Code introduced by
The call to PHPCoord\UnitOfMeasure\L...h\Length::__construct() has too many arguments starting with 1. ( Ignorable by Annotation )

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

215
        $conversionRatio = (/** @scrutinizer ignore-call */ new static(1))->asMetres()->getValue();

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
216
217 30
        return new static($resultAsMetres->getValue() / $conversionRatio);
218
    }
219
220 23
    public function subtract(self $unit): self
221
    {
222 23
        $resultAsMetres = new Metre($this->asMetres()->getValue() - $unit->asMetres()->getValue());
223 23
        $conversionRatio = (new static(1))->asMetres()->getValue();
0 ignored issues
show
Unused Code introduced by
The call to PHPCoord\UnitOfMeasure\L...h\Length::__construct() has too many arguments starting with 1. ( Ignorable by Annotation )

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

223
        $conversionRatio = (/** @scrutinizer ignore-call */ new static(1))->asMetres()->getValue();

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
224
225 23
        return new static($resultAsMetres->getValue() / $conversionRatio);
226
    }
227
228 157
    public function multiply(float $multiplicand): self
229
    {
230 157
        return new static($this->getValue() * $multiplicand);
0 ignored issues
show
Unused Code introduced by
The call to PHPCoord\UnitOfMeasure\L...h\Length::__construct() has too many arguments starting with $this->getValue() * $multiplicand. ( Ignorable by Annotation )

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

230
        return /** @scrutinizer ignore-call */ new static($this->getValue() * $multiplicand);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
231
    }
232
233 25
    public function divide(float $divisor): self
234
    {
235 25
        return new static($this->getValue() / $divisor);
0 ignored issues
show
Unused Code introduced by
The call to PHPCoord\UnitOfMeasure\L...h\Length::__construct() has too many arguments starting with $this->getValue() / $divisor. ( Ignorable by Annotation )

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

235
        return /** @scrutinizer ignore-call */ new static($this->getValue() / $divisor);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
236
    }
237
238 265
    public static function makeUnit(float $measurement, string $srid): self
239
    {
240
        switch ($srid) {
241 265
            case self::EPSG_METRE:
242 217
                return new Metre($measurement);
243 62
            case self::EPSG_KILOMETRE:
244 2
                return new Kilometre($measurement);
245 60
            case self::EPSG_CENTIMETRE:
246 4
                return new Centimetre($measurement);
247 56
            case self::EPSG_MILLIMETRE:
248 4
                return new Millimetre($measurement);
249 52
            case self::EPSG_FOOT:
250 5
                return new Foot($measurement);
251 50
            case self::EPSG_LINK:
252 5
                return new Link($measurement);
253 45
            case self::EPSG_BRITISH_CHAIN_BENOIT_1895_B:
254 2
                return new BritishChain1895BenoitB($measurement);
255 43
            case self::EPSG_BRITISH_FOOT_SEARS_1922:
256 2
                return new BritishFoot1922Sears($measurement);
257 41
            case self::EPSG_BRITISH_YARD_SEARS_1922:
258 2
                return new BritishYard1922Sears($measurement);
259 39
            case self::EPSG_BRITISH_CHAIN_SEARS_1922:
260 2
                return new BritishChain1922Sears($measurement);
261 37
            case self::EPSG_BRITISH_CHAIN_SEARS_1922_TRUNCATED:
262 2
                return new BritishChain1922SearsTruncated($measurement);
263 35
            case self::EPSG_BRITISH_FOOT_1936:
264 3
                return new BritishFoot1936($measurement);
265 32
            case self::EPSG_US_SURVEY_FOOT:
266 13
                return new USSurveyFoot($measurement);
267 19
            case self::EPSG_CLARKES_FOOT:
268 6
                return new ClarkeFoot($measurement);
269 15
            case self::EPSG_CLARKES_YARD:
270 2
                return new ClarkeYard($measurement);
271 13
            case self::EPSG_CLARKES_LINK:
272 4
                return new ClarkeLink($measurement);
273 9
            case self::EPSG_INDIAN_FOOT:
274 2
                return new IndianFoot($measurement);
275 7
            case self::EPSG_INDIAN_YARD:
276 2
                return new IndianYard($measurement);
277 5
            case self::EPSG_GOLD_COAST_FOOT:
278 2
                return new GoldCoastFoot($measurement);
279 3
            case self::EPSG_GERMAN_LEGAL_METRE:
280 2
                return new GermanLegalMetre($measurement);
281
        }
282
283 1
        throw new UnknownUnitOfMeasureException($srid);
284
    }
285
286 46
    public static function getSupportedSRIDs(): array
287
    {
288 46
        return array_map(function ($sridData) {return $sridData['name']; }, static::$sridData);
289
    }
290
291 187
    public static function convert(self $length, string $targetSRID): self
292
    {
293 187
        $conversionRatio = static::makeUnit(1, $targetSRID)->asMetres()->getValue();
294
295 187
        return self::makeUnit($length->asMetres()->getValue() / $conversionRatio, $targetSRID);
296
    }
297
298 25
    public function __toString(): string
299
    {
300 25
        return (string) $this->getValue();
301
    }
302
}
303