Passed
Push — master ( 2b8bad...6fb66b )
by Doug
25:16
created

Time::multiply()   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\Time;
10
11
use PHPCoord\Exception\UnknownUnitOfMeasureException;
12
use PHPCoord\UnitOfMeasure\UnitOfMeasure;
13
14
abstract class Time implements UnitOfMeasure
15
{
16
    /**
17
     * year.
18
     */
19
    public const EPSG_YEAR = 'urn:ogc:def:uom:EPSG::1029';
20
21
    protected static array $sridData = [
22
        'urn:ogc:def:uom:EPSG::1029' => [
23
            'name' => 'year',
24
        ],
25
    ];
26
27
    protected static array $customSridData = [];
28
29
    private static array $supportedCache = [];
30
31
    abstract public function asYears(): Year;
32
33 9
    public function add(self $unit): self
34
    {
35 9
        $resultAsYears = new Year($this->asYears()->getValue() + $unit->asYears()->getValue());
36 9
        $conversionRatio = (new static(1))->asYears()->getValue();
0 ignored issues
show
Unused Code introduced by
The call to PHPCoord\UnitOfMeasure\Time\Time::__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

36
        $conversionRatio = (/** @scrutinizer ignore-call */ new static(1))->asYears()->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...
37
38 9
        return new static($resultAsYears->getValue() / $conversionRatio);
39
    }
40
41 45
    public function subtract(self $unit): self
42
    {
43 45
        $resultAsYears = new Year($this->asYears()->getValue() - $unit->asYears()->getValue());
44 45
        $conversionRatio = (new static(1))->asYears()->getValue();
0 ignored issues
show
Unused Code introduced by
The call to PHPCoord\UnitOfMeasure\Time\Time::__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

44
        $conversionRatio = (/** @scrutinizer ignore-call */ new static(1))->asYears()->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...
45
46 45
        return new static($resultAsYears->getValue() / $conversionRatio);
47
    }
48
49 9
    public function multiply(float $multiplicand): self
50
    {
51 9
        return new static($this->getValue() * $multiplicand);
0 ignored issues
show
Unused Code introduced by
The call to PHPCoord\UnitOfMeasure\Time\Time::__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

51
        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...
52
    }
53
54 9
    public function divide(float $divisor): self
55
    {
56 9
        return new static($this->getValue() / $divisor);
0 ignored issues
show
Unused Code introduced by
The call to PHPCoord\UnitOfMeasure\Time\Time::__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

56
        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...
57
    }
58
59 45
    public static function makeUnit(float $measurement, string $srid): self
60
    {
61 45
        if (isset(self::$customSridData[$srid])) {
62
            return new self::$customSridData[$srid]['fqcn']($measurement);
63
        }
64
65
        return match ($srid) {
66 45
            self::EPSG_YEAR => new Year($measurement),
67 45
            default => throw new UnknownUnitOfMeasureException($srid),
68
        };
69
    }
70
71 36
    public static function getSupportedSRIDs(): array
72
    {
73 36
        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...
74
            foreach (static::$sridData as $srid => $data) {
75
                self::$supportedCache[$srid] = $data['name'];
76
            }
77
        }
78
79 36
        return self::$supportedCache;
80
    }
81
82
    public static function registerCustomUnit(string $srid, string $name, string $implementingClassFQCN): void
83
    {
84
        self::$customSridData[$srid] = ['name' => $name, 'fqcn' => $implementingClassFQCN];
85
        self::getSupportedSRIDs(); // init cache if not already
86
        self::$supportedCache[$srid] = $name; // update cache
87
    }
88
89 18
    public function __toString(): string
90
    {
91 18
        return (string) $this->getValue();
92
    }
93
}
94