| Conditions | 8 |
| Paths | 32 |
| Total Lines | 56 |
| Code Lines | 32 |
| 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 |
||
| 71 | public function getWeatherMultiCurl($when, $lat, $long) : array |
||
| 72 | { |
||
| 73 | $dates = []; |
||
| 74 | $now = time(); |
||
| 75 | |||
| 76 | if ($when == "past") { |
||
| 77 | for ($i = 0; $i < 30; $i++) { |
||
| 78 | // 24h = 86400 unix time |
||
| 79 | $now -= 86400; |
||
| 80 | $dates[] = $now; |
||
| 81 | } |
||
| 82 | } else { |
||
| 83 | for ($i = 0; $i < 7; $i++) { |
||
| 84 | // 24h = 86400 unix time |
||
| 85 | $now += 86400; |
||
| 86 | $dates[] = $now; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | $url = "https://api.darksky.net/forecast/{$this->config->config}/{$lat},{$long}"; |
||
| 91 | |||
| 92 | $options = [ |
||
| 93 | CURLOPT_RETURNTRANSFER => true, |
||
| 94 | ]; |
||
| 95 | |||
| 96 | $mh = curl_multi_init(); |
||
| 97 | $chAll = []; |
||
| 98 | foreach ($dates as $day) { |
||
| 99 | $ch = curl_init("$url,{$day}?lang=sv&units=si"); |
||
| 100 | curl_setopt_array($ch, $options); |
||
| 101 | curl_multi_add_handle($mh, $ch); |
||
| 102 | $chAll[] = $ch; |
||
| 103 | } |
||
| 104 | |||
| 105 | // execute all queries simultaneously and countinue when all are complete |
||
| 106 | $running = null; |
||
| 107 | do { |
||
| 108 | curl_multi_exec($mh, $running); |
||
| 109 | } while ($running); |
||
| 110 | |||
| 111 | // Close handle |
||
| 112 | foreach ($chAll as $ch) { |
||
| 113 | curl_multi_remove_handle($mh, $ch); |
||
| 114 | } |
||
| 115 | curl_multi_close($mh); |
||
| 116 | |||
| 117 | //req are done, access results |
||
| 118 | $response = []; |
||
| 119 | foreach ($chAll as $ch) { |
||
| 120 | $data = curl_multi_getcontent($ch); |
||
| 121 | $response[] = json_decode($data, true); |
||
| 122 | |||
| 123 | // $weather[] = $response->currently->summary; |
||
| 124 | // $temp[] = $response->currently->temperature; |
||
| 125 | } |
||
| 126 | return $response; |
||
| 127 | } |
||
| 129 |