Completed
Pull Request — master (#6)
by
unknown
09:39 queued 04:59
created

OSRef   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 183
Duplicated Lines 6.01 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 17
Bugs 2 Features 3
Metric Value
wmc 16
c 17
b 2
f 3
lcom 1
cbo 2
dl 11
loc 183
ccs 47
cts 47
cp 1
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getReferenceEllipsoid() 0 4 1
A getScaleFactor() 0 4 1
A getOriginNorthing() 0 4 1
A getOriginEasting() 0 4 1
A getOriginLatitude() 0 4 1
A getOriginLongitude() 0 4 1
A __construct() 0 4 1
A fromSixFigureReference() 0 16 1
B toGridReference() 0 24 1
A toTwoFigureReference() 0 4 1
A toFourFigureReference() 0 4 1
A toSixFigureReference() 0 4 1
A toEightFigureReference() 0 4 1
A toTenFigureReference() 0 4 1
A toLatLng() 11 11 1
A __toString() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * PHPCoord
4
 * @package PHPCoord
5
 * @author Jonathan Stott
6
 * @author Doug Wright
7
 */
8
namespace PHPCoord;
9
10
/**
11
 * Ordnance Survey grid reference
12
 * References are accurate to 1m
13
 * @author Jonathan Stott
14
 * @author Doug Wright
15
 * @package PHPCoord
16
 */
17
class OSRef extends TransverseMercator
18
{
19
20
    const GRID_LETTERS = "VWXYZQRSTULMNOPFGHJKABCDE";
21
22 2
    public function getReferenceEllipsoid()
23
    {
24 2
        return RefEll::airy1830();
25
    }
26
27 4
    public function getScaleFactor()
28
    {
29 4
        return 0.9996012717;
30
    }
31
32 4
    public function getOriginNorthing()
33
    {
34 4
        return -100000;
35
    }
36
37 4
    public function getOriginEasting()
38
    {
39 4
        return 400000;
40
    }
41
42 4
    public function getOriginLatitude()
43
    {
44 4
        return 49;
45
    }
46
47 4
    public function getOriginLongitude()
48
    {
49 4
        return -2;
50
    }
51
52
    /**
53
     * Create a new object representing a OSGB reference.
54
     *
55
     * @param int $x
56
     * @param int $y
57
     * @param int $z
58
     */
59 10
    public function __construct($x, $y, $z = 0)
60
    {
61 10
        parent::__construct($x, $y, $z, RefEll::airy1830());
62 10
    }
63
64
    /**
65
     * Take a string formatted as a six-figure OS grid reference (e.g.
66
     * "TG514131") and return a reference to an OSRef object that represents
67
     * that grid reference.
68
     *
69
     * @param string $ref
70
     * @return OSRef
71
     */
72 2
    public static function fromSixFigureReference($ref)
73
    {
74
75
        //first (major) letter is the 500km grid sq, origin at -1000000, -500000
76 2
        $majorEasting = strpos(self::GRID_LETTERS, $ref[0]) % 5  * 500000 - 1000000;
77 2
        $majorNorthing = (floor(strpos(self::GRID_LETTERS, $ref[0]) / 5)) * 500000 - 500000;
78
79
        //second (minor) letter is 100km grid sq, origin at 0,0 of this square
80 2
        $minorEasting = strpos(self::GRID_LETTERS, $ref[1]) % 5  * 100000;
81 2
        $minorNorthing = (floor(strpos(self::GRID_LETTERS, $ref[1]) / 5)) * 100000;
82
83 2
        $easting = $majorEasting + $minorEasting + (substr($ref, 2, 3) * 100);
84 2
        $northing = $majorNorthing + $minorNorthing + (substr($ref, 5, 3) * 100);
85
86 2
        return new OSRef($easting, $northing);
87
    }
88
89
    /**
90
     * Convert this grid reference into a grid reference string of a 
91
     * given length (2, 4, 6, 8 or  10) including the two-character 
92
     * designation for the 100km square. e.g. TG514131.
93
     * @return string
94
     */
95 3
    private function toGridReference($length)
96
    {
97
98 3
        $halfLangth = $length / 2;
99 3
100
        $easting = str_pad($this->x, 6, 0, STR_PAD_LEFT);
101
        $northing = str_pad($this->y, 6, 0, STR_PAD_LEFT);
102 3
103 3
104 3
        $adjustedX = $this->x + 1000000;
105 3
        $adjustedY = $this->y + 500000;
106 3
        $majorSquaresEast = floor($adjustedX / 500000);
107 3
        $majorSquaresNorth = floor($adjustedY / 500000);
108
        $majorLetterIndex = (int)(5 * $majorSquaresNorth + $majorSquaresEast);
109
        $majorLetter = substr(self::GRID_LETTERS, $majorLetterIndex, 1);
110 3
111 3
        //second (minor) letter is 100km grid sq, origin at 0,0 of this square
112 3
        $minorSquaresEast = $easting[0] % 5;
113 3
        $minorSquaresNorth = $northing[0] % 5;
114
        $minorLetterIndex = (int)(5 * $minorSquaresNorth + $minorSquaresEast);
115 3
        $minorLetter = substr(self::GRID_LETTERS, $minorLetterIndex, 1);
116
117
        return $majorLetter . $minorLetter . substr($easting, 1, $halfLangth) . substr($northing, 1, $halfLangth);
118
    }
119
120
    /**
121
     * Convert this grid reference into a string using a standard two-figure
122 2
     * grid reference including the two-character designation for the 100km
123
     * square. e.g. TG51 (10km square).
124 2
     * @return string
125 2
     */
126 2
    public function toTwoFigureReference()
127 2
    {
128 2
        return $this->toGridReference(2);
129 2
    }
130
131 2
    /**
132
     * Convert this grid reference into a string using a standard four-figure
133
     * grid reference including the two-character designation for the 100km
134
     * square. e.g. TG5113 (1km square).
135
     * @return string
136
     */
137
    public function toFourFigureReference()
138 5
    {
139
        return $this->toGridReference(4);
140 5
    }
141
142
    /**
143
     * Convert this grid reference into a string using a standard six-figure
144
     * grid reference including the two-character designation for the 100km
145
     * square. e.g. TG514131 (100m square).
146
     * @return string
147
     */
148
    public function toSixFigureReference()
149
    {
150
        return $this->toGridReference(6);
151
    }
152
153
    /**
154
     * Convert this grid reference into a string using a standard eight-figure
155
     * grid reference including the two-character designation for the 100km
156
     * square. e.g. TG51431312 (10m square).
157
     * @return string
158
     */
159
    public function toEightFigureReference()
160
    {
161
        return $this->toGridReference(8);
162
    }
163
164
    /**
165
     * Convert this grid reference into a string using a standard ten-figure
166
     * grid reference including the two-character designation for the 100km
167
     * square. e.g. TG5143113121 (1m square).
168
     * @return string
169
     */
170
    public function toTenFigureReference()
171
    {
172
        return $this->toGridReference(10);
173
    }
174
175
    /**
176
     * Convert this grid reference into a latitude and longitude
177
     * @return LatLng
178
     */
179 View Code Duplication
    public function toLatLng()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
180
    {
181
        $N = $this->y;
182
        $E = $this->x;
183
        $N0 = $this->getOriginNorthing();
184
        $E0 = $this->getOriginEasting();
185
        $phi0 = $this->getOriginLatitude();
186
        $lambda0 = $this->getOriginLongitude();
187
188
        return $this->convertToLatitudeLongitude($N, $E, $N0, $E0, $phi0, $lambda0);
189
    }
190
191
    /**
192
     * String version of coordinate.
193
     * @return string
194
     */
195
    public function __toString()
196
    {
197
        return "({$this->x}, {$this->y})";
198
    }
199
}
200