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

Scale::divide()   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 1
crap 1
1
<?php
2
/**
3
 * PHPCoord.
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace PHPCoord\UnitOfMeasure\Scale;
10
11
use PHPCoord\Exception\UnknownUnitOfMeasureException;
12
use PHPCoord\UnitOfMeasure\UnitOfMeasure;
13
14
abstract class Scale implements UnitOfMeasure
15
{
16
    /**
17
     * coefficient
18
     * Used when parameters are coefficients.  They inherently take the units which depend upon the term to which the
19
     * coefficient applies.
20
     */
21
    public const EPSG_COEFFICIENT = 'urn:ogc:def:uom:EPSG::9203';
22
23
    /**
24
     * parts per billion
25
     * Billion is internationally ambiguous, in different languages being 1E+9 and 1E+12. One billion taken here to be
26
     * 1E+9.
27
     */
28
    public const EPSG_PARTS_PER_BILLION = 'urn:ogc:def:uom:EPSG::1028';
29
30
    /**
31
     * parts per million.
32
     */
33
    public const EPSG_PARTS_PER_MILLION = 'urn:ogc:def:uom:EPSG::9202';
34
35
    /**
36
     * unity
37
     * EPSG standard unit for scale. SI coherent derived unit (standard unit) for dimensionless quantity, expressed by
38
     * the number one but this is not explicitly shown.
39
     */
40
    public const EPSG_UNITY = 'urn:ogc:def:uom:EPSG::9201';
41
42
    protected static array $sridData = [
43
        'urn:ogc:def:uom:EPSG::1028' => [
44
            'name' => 'parts per billion',
45
        ],
46
        'urn:ogc:def:uom:EPSG::9201' => [
47
            'name' => 'unity',
48
        ],
49
        'urn:ogc:def:uom:EPSG::9202' => [
50
            'name' => 'parts per million',
51
        ],
52
        'urn:ogc:def:uom:EPSG::9203' => [
53
            'name' => 'coefficient',
54
        ],
55
    ];
56
57
    private static array $supportedCache = [];
58
59
    abstract public function asUnity(): Unity;
60
61 36
    public function add(self $unit): self
62
    {
63 36
        $resultAsUnity = new Unity($this->asUnity()->getValue() + $unit->asUnity()->getValue());
64 36
        $conversionRatio = (new static(1))->asUnity()->getValue();
0 ignored issues
show
Unused Code introduced by
The call to PHPCoord\UnitOfMeasure\Scale\Scale::__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

64
        $conversionRatio = (/** @scrutinizer ignore-call */ new static(1))->asUnity()->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...
65
66 36
        return new static($resultAsUnity->getValue() / $conversionRatio);
67
    }
68
69 9
    public function subtract(self $unit): self
70
    {
71 9
        $resultAsUnity = new Unity($this->asUnity()->getValue() - $unit->asUnity()->getValue());
72 9
        $conversionRatio = (new static(1))->asUnity()->getValue();
0 ignored issues
show
Unused Code introduced by
The call to PHPCoord\UnitOfMeasure\Scale\Scale::__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

72
        $conversionRatio = (/** @scrutinizer ignore-call */ new static(1))->asUnity()->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...
73
74 9
        return new static($resultAsUnity->getValue() / $conversionRatio);
75
    }
76
77 36
    public function multiply(float $multiplicand): self
78
    {
79 36
        return new static($this->getValue() * $multiplicand);
0 ignored issues
show
Unused Code introduced by
The call to PHPCoord\UnitOfMeasure\Scale\Scale::__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

79
        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...
80
    }
81
82 27
    public function divide(float $divisor): self
83
    {
84 27
        return new static($this->getValue() / $divisor);
0 ignored issues
show
Unused Code introduced by
The call to PHPCoord\UnitOfMeasure\Scale\Scale::__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

84
        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...
85
    }
86
87 260
    public static function makeUnit(float $measurement, string $srid): self
88
    {
89
        return match ($srid) {
90 260
            self::EPSG_COEFFICIENT => new Coefficient($measurement),
91 242
            self::EPSG_PARTS_PER_BILLION => new PartsPerBillion($measurement),
92 224
            self::EPSG_PARTS_PER_MILLION => new PartsPerMillion($measurement),
93 143
            self::EPSG_UNITY => new Unity($measurement),
94 260
            default => throw new UnknownUnitOfMeasureException($srid),
95
        };
96
    }
97
98 27
    public static function getSupportedSRIDs(): array
99
    {
100 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...
101
            foreach (static::$sridData as $srid => $data) {
102
                self::$supportedCache[$srid] = $data['name'];
103
            }
104
        }
105
106 27
        return self::$supportedCache;
107
    }
108
109 9
    public function __toString(): string
110
    {
111 9
        return (string) $this->getValue();
112
    }
113
}
114