Conditions | 7 |
Paths | 14 |
Total Lines | 76 |
Code Lines | 50 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
24 | protected function query($query, $params = []) |
||
25 | { |
||
26 | $url = self::API_URL; |
||
27 | |||
28 | $defaultParams = [ |
||
29 | 'locate' => $query, |
||
30 | 'json' => 1 |
||
31 | ]; |
||
32 | $params = array_merge($defaultParams, $params); |
||
33 | |||
34 | $url .= '?' . http_build_query($params); |
||
35 | |||
36 | $headers = [ |
||
37 | "accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", |
||
38 | "accept-encoding: gzip, deflate, br", |
||
39 | "accept-language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7,nb;q=0.6", |
||
40 | "cache-control: max-age=0", |
||
41 | "user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36", |
||
42 | ]; |
||
43 | $opts = array('http' => |
||
44 | array( |
||
45 | 'method' => 'POST', |
||
46 | 'header' => implode("\r\n", $headers), |
||
47 | )); |
||
48 | |||
49 | $context = stream_context_create($opts, $opts); |
||
50 | $result = file_get_contents($url, false, $context); |
||
51 | if (!$result) { |
||
52 | throw new Exception("The api returned no result"); |
||
53 | } |
||
54 | |||
55 | $data = json_decode($result, true); |
||
56 | |||
57 | if (!$data) { |
||
58 | throw new Exception("Failed to decode api results"); |
||
59 | } |
||
60 | |||
61 | $location = []; |
||
62 | $countryCode = $countryName = null; |
||
63 | $lat = $lon = null; |
||
64 | |||
65 | // normalize |
||
66 | if (isset($data['standard'])) { |
||
67 | $location = [ |
||
68 | 'streetName' => $data['standard']['addresst'] ?? null, |
||
69 | 'streetNumber' => $data['standard']['stno'] ?? null, |
||
70 | 'postalCode' => $data['standard']['postal'] ?? null, |
||
71 | 'locality' => $data['standard']['city'] ?? null, |
||
72 | ]; |
||
73 | $countryCode = $data['standard']['prov'] ?? null; |
||
74 | $countryName = $data['standard']['countryname'] ?? null; |
||
75 | } elseif (isset($data['staddress'])) { |
||
76 | $location = [ |
||
77 | 'streetName' => $data['staddress'] ?? null, |
||
78 | 'streetNumber' => $data['stnumber'] ?? null, |
||
79 | 'postalCode' => $data['postal'] ?? null, |
||
80 | 'locality' => $data['city'] ?? null, |
||
81 | ]; |
||
82 | $countryCode = $data['prov'] ?? null; |
||
83 | $countryName = $data['country'] ?? null; |
||
84 | } |
||
85 | |||
86 | // Make sure we have a string |
||
87 | if (empty($location['postalCode'])) { |
||
88 | $location['postalCode'] = ''; |
||
89 | } |
||
90 | |||
91 | if (!empty($data['latt'])) { |
||
92 | $lat = $data['latt']; |
||
93 | $lon = $data['longt']; |
||
94 | } |
||
95 | |||
96 | $country = new Country($countryCode, $countryName); |
||
97 | $coordinates = new Coordinates($lat, $lon); |
||
98 | |||
99 | return new Address($location, $country, $coordinates); |
||
100 | } |
||
134 |