Passed
Push — master ( 6a435f...dad6c0 )
by Doug
13:08
created

UnitOfMeasureFactory::makeUnit()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 17
c 1
b 0
f 0
nc 12
nop 2
dl 0
loc 31
ccs 18
cts 18
cp 1
crap 7
rs 8.8333
1
<?php
2
/**
3
 * PHPCoord.
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace PHPCoord\UnitOfMeasure;
10
11
use function array_merge;
12
use PHPCoord\Exception\UnknownUnitOfMeasureException;
13
use PHPCoord\UnitOfMeasure\Angle\Angle;
14
use PHPCoord\UnitOfMeasure\Length\Length;
15
use PHPCoord\UnitOfMeasure\Scale\Scale;
16
use PHPCoord\UnitOfMeasure\Time\Time;
17
18
class UnitOfMeasureFactory
19
{
20
    private static array $sridCache = [];
21
22
    /**
23
     * @param float|string $measurement
24
     */
25 677
    public static function makeUnit($measurement, string $srid): UnitOfMeasure
26
    {
27 677
        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...
28 9
            self::$sridCache['angle'] = Angle::getSupportedSRIDs();
29 9
            self::$sridCache['length'] = Length::getSupportedSRIDs();
30 9
            self::$sridCache['scale'] = Scale::getSupportedSRIDs();
31 9
            self::$sridCache['time'] = Time::getSupportedSRIDs();
32 9
            self::$sridCache['rate'] = Rate::getSupportedSRIDs();
33
        }
34
35 677
        if (isset(self::$sridCache['angle'][$srid])) {
36 356
            return Angle::makeUnit($measurement, $srid);
37
        }
38
39 542
        if (isset(self::$sridCache['length'][$srid])) {
40 416
            return Length::makeUnit($measurement, $srid);
0 ignored issues
show
Bug introduced by
It seems like $measurement can also be of type string; however, parameter $measurement of PHPCoord\UnitOfMeasure\Length\Length::makeUnit() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

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

40
            return Length::makeUnit(/** @scrutinizer ignore-type */ $measurement, $srid);
Loading history...
41
        }
42
43 302
        if (isset(self::$sridCache['scale'][$srid])) {
44 221
            return Scale::makeUnit($measurement, $srid);
0 ignored issues
show
Bug introduced by
It seems like $measurement can also be of type string; however, parameter $measurement of PHPCoord\UnitOfMeasure\Scale\Scale::makeUnit() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

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

44
            return Scale::makeUnit(/** @scrutinizer ignore-type */ $measurement, $srid);
Loading history...
45
        }
46
47 90
        if (isset(self::$sridCache['time'][$srid])) {
48 18
            return Time::makeUnit($measurement, $srid);
0 ignored issues
show
Bug introduced by
It seems like $measurement can also be of type string; however, parameter $measurement of PHPCoord\UnitOfMeasure\Time\Time::makeUnit() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

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

48
            return Time::makeUnit(/** @scrutinizer ignore-type */ $measurement, $srid);
Loading history...
49
        }
50
51 72
        if (isset(self::$sridCache['rate'][$srid])) {
52 63
            return Rate::makeUnit($measurement, $srid);
0 ignored issues
show
Bug introduced by
It seems like $measurement can also be of type string; however, parameter $measurement of PHPCoord\UnitOfMeasure\Rate::makeUnit() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

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

52
            return Rate::makeUnit(/** @scrutinizer ignore-type */ $measurement, $srid);
Loading history...
53
        }
54
55 9
        throw new UnknownUnitOfMeasureException($srid);
56
    }
57
58 9
    public static function getSupportedSRIDs(): array
59
    {
60 9
        return array_merge(Angle::getSupportedSRIDs(), Length::getSupportedSRIDs(), Scale::getSupportedSRIDs(), Time::getSupportedSRIDs(), Rate::getSupportedSRIDs());
61
    }
62
}
63