Completed
Pull Request — master (#6)
by
unknown
02:47
created

OSRef::toTenFigureString()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 30
Code Lines 20

Duplication

Lines 30
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 30
loc 30
ccs 0
cts 21
cp 0
rs 4.8197
cc 10
eloc 20
nc 10
nop 0
crap 110

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 string using a standard six-figure
91
     * grid reference including the two-character designation for the 100km
92
     * square. e.g. TG514131.
93
     * @return string
94
     */
95 3
    public function toSixFigureReference()
96
    {
97
98 3
        $easting = str_pad($this->x, 6, 0, STR_PAD_LEFT);
99 3
        $northing = str_pad($this->y, 6, 0, STR_PAD_LEFT);
100
101
102 3
        $adjustedX = $this->x + 1000000;
103 3
        $adjustedY = $this->y + 500000;
104 3
        $majorSquaresEast = floor($adjustedX / 500000);
105 3
        $majorSquaresNorth = floor($adjustedY / 500000);
106 3
        $majorLetterIndex = (int)(5 * $majorSquaresNorth + $majorSquaresEast);
107 3
        $majorLetter = substr(self::GRID_LETTERS, $majorLetterIndex, 1);
108
109
        //second (minor) letter is 100km grid sq, origin at 0,0 of this square
110 3
        $minorSquaresEast = $easting[0] % 5;
111 3
        $minorSquaresNorth = $northing[0] % 5;
112 3
        $minorLetterIndex = (int)(5 * $minorSquaresNorth + $minorSquaresEast);
113 3
        $minorLetter = substr(self::GRID_LETTERS, $minorLetterIndex, 1);
114
115 3
        return $majorLetter . $minorLetter . substr($easting, 1, 3) . substr($northing, 1, 3);
116
    }
117
    
118
    /**
119
     * Convert this grid reference into a string using a standard eight-figure
120
     * grid reference including the two-character designation for the 100km
121
     * square. e.g. TG51411311.
122
     * @return string
123
     */
124 View Code Duplication
    public function toEightFigureString()
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...
125
    {
126
127
        $easting = str_pad($this->easting, 6, 0, STR_PAD_LEFT);
0 ignored issues
show
Bug introduced by
The property easting does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
128
        $northing = str_pad($this->northing, 6, 0, STR_PAD_LEFT);
0 ignored issues
show
Bug introduced by
The property northing does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
129
130
        $hundredkmE = $easting[0];
131
        $hundredkmN = $northing[0];
132
133
        if ($hundredkmN < 5 && $hundredkmE < 5) {
134
            $firstLetter = 'S';
135
        } else if ($hundredkmN < 5 && $hundredkmE >= 5) {
136
            $firstLetter = 'T';
137
        } else if ($hundredkmN < 10 && $hundredkmE < 5) {
138
            $firstLetter = 'N';
139
        } else if ($hundredkmN < 10 && $hundredkmE >= 5) {
140
            $firstLetter = 'O';
141
        } else {
142
            $firstLetter = 'H';
143
        }
144
145
        $index = 65 + ((4 - ($hundredkmN % 5)) * 5) + ($hundredkmE % 5);
146
        if ($index >= 73) {
147
            //skip the letter I
148
            $index++;
149
        }
150
        $secondLetter = chr($index);
151
152
        return $firstLetter . $secondLetter . substr($easting, 1, 4) . substr($northing, 1, 4);
153
    }
154
155
    /**
156
     * Convert this grid reference into a string using a standard ten-figure
157
     * grid reference including the two-character designation for the 100km
158
     * square. e.g. TG5141213112.
159
     * @return string
160
     */
161 View Code Duplication
    public function toTenFigureString()
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...
162
    {
163
164
        $easting = str_pad($this->easting, 6, 0, STR_PAD_LEFT);
165
        $northing = str_pad($this->northing, 6, 0, STR_PAD_LEFT);
166
167
        $hundredkmE = $easting[0];
168
        $hundredkmN = $northing[0];
169
170
        if ($hundredkmN < 5 && $hundredkmE < 5) {
171
            $firstLetter = 'S';
172
        } else if ($hundredkmN < 5 && $hundredkmE >= 5) {
173
            $firstLetter = 'T';
174
        } else if ($hundredkmN < 10 && $hundredkmE < 5) {
175
            $firstLetter = 'N';
176
        } else if ($hundredkmN < 10 && $hundredkmE >= 5) {
177
            $firstLetter = 'O';
178
        } else {
179
            $firstLetter = 'H';
180
        }
181
182
        $index = 65 + ((4 - ($hundredkmN % 5)) * 5) + ($hundredkmE % 5);
183
        if ($index >= 73) {
184
            //skip the letter I
185
            $index++;
186
        }
187
        $secondLetter = chr($index);
188
189
        return $firstLetter . $secondLetter . substr($easting, 1, 5) . substr($northing, 1, 5);
190
    }
191
192
    /**
193
     * Convert this grid reference into a latitude and longitude
194
     * @return LatLng
195
     */
196 2 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...
197
    {
198 2
        $N = $this->y;
199 2
        $E = $this->x;
200 2
        $N0 = $this->getOriginNorthing();
201 2
        $E0 = $this->getOriginEasting();
202 2
        $phi0 = $this->getOriginLatitude();
203 2
        $lambda0 = $this->getOriginLongitude();
204
205 2
        return $this->convertToLatitudeLongitude($N, $E, $N0, $E0, $phi0, $lambda0);
206
    }
207
208
    /**
209
     * String version of coordinate.
210
     * @return string
211
     */
212 5
    public function __toString()
213
    {
214 5
        return "({$this->x}, {$this->y})";
215
    }
216
}
217