| Conditions | 4 |
| Paths | 4 |
| Total Lines | 21 |
| Code Lines | 14 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 14 |
| CRAP Score | 4 |
| Changes | 0 | ||
| 1 | <?php |
||
| 12 | 5 | public function fetchData(array $urls) : array |
|
| 13 | { |
||
| 14 | 5 | $curlHandles = []; |
|
| 15 | 5 | $multiHandle = curl_multi_init(); |
|
| 16 | |||
| 17 | 5 | for ($i=0; $i < count($urls); $i++) { |
|
|
|
|||
| 18 | 5 | $curlHandles[$i] = curl_init($urls[$i]); |
|
| 19 | 5 | curl_setopt($curlHandles[$i], CURLOPT_RETURNTRANSFER, true); |
|
| 20 | 5 | curl_multi_add_handle($multiHandle, $curlHandles[$i]); |
|
| 21 | } |
||
| 22 | |||
| 23 | 5 | $running = null; |
|
| 24 | do { |
||
| 25 | 5 | curl_multi_exec($multiHandle, $running); |
|
| 26 | 5 | } while ($running); |
|
| 27 | |||
| 28 | 5 | $results = []; |
|
| 29 | 5 | for ($i=0; $i < count($curlHandles); $i++) { |
|
| 30 | 5 | $results[] = (json_decode(curl_multi_getcontent($curlHandles[$i]), true)); |
|
| 31 | } |
||
| 32 | 5 | return $results; |
|
| 33 | } |
||
| 35 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: