Passed
Push — master ( 251b1a...cf6e60 )
by Gaetano
08:24
created

ParallelClient::sendParallel()   B

Complexity

Conditions 10
Paths 36

Size

Total Lines 76
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 10.4354

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 10
eloc 53
c 1
b 0
f 1
nc 36
nop 3
dl 0
loc 76
ccs 41
cts 49
cp 0.8367
crap 10.4354
rs 7.1587

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
require_once __DIR__ . "/_prepend.php";
3
4
use PhpXmlRpc\Encoder;
5
use PhpXmlRpc\Client;
6
use PhpXmlRpc\PhpXmlRpc;
7
use PhpXmlRpc\Request;
8
use PhpXmlRpc\Response;
9
10
/**
11
 * A class taking advantage of cURL to send many requests in parallel (to a single server), for when the given server
12
 * does not support system.multicall method
13
 */
14
class ParallelClient extends Client
15
{
16
    public function sendParallel($requests, $timeout = 0, $method = '')
17 1
    {
18
        if ($method == '') {
19 1
            $method = $this->method;
20 1
        }
21
22
        /// @todo validate that $method can be handled by the current curl install
23
24
        $handles = array();
25 1
        $curl = curl_multi_init();
26 1
27
        foreach($requests as $k => $req) {
28 1
            $req->setDebug($this->debug);
29 1
30
            $handle = $this->prepareCurlHandle(
31 1
                $req,
32 1
                $this->server,
33 1
                $this->port,
34 1
                $timeout,
35
                $this->username,
36 1
                $this->password,
37 1
                $this->authtype,
38 1
                $this->cert,
39 1
                $this->certpass,
40 1
                $this->cacert,
41 1
                $this->cacertdir,
42 1
                $this->proxy,
43 1
                $this->proxyport,
44 1
                $this->proxy_user,
45 1
                $this->proxy_pass,
46 1
                $this->proxy_authtype,
47 1
                $method,
48
                false,
49 1
                $this->key,
50 1
                $this->keypass,
51 1
                $this->sslversion
52 1
            );
53
            curl_multi_add_handle($curl, $handle);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type true; however, parameter $multi_handle of curl_multi_add_handle() does only seem to accept CurlMultiHandle|resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
            curl_multi_add_handle(/** @scrutinizer ignore-type */ $curl, $handle);
Loading history...
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of curl_multi_add_handle() does only seem to accept CurlHandle|resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
            curl_multi_add_handle($curl, /** @scrutinizer ignore-type */ $handle);
Loading history...
54 1
            $handles[$k] = $handle;
55 1
        }
56
57
        $running = 0;
58 1
        do {
59
            curl_multi_exec($curl, $running);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type true; however, parameter $multi_handle of curl_multi_exec() does only seem to accept CurlMultiHandle|resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
            curl_multi_exec(/** @scrutinizer ignore-type */ $curl, $running);
Loading history...
60 1
        } while($running > 0);
61 1
62
        $responses = array();
63 1
        foreach($handles as $k => $h) {
64 1
            $responses[$k] = curl_multi_getcontent($handles[$k]);
65 1
66
            if ($this->debug > 1) {
67 1
                $message = "---CURL INFO---\n";
68
                foreach (curl_getinfo($h) as $name => $val) {
69
                    if (is_array($val)) {
70
                        $val = implode("\n", $val);
71
                    }
72
                    $message .= $name . ': ' . $val . "\n";
73
                }
74
                $message .= '---END---';
75
                $this->getLogger()->debugMessage($message);
76
            }
77
78
            //curl_close($h);
79
            curl_multi_remove_handle($curl, $h);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type true; however, parameter $multi_handle of curl_multi_remove_handle() does only seem to accept CurlMultiHandle|resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
            curl_multi_remove_handle(/** @scrutinizer ignore-type */ $curl, $h);
Loading history...
80 1
        }
81
        curl_multi_close($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type true; however, parameter $multi_handle of curl_multi_close() does only seem to accept CurlMultiHandle|resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
        curl_multi_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
82 1
83
        foreach($responses as $k => $resp) {
84 1
            if (!$resp) {
85 1
                $responses[$k] = new Response(0, PhpXmlRpc::$xmlrpcerr['curl_fail'], PhpXmlRpc::$xmlrpcstr['curl_fail'] . ': ' . curl_error($curl));
0 ignored issues
show
Bug introduced by
$curl of type CurlMultiHandle|true is incompatible with the type CurlHandle|resource expected by parameter $handle of curl_error(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
                $responses[$k] = new Response(0, PhpXmlRpc::$xmlrpcerr['curl_fail'], PhpXmlRpc::$xmlrpcstr['curl_fail'] . ': ' . curl_error(/** @scrutinizer ignore-type */ $curl));
Loading history...
86
            } else {
87
                $responses[$k] = $requests[$k]->parseResponse($resp, true, $this->return_type);
88 1
            }
89
        }
90
91
        return $responses;
92 1
    }
93
}
94
95
$num_tests = 25;
96 1
97
$data = array(1, 1.0, 'hello world', true, '20051021T23:43:00', -1, 11.0, '~!@#$%^&*()_+|', false, '20051021T23:43:00');
98 1
$encoder = new Encoder();
99 1
$value = $encoder->encode($data, array('auto_dates'));
100 1
$req = new Request('interopEchoTests.echoValue', array($value));
101 1
$reqs = array();
102 1
for ($i = 0; $i < $num_tests; $i++) {
103 1
    $reqs[] = $req;
104 1
}
105
106
$client = new ParallelClient(XMLRPCSERVER);
107 1
$client->no_multicall = true;
108 1
109
$t = microtime(true);
110 1
$resp = $client->send($reqs);
111 1
$t = microtime(true) - $t;
112 1
echo "Sequential send: $t secs\n";
113 1
114
$t = microtime(true);
115 1
$resp = $client->sendParallel($reqs);
116 1
$t = microtime(true) - $t;
117 1
echo "Parallel send: $t secs\n";
118 1
119
$client->no_multicall = false;
120 1
$t = microtime(true);
121 1
$resp = $client->send($reqs);
122 1
$t = microtime(true) - $t;
123
echo "Multicall send: $t secs\n";
124