Passed
Push — 4.x ( 1d49e7...4cde9a )
by Doug
06:44
created

Length   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 286
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 116
dl 0
loc 286
ccs 61
cts 61
cp 1
rs 10
c 0
b 0
f 0
wmc 28

8 Methods

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

214
        $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...
215
216 30
        return new static($resultAsMetres->getValue() / $conversionRatio);
217
    }
218
219 23
    public function subtract(self $unit): self
220
    {
221 23
        $resultAsMetres = new Metre($this->asMetres()->getValue() - $unit->asMetres()->getValue());
222 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

222
        $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...
223
224 23
        return new static($resultAsMetres->getValue() / $conversionRatio);
225
    }
226
227 156
    public function multiply(float $multiplicand): self
228
    {
229 156
        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

229
        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...
230
    }
231
232 25
    public function divide(float $divisor): self
233
    {
234 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

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