Passed
Push — 4.x ( 2e43a7...18aaa8 )
by Doug
06:37
created

UnitOfMeasureFactory::getSupportedSRIDs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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\UnknownUnitOfMeasureException;
12
use PHPCoord\UnitOfMeasure\Angle\Angle;
13
use PHPCoord\UnitOfMeasure\Length\Length;
14
use PHPCoord\UnitOfMeasure\Scale\Scale;
15
use PHPCoord\UnitOfMeasure\Time\Time;
16
17
use function array_merge;
18
19
class UnitOfMeasureFactory
20
{
21
    /**
22
     * @var array<string, array<string, string>>
23
     */
24
    private static array $sridCache = [];
25
26 261
    public static function makeUnit($measurement, string $srid): UnitOfMeasure
27
    {
28 261
        if (!self::$sridCache) {
0 ignored issues
show
Bug Best Practice introduced by
The expression self::sridCache 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...
29 9
            self::$sridCache['angle'] = Angle::getSupportedSRIDs();
30 9
            self::$sridCache['length'] = Length::getSupportedSRIDs();
31 9
            self::$sridCache['scale'] = Scale::getSupportedSRIDs();
32 9
            self::$sridCache['time'] = Time::getSupportedSRIDs();
33 9
            self::$sridCache['rate'] = Rate::getSupportedSRIDs();
34
        }
35
36 261
        if (isset(self::$sridCache['angle'][$srid])) {
37 242
            return Angle::makeUnit($measurement, $srid);
38
        }
39
40 261
        if (isset(self::$sridCache['length'][$srid])) {
41 252
            return Length::makeUnit((float) $measurement, $srid);
42
        }
43
44 197
        if (isset(self::$sridCache['scale'][$srid])) {
45 188
            return Scale::makeUnit((float) $measurement, $srid);
46
        }
47
48 18
        if (isset(self::$sridCache['time'][$srid])) {
49 9
            return Time::makeUnit((float) $measurement, $srid);
50
        }
51
52 9
        if (isset(self::$sridCache['rate'][$srid])) {
53
            return Rate::makeUnit((float) $measurement, $srid);
54
        }
55
56 9
        throw new UnknownUnitOfMeasureException($srid);
57
    }
58
59
    /**
60
     * @return array<string, string>
61
     */
62 9
    public static function getSupportedSRIDs(): array
63
    {
64 9
        return array_merge(Angle::getSupportedSRIDs(), Length::getSupportedSRIDs(), Scale::getSupportedSRIDs(), Time::getSupportedSRIDs(), Rate::getSupportedSRIDs());
65
    }
66
}
67