Passed
Push — 4.x ( c37faa...bd875a )
by Doug
12:53
created

CoordinateSystem::fromEPSGCode()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 34
rs 9.0111
cc 6
nc 9
nop 1
1
<?php
2
/**
3
 * PHPCoord.
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace PHPCoord\CoordinateSystem;
10
11
use PHPCoord\EPSG\Repository;
12
use PHPCoord\Exception\UnknownCoordinateSystemException;
13
14
abstract class CoordinateSystem implements CoordinateSystemIds
15
{
16
    public const CS_TYPE_CARTESIAN = 'Cartesian';
17
18
    public const CS_TYPE_ELLIPSOIDAL = 'ellipsoidal';
19
20
    public const CS_TYPE_ORDINAL = 'ordinal';
21
22
    public const CS_TYPE_SPHERICAL = 'spherical';
23
24
    public const CS_TYPE_VERTICAL = 'vertical';
25
26
    /** @var int */
27
    protected $epsgCode;
28
29
    /** @var Axis[] */
30
    protected $axes;
31
32
    /** @var Repository */
33
    private static $repository;
34
35
    public static function fromEPSGCode(int $epsgCode): self
36
    {
37
        $repository = static::$repository ?? new Repository();
0 ignored issues
show
Bug introduced by
Since $repository is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $repository to at least protected.
Loading history...
38
        $allData = $repository->getCoordinateSystems(true);
0 ignored issues
show
Unused Code introduced by
The call to PHPCoord\EPSG\Repository::getCoordinateSystems() has too many arguments starting with true. ( Ignorable by Annotation )

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

38
        /** @scrutinizer ignore-call */ 
39
        $allData = $repository->getCoordinateSystems(true);

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...
39
40
        if (!isset($allData[$epsgCode])) {
41
            throw new UnknownCoordinateSystemException($epsgCode);
42
        }
43
44
        $data = $allData[$epsgCode];
45
46
        $axes = [];
47
        foreach ($data['axes'] as $axisData) {
48
            $axes[$axisData['coord_axis_order']] = new Axis(
49
                $axisData['coord_axis_orientation'],
50
                $axisData['coord_axis_abbreviation'],
51
                $axisData['coord_axis_name'],
52
                $axisData['uom_code'],
53
            );
54
        }
55
56
        if ($data['coord_sys_type'] === self::CS_TYPE_CARTESIAN) {
57
            return new Cartesian($epsgCode, $axes);
58
        }
59
60
        if ($data['coord_sys_type'] === self::CS_TYPE_ELLIPSOIDAL) {
61
            return new Ellipsoidal($epsgCode, $axes);
62
        }
63
64
        if ($data['coord_sys_type'] === self::CS_TYPE_VERTICAL) {
65
            return new Vertical($epsgCode, $axes);
66
        }
67
68
        throw new UnknownCoordinateSystemException($epsgCode); // @codeCoverageIgnore
69
    }
70
71
    public function __construct(
72
        int $epsgCode,
73
        array $axes
74
    ) {
75
        $this->epsgCode = $epsgCode;
76
        $this->axes = $axes;
77
    }
78
79
    public function getEpsgCode(): int
80
    {
81
        return $this->epsgCode;
82
    }
83
84
    /**
85
     * @return Axis[]
86
     */
87
    public function getAxes(): array
88
    {
89
        return $this->axes;
90
    }
91
}
92