Issues (5)

src/Curl/Curl.php (2 issues)

1
<?php
2
namespace Oliver\Curl;
3
4
use Oliver\Curl\CurlInterface;
5
6
/**
7
 *
8
 */
9
class Curl implements CurlInterface
10
{
11
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++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

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:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
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++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

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:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
30 5
            $results[] = (json_decode(curl_multi_getcontent($curlHandles[$i]), true));
31
        }
32 5
        return $results;
33
    }
34
}
35