Passed
Push — master ( 0336f4...58f541 )
by Gaetano
05:32
created

ParallelClient::sendParallel()   B

Complexity

Conditions 10
Paths 36

Size

Total Lines 76
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

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 0
cts 64
cp 0
crap 110
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
3
require_once __DIR__ . "/_prepend.php";
4
5
use PhpXmlRpc\Encoder;
6
use PhpXmlRpc\Client;
7
use PhpXmlRpc\PhpXmlRpc;
8
use PhpXmlRpc\Request;
9
use PhpXmlRpc\Response;
10
11
/**
12
 * A class taking advantage of cURL to send many requests in parallel (to a single server), for when the given server
13
 * does not support system.multicall method
14
 */
15
class ParallelClient extends Client
16
{
17
    public function sendParallel($requests, $timeout = 0, $method = '')
18
    {
19
        if ($method == '') {
20
            $method = $this->method;
21
        }
22
23
        /// @todo validate that $method can be handled by the current curl install
24
25
        $handles = array();
26
        $curl = curl_multi_init();
27
28
        foreach($requests as $k => $req) {
29
            $req->setDebug($this->debug);
30
31
            $handle = $this->prepareCurlHandle(
32
                $req,
33
                $this->server,
34
                $this->port,
35
                $timeout,
36
                $this->username,
37
                $this->password,
38
                $this->authtype,
39
                $this->cert,
40
                $this->certpass,
41
                $this->cacert,
42
                $this->cacertdir,
43
                $this->proxy,
44
                $this->proxyport,
45
                $this->proxy_user,
46
                $this->proxy_pass,
47
                $this->proxy_authtype,
48
                $method,
49
                false,
50
                $this->key,
51
                $this->keypass,
52
                $this->sslversion
53
            );
54
            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

54
            curl_multi_add_handle(/** @scrutinizer ignore-type */ $curl, $handle);
Loading history...
55
            $handles[$k] = $handle;
56
        }
57
58
        $running = 0;
59
        do {
60
            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

60
            curl_multi_exec(/** @scrutinizer ignore-type */ $curl, $running);
Loading history...
61
        } while($running > 0);
62
63
        $responses = array();
64
        foreach($handles as $k => $h) {
65
            $responses[$k] = curl_multi_getcontent($handles[$k]);
66
67
            if ($this->debug > 1) {
68
                $message = "---CURL INFO---\n";
69
                foreach (curl_getinfo($h) as $name => $val) {
70
                    if (is_array($val)) {
71
                        $val = implode("\n", $val);
72
                    }
73
                    $message .= $name . ': ' . $val . "\n";
74
                }
75
                $message .= '---END---';
76
                $this->getLogger()->debugMessage($message);
77
            }
78
79
            //curl_close($h);
80
            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

80
            curl_multi_remove_handle(/** @scrutinizer ignore-type */ $curl, $h);
Loading history...
81
        }
82
        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

82
        curl_multi_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
83
84
        foreach($responses as $k => $resp) {
85
            if (!$resp) {
86
                $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

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