| Conditions | 8 |
| Paths | 40 |
| Total Lines | 83 |
| Code Lines | 57 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 23 | public function getWeather($inputLat, $inputLong) |
||
| 24 | { |
||
| 25 | $this->latitude = $inputLat; |
||
| 26 | $this->longitude = $inputLong; |
||
| 27 | $owmApiKey = $this->owmApiKey; |
||
| 28 | |||
| 29 | // Fetching epoch times for last 5 days |
||
| 30 | $epochDays = array(); |
||
| 31 | $epochDays[0] = strtotime("-5 days"); |
||
| 32 | $epochDays[1] = strtotime("-4 days"); |
||
| 33 | $epochDays[2] = strtotime("-3 days"); |
||
| 34 | $epochDays[3] = strtotime("-2 days"); |
||
| 35 | $epochDays[4] = strtotime("-1 days"); |
||
| 36 | |||
| 37 | $url1 = "https://api.openweathermap.org/data/2.5/onecall?" |
||
| 38 | . "lat=" . $inputLat . "&lon=" . $inputLong |
||
| 39 | . "&exclude=minutely,hourly,alerts&" |
||
| 40 | . "appid=" . $owmApiKey |
||
| 41 | . "&units=metric&lang=se"; |
||
| 42 | |||
| 43 | //Creating URLs for historical data |
||
| 44 | $historicalUrls = array(); |
||
| 45 | for ($i = 0; $i < 5; $i++) { |
||
| 46 | $historicalUrls[$i] = "https://api.openweathermap.org/data/2.5/onecall/timemachine?" |
||
| 47 | . "lat=" . $inputLat . "&lon=" . $inputLong |
||
| 48 | . "&dt=" . $epochDays[$i] |
||
| 49 | . "&appid=" . $owmApiKey |
||
| 50 | . "&units=metric&lang=se"; |
||
| 51 | } |
||
| 52 | |||
| 53 | // Send request |
||
| 54 | $urlReq1 = array($url1); |
||
| 55 | $urls = array_merge($urlReq1, $historicalUrls); |
||
| 56 | $urlCount = count($urls); |
||
| 57 | |||
| 58 | $responseArr = array(); |
||
| 59 | $master = curl_multi_init(); |
||
| 60 | $running = 0; |
||
| 61 | |||
| 62 | for ($i = 0; $i < $urlCount; $i++) { |
||
| 63 | $url =$urls[$i]; |
||
| 64 | $responseArr[$i] = curl_init($url); |
||
| 65 | curl_setopt($responseArr[$i], CURLOPT_RETURNTRANSFER, true); |
||
| 66 | curl_multi_add_handle($master, $responseArr[$i]); |
||
| 67 | } |
||
| 68 | |||
| 69 | do { |
||
| 70 | curl_multi_exec($master, $running); |
||
| 71 | } while ($running > 0); |
||
| 72 | |||
| 73 | $results = array(); |
||
| 74 | for ($i = 0; $i < $urlCount; $i++) { |
||
| 75 | $results[$i] = curl_multi_getcontent($responseArr[$i]); |
||
| 76 | $results[$i] = json_decode($results[$i]); |
||
| 77 | } |
||
| 78 | |||
| 79 | if (isset($results[0])) { |
||
| 80 | // Handle response for request 1 |
||
| 81 | |||
| 82 | // Current weather |
||
| 83 | $this->todaysWeather["temperature"] = @$results[0]->current->temp; |
||
| 84 | $this->todaysWeather["feelsLike"] = @$results[0]->current->feels_like; |
||
| 85 | $this->todaysWeather["windSpeed"] = @$results[0]->current->wind_speed; |
||
| 86 | |||
| 87 | $this->todaysWeather["weatherMain"] = @$results[0]->current->weather[0]->main; |
||
| 88 | $this->todaysWeather["weatherDesc"] = @$results[0]->current->weather[0]->description; |
||
| 89 | |||
| 90 | // Weather next 7 days |
||
| 91 | $dailySize = @sizeof($results[0]->daily); |
||
| 92 | for ($i = 1; $i < @$dailySize; $i++) { |
||
| 93 | $this->dailyDate[$i] = $results[0]->daily[$i]->dt; |
||
| 94 | $this->dailyDesc[$i] = $results[0]->daily[$i]->weather[0]->description; |
||
| 95 | $this->dailyTemp[$i] = $results[0]->daily[$i]->temp->day; |
||
| 96 | $this->dailyWind[$i] = $results[0]->daily[$i]->wind_speed; |
||
| 97 | } |
||
| 98 | |||
| 99 | // Weather last 5 days |
||
| 100 | $resultSize = sizeof($results); |
||
| 101 | for ($i = 1; $i < $resultSize; $i++) { |
||
| 102 | $this->historicalDate[$i] = @$results[$i]->current->dt; |
||
| 103 | $this->historicalDesc[$i] = @$results[$i]->current->weather[0]->description; |
||
| 104 | $this->historicalTemp[$i] = @$results[$i]->current->temp; |
||
| 105 | $this->historicalWind[$i] = @$results[$i]->current->wind_speed; |
||
| 106 | } |
||
| 278 |