API::request()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Suilven\UKPostCodes;
4
5
//Based on code by Ryan Hart 2016 https://github.com/ryryan/Postcodes-IO-PHP/blob/master/Postcodes-IO-PHP.php
6
7
use Suilven\UKPostCodes\Models\PostCode;
8
9
class API
10
{
11
    /**
12
     * @param string $postcode
13
     * @return false|PostCode
14
     */
15 View Code Duplication
    public function lookup($postcode)
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...
16
    {
17
        $jsonurl = "https://api.postcodes.io/postcodes/".$postcode;
18
        $json = $this->request($jsonurl);
19
        $decoded = json_decode($json, true);
20
        if ($decoded['status'] == 200) {
21
            return new PostCode($decoded['result']);
22
        } else {
23
            return false;
24
        }
25
        return false;
0 ignored issues
show
Unused Code introduced by
return false; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
26
    }
27
28
29 View Code Duplication
    public function bulkLookup($postcodes)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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...
30
    {
31
        $data_string = json_encode(array('postcodes' => $postcodes));
32
        $ch = curl_init('https://api.postcodes.io/postcodes');
33
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
34
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
35
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
36
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
37
                'Content-Type: application/json',
38
                'Content-Length: ' . strlen($data_string)));
39
40
        $result = curl_exec($ch);
41
        curl_close($ch);
42
        $decoded = json_decode($result);
43
        if ($decoded->status == 200) {
44
            return $decoded->result;
45
        } else {
46
            return false;
47
        }
48
        return false;
0 ignored issues
show
Unused Code introduced by
return false; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
49
    }
50 View Code Duplication
    public function nearestPostcodesFromLongLat($longitude, $latitude)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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...
51
    {
52
        $jsonurl = "https://api.postcodes.io/postcodes?lon=".$longitude."&lat=".$latitude;
53
        $json = $this->request($jsonurl);
54
55
        $decoded = json_decode($json);
56
        if ($decoded->status == 200) {
57
            return $decoded->result;
58
        } else {
59
            return false;
60
        }
61
        return false;
0 ignored issues
show
Unused Code introduced by
return false; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
62
    }
63 View Code Duplication
    public function bulkReverseGeocoding($geolocations)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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...
64
    {
65
        $data_string = json_encode(array('geolocations' => $geolocations));
66
67
        $ch = curl_init('https://api.postcodes.io/postcodes');
68
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
69
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
70
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
71
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
72
                'Content-Type: application/json',
73
                'Content-Length: ' . strlen($data_string)));
74
75
        $result = curl_exec($ch);
76
        curl_close($ch);
77
        $decoded = json_decode($result);
78
        if ($decoded->status == 200) {
79
            return $decoded->result;
80
        } else {
81
            return false;
82
        }
83
        return false;
0 ignored issues
show
Unused Code introduced by
return false; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
84
    }
85
86
87
    /**
88
     * @return bool|PostCode a random PostCode
89
     */
90 View Code Duplication
    public function random()
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...
91
    {
92
        $jsonurl = "https://api.postcodes.io/random/postcodes/";
93
        $json = $this->request($jsonurl);
94
95
        $decoded = json_decode($json, true);
96
        if ($decoded['status'] == 200) {
97
            return new PostCode($decoded['result']);
98
        } else {
99
            return false;
100
        }
101
        return false;
0 ignored issues
show
Unused Code introduced by
return false; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
102
    }
103
104
105
    public function validate($postcode)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
106
    {
107
        $jsonurl = "https://api.postcodes.io/postcodes/".$postcode."/validate";
108
        $json = $this->request($jsonurl);
109
110
        $decoded = json_decode($json);
111
        if ($decoded->status == 200) {
112
            if ($decoded->result == 1) {
0 ignored issues
show
Coding Style introduced by
The if-else statement can be simplified to return $decoded->result == 1;.
Loading history...
113
                return true;
114
            } else {
115
                return false;
116
            }
117
        } else {
118
            return false;
119
        }
120
        return false;
0 ignored issues
show
Unused Code introduced by
return false; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
121
    }
122
123
124 View Code Duplication
    public function nearest($postcode)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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
        $jsonurl = "https://api.postcodes.io/postcodes/".$postcode."/nearest";
127
        $json = $this->request($jsonurl);
128
129
        $decoded = json_decode($json);
130
        if ($decoded->status == 200) {
131
            return $decoded->result;
132
        } else {
133
            return false;
134
        }
135
        return false;
0 ignored issues
show
Unused Code introduced by
return false; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
136
    }
137
138
139 View Code Duplication
    public function partial($postcode)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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...
140
    {
141
        $jsonurl = "https://api.postcodes.io/postcodes/".$postcode."/autocomplete";
142
        $json = $this->request($jsonurl);
143
144
        $decoded = json_decode($json);
145
        if ($decoded->status == 200) {
146
            return $decoded->result;
147
        } else {
148
            return false;
149
        }
150
        return false;
0 ignored issues
show
Unused Code introduced by
return false; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
151
    }
152
153
154
    public function query($postcode)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
155
    {
156
        $jsonurl = "https://api.postcodes.io/postcodes?q=".$postcode;
157
        $json = $this->request($jsonurl);
158
159
        $decoded = json_decode($json);
160
        if ($decoded->status == 200) {
161
            return $decoded->result;
162
        } else {
163
            return false;
164
        }
165
        return false;
0 ignored issues
show
Unused Code introduced by
return false; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
166
    }
167
168
169
    public function lookupTerminated($postcode)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
170
    {
171
        $jsonurl = "https://api.postcodes.io/terminated_postcodes/".$postcode;
172
        $json = $this->request($jsonurl);
173
174
        $decoded = json_decode($json);
175
        if ($decoded->status == 200) {
176
            return $decoded->result;
177
        } else {
178
            return false;
179
        }
180
        return false;
0 ignored issues
show
Unused Code introduced by
return false; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
181
    }
182
183
184
    public function lookupOutwardCode($code)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
185
    {
186
        $jsonurl = "https://api.postcodes.io/outcodes/".$code;
187
        $json = $this->request($jsonurl);
188
189
        $decoded = json_decode($json);
190
        if ($decoded->status == 200) {
191
            return $decoded->result;
192
        } else {
193
            return false;
194
        }
195
        return false;
0 ignored issues
show
Unused Code introduced by
return false; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
196
    }
197
198
199 View Code Duplication
    public function nearestOutwardCode($code)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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...
200
    {
201
        $jsonurl = "https://api.postcodes.io/outcodes/".$code."/nearest";
202
        $json = $this->request($jsonurl);
203
204
        $decoded = json_decode($json);
205
        if ($decoded->status == 200) {
206
            return $decoded->result;
207
        } else {
208
            return false;
209
        }
210
        return false;
0 ignored issues
show
Unused Code introduced by
return false; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
211
    }
212
213
214 View Code Duplication
    public function nearestOutwardCodeFromLongLat($longitude, $latitude)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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...
215
    {
216
        $jsonurl = "https://api.postcodes.io/outcodes?lon=".$longitude."&lat=".$latitude;
217
        $json = $this->request($jsonurl);
218
219
        $decoded = json_decode($json);
220
        if ($decoded->status == 200) {
221
            return $decoded->result;
222
        } else {
223
            return false;
224
        }
225
        return false;
0 ignored issues
show
Unused Code introduced by
return false; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
226
    }
227
228
229
    public function distance($postcode1, $postcode2, $unit)
230
    {
231
        //adapted from http://www.geodatasource.com/developers/php
232
        /*
233
            Units:
234
            M = Miles
235
            N = Nautical Miles
236
            K = Kilometers
237
        */
238
        $postcode1 = $this->lookup($postcode1);
239
        $postcode2 = $this->lookup($postcode2);
240
241
        if ($postcode1 == null || $postcode2 == null) {
242
            return false;
243
        }
244
245
        $theta = $postcode1->longitude - $postcode2->longitude;
246
        $dist = sin(deg2rad($postcode1->latitude)) * sin(deg2rad($postcode2->latitude)) +
247
            cos(deg2rad($postcode1->latitude)) * cos(deg2rad($postcode2->latitude)) * cos(deg2rad($theta));
248
        $dist = acos($dist);
249
        $dist = rad2deg($dist);
250
        $miles = $dist * 60 * 1.1515;
251
        $unit = strtoupper($unit);
252
253
        if ($unit == "K") {
254
            return ($miles * 1.609344);
255
        } elseif ($unit == "N") {
256
            return ($miles * 0.8684);
257
        } else {
258
            return $miles;
259
        }
260
    }
261
262
    /**
263
     * Execute a request
264
     *
265
     * @param string $jsonurl
266
     * @return bool|string
267
     */
268
    public function request($jsonurl)
269
    {
270
        $ch = curl_init();
271
        curl_setopt($ch, CURLOPT_URL, str_replace(' ', '%20', $jsonurl));
272
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
273
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
274
          'Content-Type: application/json',
275
        ]);
276
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
277
        $response = curl_exec($ch);
278
        curl_close($ch);
279
        return $response;
280
    }
281
}
282