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

UnitOfMeasureFactory::makeUnit()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7.0084

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 17
nc 12
nop 2
dl 0
loc 31
ccs 17
cts 18
cp 0.9444
crap 7.0084
rs 8.8333
c 1
b 0
f 0
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