Passed
Push — master ( fd93b5...48e916 )
by Doug
40:26 queued 29:39
created

Length::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 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
    private static array $supportedCache = [];
210
211
    abstract public function asMetres(): Metre;
212
213 323
    public function add(self $unit): self
214
    {
215 323
        $resultAsMetres = new Metre($this->asMetres()->getValue() + $unit->asMetres()->getValue());
216 323
        $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

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

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

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

236
        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...
237
    }
238
239 896
    public static function makeUnit(float $measurement, string $srid): self
240
    {
241
        return match ($srid) {
242 896
            self::EPSG_METRE => new Metre($measurement),
243 522
            self::EPSG_KILOMETRE => new Kilometre($measurement),
244 504
            self::EPSG_CENTIMETRE => new Centimetre($measurement),
245 486
            self::EPSG_MILLIMETRE => new Millimetre($measurement),
246 468
            self::EPSG_FOOT => new Foot($measurement),
247 450
            self::EPSG_LINK => new Link($measurement),
248 405
            self::EPSG_BRITISH_CHAIN_BENOIT_1895_B => new BritishChain1895BenoitB($measurement),
249 387
            self::EPSG_BRITISH_FOOT_SEARS_1922 => new BritishFoot1922Sears($measurement),
250 369
            self::EPSG_BRITISH_YARD_SEARS_1922 => new BritishYard1922Sears($measurement),
251 351
            self::EPSG_BRITISH_CHAIN_SEARS_1922 => new BritishChain1922Sears($measurement),
252 333
            self::EPSG_BRITISH_CHAIN_SEARS_1922_TRUNCATED => new BritishChain1922SearsTruncated($measurement),
253 315
            self::EPSG_BRITISH_FOOT_1936 => new BritishFoot1936($measurement),
254 288
            self::EPSG_US_SURVEY_FOOT => new USSurveyFoot($measurement),
255 171
            self::EPSG_CLARKES_FOOT => new ClarkeFoot($measurement),
256 135
            self::EPSG_CLARKES_YARD => new ClarkeYard($measurement),
257 117
            self::EPSG_CLARKES_LINK => new ClarkeLink($measurement),
258 81
            self::EPSG_INDIAN_FOOT => new IndianFoot($measurement),
259 63
            self::EPSG_INDIAN_YARD => new IndianYard($measurement),
260 45
            self::EPSG_GOLD_COAST_FOOT => new GoldCoastFoot($measurement),
261 27
            self::EPSG_GERMAN_LEGAL_METRE => new GermanLegalMetre($measurement),
262 896
            default => throw new UnknownUnitOfMeasureException($srid),
263
        };
264
    }
265
266 27
    public static function getSupportedSRIDs(): array
267
    {
268 27
        if (!self::$supportedCache) {
0 ignored issues
show
Bug Best Practice introduced by
The expression self::supportedCache of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
269
            foreach (static::$sridData as $srid => $data) {
270
                self::$supportedCache[$srid] = $data['name'];
271
            }
272
        }
273
274 27
        return self::$supportedCache;
275
    }
276
277 225
    public static function convert(self $length, string $targetSRID): self
278
    {
279 225
        $conversionRatio = static::makeUnit(1, $targetSRID)->asMetres()->getValue();
280
281 225
        return self::makeUnit($length->asMetres()->getValue() / $conversionRatio, $targetSRID);
282
    }
283
284 225
    public function __toString(): string
285
    {
286 225
        return (string) $this->getValue();
287
    }
288
}
289