| Conditions | 5 |
| Paths | 4 |
| Total Lines | 76 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 81 | public function getWeatherHistoryFromAPI($lat, $lon) |
||
| 82 | { |
||
| 83 | $apiKey; |
||
| 84 | $keyChain = $this->keyChain; |
||
| 85 | if (array_key_exists("keys", $keyChain)) { |
||
| 86 | $apiKey = $keyChain["keys"]["openweathermap"]; |
||
| 87 | } |
||
| 88 | |||
| 89 | // Create all cURL resources |
||
| 90 | $dayOne = curl_init(); |
||
| 91 | $dayTwo = curl_init(); |
||
| 92 | $dayThree = curl_init(); |
||
| 93 | $dayFour = curl_init(); |
||
| 94 | $dayFive = curl_init(); |
||
| 95 | |||
| 96 | // Set dates |
||
| 97 | $dayMinusOne = mktime(0, 0, 0, date("m"), date("d"), date("Y")); |
||
| 98 | $dayMinusTwo = mktime(0, 0, 0, date("m"), date("d")-1, date("Y")); |
||
| 99 | $dayMinusThree = mktime(0, 0, 0, date("m"), date("d")-2, date("Y")); |
||
| 100 | $dayMinusFour = mktime(0, 0, 0, date("m"), date("d")-3, date("Y")); |
||
| 101 | $dayMinusFive = mktime(0, 0, 0, date("m"), date("d")-4, date("Y")); |
||
| 102 | |||
| 103 | // set URL and other appropriate options |
||
| 104 | curl_setopt($dayOne, CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=$lat&lon=$lon&dt=$dayMinusOne&units=metric&appid=$apiKey"); |
||
| 105 | curl_setopt($dayOne, CURLOPT_RETURNTRANSFER, true); |
||
| 106 | curl_setopt($dayTwo, CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=$lat&lon=$lon&dt=$dayMinusTwo&units=metric&appid=$apiKey"); |
||
| 107 | curl_setopt($dayTwo, CURLOPT_RETURNTRANSFER, true); |
||
| 108 | curl_setopt($dayThree, CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=$lat&lon=$lon&dt=$dayMinusThree&units=metric&appid=$apiKey"); |
||
| 109 | curl_setopt($dayThree, CURLOPT_RETURNTRANSFER, true); |
||
| 110 | curl_setopt($dayFour, CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=$lat&lon=$lon&dt=$dayMinusFour&units=metric&appid=$apiKey"); |
||
| 111 | curl_setopt($dayFour, CURLOPT_RETURNTRANSFER, true); |
||
| 112 | curl_setopt($dayFive, CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=$lat&lon=$lon&dt=$dayMinusFive&units=metric&appid=$apiKey"); |
||
| 113 | curl_setopt($dayFive, CURLOPT_RETURNTRANSFER, true); |
||
| 114 | |||
| 115 | // Create the multiple cURL handle |
||
| 116 | $mh = curl_multi_init(); |
||
| 117 | |||
| 118 | // Add the five handles |
||
| 119 | curl_multi_add_handle($mh, $dayOne); |
||
| 120 | curl_multi_add_handle($mh, $dayTwo); |
||
| 121 | curl_multi_add_handle($mh, $dayThree); |
||
| 122 | curl_multi_add_handle($mh, $dayFour); |
||
| 123 | curl_multi_add_handle($mh, $dayFive); |
||
| 124 | |||
| 125 | do { |
||
| 126 | $status = curl_multi_exec($mh, $active); |
||
| 127 | if ($active) { |
||
| 128 | curl_multi_select($mh); |
||
| 129 | } |
||
| 130 | } while ($active && $status == CURLM_OK); |
||
| 131 | |||
| 132 | curl_multi_remove_handle($mh, $dayOne); |
||
| 133 | curl_multi_remove_handle($mh, $dayTwo); |
||
| 134 | curl_multi_remove_handle($mh, $dayThree); |
||
| 135 | curl_multi_remove_handle($mh, $dayFour); |
||
| 136 | curl_multi_remove_handle($mh, $dayFive); |
||
| 137 | curl_multi_close($mh); |
||
| 138 | |||
| 139 | $data; |
||
| 140 | $response1 = curl_multi_getcontent($dayOne); |
||
| 141 | $response2 = curl_multi_getcontent($dayTwo); |
||
| 142 | $response3 = curl_multi_getcontent($dayThree); |
||
| 143 | $response4 = curl_multi_getcontent($dayFour); |
||
| 144 | $response5 = curl_multi_getcontent($dayFive); |
||
| 145 | $data = [ |
||
| 146 | "cod" => 200, |
||
| 147 | "past_days" => [ |
||
| 148 | "past_01" => $response1, |
||
| 149 | "past_02" => $response2, |
||
| 150 | "past_03" => $response3, |
||
| 151 | "past_04" => $response4, |
||
| 152 | "past_05" => $response5 |
||
| 153 | ], |
||
| 154 | ]; |
||
| 155 | |||
| 156 | return $data; |
||
| 157 | } |
||
| 262 |