| Conditions | 6 |
| Paths | 44 |
| Total Lines | 53 |
| Code Lines | 33 |
| 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 |
||
| 95 | private function getHistoricalResponse($lat, $long) : array |
||
| 96 | { |
||
| 97 | //openweathermap documentation |
||
| 98 | // https://openweathermap.org/api/one-call-api#history |
||
| 99 | $key = $this->accessKey; |
||
| 100 | $url = 'http://api.openweathermap.org/data/2.5/onecall/timemachine'; |
||
| 101 | $options = [ |
||
| 102 | CURLOPT_RETURNTRANSFER => true, |
||
| 103 | ]; |
||
| 104 | $currentTime = time(); |
||
| 105 | $day1 = ($currentTime - ((24*60*60)*1)); |
||
| 106 | $day2 = ($currentTime - ((24*60*60)*2)); |
||
| 107 | $day3 = ($currentTime - ((24*60*60)*3)); |
||
| 108 | $day4 = ($currentTime - ((24*60*60)*4)); |
||
| 109 | $day5 = ($currentTime - ((24*60*60)*5)); |
||
| 110 | $timeList = [$currentTime, $day1, $day2, $day3, $day4, $day5]; |
||
| 111 | |||
| 112 | |||
| 113 | // Add all curl handlers and remember them |
||
| 114 | // Initiate the multi curl handler |
||
| 115 | $mh = curl_multi_init(); |
||
| 116 | $chAll = []; |
||
| 117 | try { |
||
| 118 | foreach ($timeList as $time) { |
||
| 119 | $ch = curl_init($url . '?lat=' . $lat . '&lon=' . $long . '&dt=' . $time . '&appid=' . $key . '&units=metric'); |
||
| 120 | curl_setopt_array($ch, $options); |
||
| 121 | curl_multi_add_handle($mh, $ch); |
||
| 122 | $chAll[] = $ch; |
||
| 123 | } |
||
| 124 | |||
| 125 | // Execute all queries simultaneously, |
||
| 126 | // and continue when all are complete |
||
| 127 | $running = null; |
||
| 128 | do { |
||
| 129 | curl_multi_exec($mh, $running); |
||
| 130 | } while ($running); |
||
| 131 | |||
| 132 | // Close the handles |
||
| 133 | foreach ($chAll as $ch) { |
||
| 134 | curl_multi_remove_handle($mh, $ch); |
||
| 135 | } |
||
| 136 | curl_multi_close($mh); |
||
| 137 | |||
| 138 | // All of our requests are done, access the results |
||
| 139 | $response = []; |
||
| 140 | foreach ($chAll as $ch) { |
||
| 141 | $data = curl_multi_getcontent($ch); |
||
| 142 | $response[] = json_decode($data, true); |
||
| 143 | } |
||
| 144 | |||
| 145 | return $response; |
||
| 146 | } catch (\Throwable $th) { |
||
| 147 | return ["could not connect to openweathermap"]; |
||
| 148 | } |
||
| 196 |