Failed Conditions
Push — master ( af2330...921095 )
by Igor
06:44 queued 03:21
created

CurlerRolling::makeResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 16
c 0
b 0
f 0
ccs 13
cts 13
cp 1
rs 9.8666
cc 1
nc 1
nop 1
crap 1
1
<?php
0 ignored issues
show
introduced by
Missing declare(strict_types=1).
Loading history...
2
3
namespace ClickHouseDB\Transport;
4
5
use ClickHouseDB\Exception\TransportException;
6
7
class CurlerRolling
8
{
9
    /**
10
     * @var int
11
     *
12
     * Max number of simultaneous requests.
13
     */
14
    private $simultaneousLimit = 10;
15
16
    /**
17
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \ClickHouseDB\Transport\CurlerRolling::$activeRequests does not specify type hint for its items.
Loading history...
18
     *
19
     * Requests currently being processed by curl
20
     */
21
    private $activeRequests = [];
22
23
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\CurlerRolling::$runningRequests with single line content, use one-line comment instead.
Loading history...
24
     * @var int
25
     */
26
    private $runningRequests = 0;
27
28
    /**
29
     * @var CurlerRequest[]
30
     *
31
     * Requests queued to be processed
32
     */
33
    private $pendingRequests = [];
34
35
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\CurlerRolling::$completedRequestCount with single line content, use one-line comment instead.
Loading history...
36
     * @return int
37
     */
38
    private $completedRequestCount = 0;
0 ignored issues
show
introduced by
Property \ClickHouseDB\Transport\CurlerRolling::$completedRequestCount does not have @var annotation.
Loading history...
39
40
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\CurlerRolling::$_pool_master with single line content, use one-line comment instead.
Loading history...
41
     * @var null|resource
0 ignored issues
show
introduced by
Null type hint should be on last position.
Loading history...
42
     */
43
    private $_pool_master = null;
44
45
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\CurlerRolling::$waitRequests with single line content, use one-line comment instead.
Loading history...
46
     * @var int
47
     */
48
    private $waitRequests = 0;
49
50
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\CurlerRolling::$handleMapTasks with single line content, use one-line comment instead.
Loading history...
51
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \ClickHouseDB\Transport\CurlerRolling::$handleMapTasks does not specify type hint for its items.
Loading history...
52
     */
53
    private $handleMapTasks = [];
54
55
    /**
0 ignored issues
show
introduced by
Empty comment
Loading history...
56
     *
57
     */
58
    public function __destructor()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::__destructor() does not have void return type hint.
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::__destructor() does not need documentation comment.
Loading history...
59
    {
60
        $this->close();
61
    }
62
63
64
    /**
65
     * @return resource
66
     */
67 10
    private function handlerMulti()
68
    {
69 10
        if (!$this->_pool_master) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
70 10
            $this->_pool_master = curl_multi_init();
0 ignored issues
show
introduced by
Function curl_multi_init() should not be referenced via a fallback global name, but via a use statement.
Loading history...
71
72 10
            if (function_exists('curl_multi_setopt')) {
0 ignored issues
show
introduced by
Function function_exists() should not be referenced via a fallback global name, but via a use statement.
Loading history...
73 10
                curl_multi_setopt($this->_pool_master, CURLMOPT_MAXCONNECTS, $this->simultaneousLimit);
0 ignored issues
show
introduced by
Function curl_multi_setopt() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Constant CURLMOPT_MAXCONNECTS should not be referenced via a fallback global name, but via a use statement.
Loading history...
74
            }
75
        }
76
77 10
        return $this->_pool_master;
78
    }
79
80
    /**
0 ignored issues
show
introduced by
Empty comment
Loading history...
81
     *
82
     */
83
    public function close()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::close() does not have void return type hint.
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::close() does not need documentation comment.
Loading history...
84
    {
85
        if ($this->_pool_master) {
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
86
            curl_multi_close($this->handlerMulti());
0 ignored issues
show
introduced by
Function curl_multi_close() should not be referenced via a fallback global name, but via a use statement.
Loading history...
87
        }
88
    }
89
90
91
    /**
92
     * @param CurlerRequest $req
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::addQueLoop() has useless @param annotation for parameter $req.
Loading history...
93
     * @param bool $checkMultiAdd
0 ignored issues
show
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
94
     * @param bool $force
0 ignored issues
show
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
95
     * @return bool
96
     * @throws TransportException
97
     */
98 15
    public function addQueLoop(CurlerRequest $req, $checkMultiAdd = true, $force = false)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::addQueLoop() does not have parameter type hint for its parameter $checkMultiAdd but it should be possible to add it based on @param annotation "bool".
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::addQueLoop() does not have parameter type hint for its parameter $force but it should be possible to add it based on @param annotation "bool".
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::addQueLoop() does not have return type hint for its return value but it should be possible to add it based on @return annotation "bool".
Loading history...
99
    {
100 15
        $id = $req->getId();
101
102 15
        if (!$id) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
103 15
            $id = $req->getUniqHash($this->completedRequestCount);
104
        }
105
106 15
        if (!$force && isset($this->pendingRequests[$id])) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
107
            if (!$checkMultiAdd) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
108
                return false;
109
            }
110
111
            throw new TransportException("Cant add exists que - cant overwrite : $id!\n");
0 ignored issues
show
Coding Style Best Practice introduced by
Variable "%s" not allowed in double quoted string; use sprintf() or concatenation instead

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
112
        }
113
114 15
        $this->pendingRequests[$id] = $req;
115 15
        return true;
116
    }
117
118
    /**
119
     * @param resource $oneHandle
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
120
     * @return CurlerResponse
121
     */
122 46
    private function makeResponse($oneHandle)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::makeResponse() does not have return type hint for its return value but it should be possible to add it based on @return annotation "CurlerResponse".
Loading history...
123
    {
124 46
        $response = curl_multi_getcontent($oneHandle);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
introduced by
Function curl_multi_getcontent() should not be referenced via a fallback global name, but via a use statement.
Loading history...
125 46
        $header_size = curl_getinfo($oneHandle, CURLINFO_HEADER_SIZE);
0 ignored issues
show
introduced by
Function curl_getinfo() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Constant CURLINFO_HEADER_SIZE should not be referenced via a fallback global name, but via a use statement.
Loading history...
126 46
        $header = substr($response, 0, $header_size);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
introduced by
Function substr() should not be referenced via a fallback global name, but via a use statement.
Loading history...
127 46
        $body = substr($response, $header_size);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
introduced by
Function substr() should not be referenced via a fallback global name, but via a use statement.
Loading history...
128
129 46
        $n = new CurlerResponse();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
130 46
        $n->_headers = $this->parse_headers_from_curl_response($header);
131 46
        $n->_body = $body;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
132 46
        $n->_info = curl_getinfo($oneHandle);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
introduced by
Function curl_getinfo() should not be referenced via a fallback global name, but via a use statement.
Loading history...
133 46
        $n->_error = curl_error($oneHandle);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
introduced by
Function curl_error() should not be referenced via a fallback global name, but via a use statement.
Loading history...
134 46
        $n->_errorNo = curl_errno($oneHandle);
0 ignored issues
show
introduced by
Function curl_errno() should not be referenced via a fallback global name, but via a use statement.
Loading history...
135 46
        $n->_useTime = 0;
136
137 46
        return $n;
138
    }
139
140
    /**
141
     * @return bool
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
142
     * @throws TransportException
143
     */
144 10
    public function execLoopWait()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::execLoopWait() does not have return type hint for its return value but it should be possible to add it based on @return annotation "bool".
Loading history...
145
    {
146 10
        $c = 0;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 21 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
147 10
        $timeStart=time();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 0 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
introduced by
Function time() should not be referenced via a fallback global name, but via a use statement.
Loading history...
148 10
        $uSleep=1000; // Timer: check response from server, and add new Task/Que to loop
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 16 spaces but found 0 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
149 10
        $PendingAllConnections=$this->countPending();
0 ignored issues
show
Unused Code introduced by
The assignment to $PendingAllConnections is dead and can be removed.
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 1 space but found 0 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
150
151
        // Max loop check
152
153 10
        $count=0;
0 ignored issues
show
Unused Code introduced by
The assignment to $count is dead and can be removed.
Loading history...
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces
Loading history...
154
        // add all tasks
155
        do {
156 10
            $this->exec();
157 10
            $timeWork=time()-$timeStart;
0 ignored issues
show
Unused Code introduced by
The assignment to $timeWork is dead and can be removed.
Loading history...
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces
Loading history...
introduced by
Function time() should not be referenced via a fallback global name, but via a use statement.
Loading history...
158
            //
0 ignored issues
show
introduced by
Empty comment
Loading history...
159 10
            $ActiveNowConnections = $this->countActive();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
160 10
            $PendingNowConnections = $this->countPending();
161
162 10
            $count=$ActiveNowConnections+$PendingNowConnections;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces
Loading history...
163 10
            $c++;
164
165 10
            if ($c > 20000) {
166
                break;
167
            }
168
169 10
            usleep($uSleep);
0 ignored issues
show
introduced by
Function usleep() should not be referenced via a fallback global name, but via a use statement.
Loading history...
170
            // usleep(2000000) == 2 seconds
171 10
        } while ($count);
172
173 10
        return true;
174
    }
175
176
    /**
177
     * @param string $response
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
178
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Transport\CurlerRolling::parse_headers_from_curl_response() does not specify type hint for items of its traversable return value.
Loading history...
179
     */
180 46
    private function parse_headers_from_curl_response($response)
0 ignored issues
show
Coding Style introduced by
Method name "CurlerRolling::parse_headers_from_curl_response" is not in camel caps format
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::parse_headers_from_curl_response() does not have parameter type hint for its parameter $response but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::parse_headers_from_curl_response() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
181
    {
182 46
        $headers = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
183 46
        $header_text = $response;
184
185 46
        foreach (explode("\r\n", $header_text) as $i => $line) {
0 ignored issues
show
introduced by
Function explode() should not be referenced via a fallback global name, but via a use statement.
Loading history...
186 46
            if ($i === 0) {
187 46
                $headers['http_code'] = $line;
188
            } else {
189 45
                $r = explode(': ', $line);
0 ignored issues
show
introduced by
Function explode() should not be referenced via a fallback global name, but via a use statement.
Loading history...
190 45
                if (sizeof($r) == 2) {
0 ignored issues
show
introduced by
Function sizeof() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Coding Style introduced by
The use of function sizeof() is forbidden; use count() instead
Loading history...
introduced by
Operator == is disallowed, use === instead.
Loading history...
191 46
                    $headers[$r[0]] = $r[1];
192
                }
193
            }
194
        }
195
196 46
        return $headers;
197
    }
198
199
    /**
200
     * @return int
201
     */
202 14
    public function countPending()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::countPending() does not have return type hint for its return value but it should be possible to add it based on @return annotation "int".
Loading history...
203
    {
204 14
        return sizeof($this->pendingRequests);
0 ignored issues
show
introduced by
Function sizeof() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Coding Style introduced by
The use of function sizeof() is forbidden; use count() instead
Loading history...
205
    }
206
207
    /**
208
     * @return int
209
     */
210 10
    public function countActive()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::countActive() does not have return type hint for its return value but it should be possible to add it based on @return annotation "int".
Loading history...
211
    {
212 10
        return count($this->activeRequests);
0 ignored issues
show
introduced by
Function count() should not be referenced via a fallback global name, but via a use statement.
Loading history...
213
    }
214
215
    /**
216
     * @return int
217
     */
218
    public function countCompleted()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::countCompleted() does not have return type hint for its return value but it should be possible to add it based on @return annotation "int".
Loading history...
219
    {
220
        return $this->completedRequestCount;
221
    }
222
223
    /**
224
     * Set the limit for how many cURL requests will be execute simultaneously.
225
     *
226
     * Please be mindful that if you set this too high, requests are likely to fail
227
     * more frequently or automated software may perceive you as a DOS attack and
228
     * automatically block further requests.
229
     *
230
     * @param int $count
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
231
     * @throws \InvalidArgumentException
0 ignored issues
show
introduced by
Class \InvalidArgumentException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
232
     * @return $this
233
     */
234
    public function setSimultaneousLimit($count)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::setSimultaneousLimit() does not have parameter type hint for its parameter $count but it should be possible to add it based on @param annotation "int".
Loading history...
235
    {
236
        if (!is_int($count) || $count < 2) {
0 ignored issues
show
introduced by
The condition is_int($count) is always true.
Loading history...
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
introduced by
Function is_int() should not be referenced via a fallback global name, but via a use statement.
Loading history...
237
            throw new \InvalidArgumentException("setSimultaneousLimit count must be an int >= 2");
0 ignored issues
show
introduced by
Class \InvalidArgumentException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
Coding Style Comprehensibility introduced by
The string literal setSimultaneousLimit count must be an int >= 2 does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
238
        }
239
240
        $this->simultaneousLimit = $count;
241
        return $this;
242
    }
243
244
    /**
245
     * @return int
246
     */
247 10
    public function getSimultaneousLimit()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::getSimultaneousLimit() does not have return type hint for its return value but it should be possible to add it based on @return annotation "int".
Loading history...
248
    {
249 10
        return $this->simultaneousLimit;
250
    }
251
252
    /**
253
     * @return int
254
     */
255
    public function getRunningRequests()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::getRunningRequests() does not have return type hint for its return value but it should be possible to add it based on @return annotation "int".
Loading history...
256
    {
257
        return $this->runningRequests;
258
    }
259
260
    /**
261
     * @param CurlerRequest $request
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::execOne() has useless @param annotation for parameter $request.
Loading history...
262
     * @param bool $auto_close
0 ignored issues
show
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
263
     * @return mixed
264
     * @throws TransportException
265
     */
266 46
    public function execOne(CurlerRequest $request, $auto_close = false)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::execOne() does not have parameter type hint for its parameter $auto_close but it should be possible to add it based on @param annotation "bool".
Loading history...
267
    {
268 46
        $h = $request->handle();
269 46
        curl_exec($h);
0 ignored issues
show
introduced by
Function curl_exec() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Bug introduced by
It seems like $h can also be of type false; however, parameter $ch of curl_exec() does only seem to accept 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

269
        curl_exec(/** @scrutinizer ignore-type */ $h);
Loading history...
270
271 46
        $request->setResponse($this->makeResponse($h));
0 ignored issues
show
Bug introduced by
It seems like $h can also be of type false; however, parameter $oneHandle of ClickHouseDB\Transport\C...Rolling::makeResponse() does only seem to accept 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

271
        $request->setResponse($this->makeResponse(/** @scrutinizer ignore-type */ $h));
Loading history...
272
273 46
        if ($auto_close) {
274 4
            $request->close();
275
        }
276
277 46
        return $request->response()->http_code();
278
    }
279
280
    /**
281
     * @return string
282
     */
283
    public function getInfo()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::getInfo() does not have return type hint for its return value but it should be possible to add it based on @return annotation "string".
Loading history...
284
    {
285
        return "runningRequests = {$this->runningRequests} , pending=" . sizeof($this->pendingRequests) . " ";
0 ignored issues
show
Coding Style Best Practice introduced by
Variable "%s" not allowed in double quoted string; use sprintf() or concatenation instead

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
introduced by
Function sizeof() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Coding Style introduced by
The use of function sizeof() is forbidden; use count() instead
Loading history...
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
286
    }
287
288
    /**
289
     * @throws TransportException
290
     */
291 10
    public function exec()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::exec() does not have return type hint nor @return annotation for its return value.
Loading history...
292
    {
293 10
        $this->makePendingRequestsQue();
294
295
        // ensure we're running
296
        // a request was just completed -- find out which one
297
298 10
        while (($execrun = curl_multi_exec($this->handlerMulti(), $running)) == CURLM_CALL_MULTI_PERFORM);
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
introduced by
Function curl_multi_exec() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Operator == is disallowed, use === instead.
Loading history...
introduced by
Constant CURLM_CALL_MULTI_PERFORM should not be referenced via a fallback global name, but via a use statement.
Loading history...
299
300 10
        if ($execrun != CURLM_OK) {
0 ignored issues
show
introduced by
Operator != is disallowed, use !== instead.
Loading history...
introduced by
Constant CURLM_OK should not be referenced via a fallback global name, but via a use statement.
Loading history...
301
            throw new TransportException("[ NOT CURLM_OK ]");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal [ NOT CURLM_OK ] does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
302
        }
303
304 10
        $this->runningRequests = $running;
305
306 10
        while ($done = curl_multi_info_read($this->handlerMulti())) {
0 ignored issues
show
introduced by
Function curl_multi_info_read() should not be referenced via a fallback global name, but via a use statement.
Loading history...
307 10
            $response = $this->makeResponse($done['handle']);
308
309
            // send the return values to the callback function.
310
311 10
            $key = (string) $done['handle'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
312 10
            $task_id = $this->handleMapTasks[$key];
313 10
            $request = $this->pendingRequests[$this->handleMapTasks[$key]];
314
315 10
            unset($this->handleMapTasks[$key]);
316 10
            unset($this->activeRequests[$task_id]);
317
318 10
            $this->pendingRequests[$task_id]->setResponse($response);
319 10
            $this->pendingRequests[$task_id]->onCallback();
320
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
321
322 10
            if (!$request->isPersistent()) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
323 10
                unset($this->pendingRequests[$task_id]);
324
            }
325
326 10
            $this->completedRequestCount++;
327
328
            // remove the curl handle that just completed
329 10
            curl_multi_remove_handle($this->handlerMulti(), $done['handle']);
0 ignored issues
show
introduced by
Function curl_multi_remove_handle() should not be referenced via a fallback global name, but via a use statement.
Loading history...
330
331
            // if something was requeued, this will get it running/update our loop check values
332 10
            $status = curl_multi_exec($this->handlerMulti(), $active);
0 ignored issues
show
Unused Code introduced by
The assignment to $status is dead and can be removed.
Loading history...
introduced by
Function curl_multi_exec() should not be referenced via a fallback global name, but via a use statement.
Loading history...
333
        }
334
335
        // see if there is anything to read
336 10
        curl_multi_select($this->handlerMulti(), 0.01);
0 ignored issues
show
introduced by
Function curl_multi_select() should not be referenced via a fallback global name, but via a use statement.
Loading history...
337 10
        return $this->countActive();
338
    }
339
340 10
    public function makePendingRequestsQue()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::makePendingRequestsQue() does not have void return type hint.
Loading history...
341
    {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
342
343 10
        $max = $this->getSimultaneousLimit();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
344 10
        $active = $this->countActive();
345
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
346
347 10
        if ($active < $max) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
348
349 10
            $canAdd = $max - $active;
350
//            $pending = sizeof($this->pendingRequests);
351
352 10
            $add = [];
353
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
354
355 10
            foreach ($this->pendingRequests as $task_id => $params) {
356 10
                if (empty($this->activeRequests[$task_id])) {
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
357 10
                    $add[$task_id] = $task_id;
358
                }
359
            }
360
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
361
362 10
            if (sizeof($add)) {
0 ignored issues
show
introduced by
Function sizeof() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Coding Style introduced by
The use of function sizeof() is forbidden; use count() instead
Loading history...
363 10
                if ($canAdd >= sizeof($add)) {
0 ignored issues
show
introduced by
Function sizeof() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Coding Style introduced by
The use of function sizeof() is forbidden; use count() instead
Loading history...
364 10
                    $ll = $add;
365
                } else {
366 1
                    $ll = array_rand($add, $canAdd);
0 ignored issues
show
introduced by
Function array_rand() should not be referenced via a fallback global name, but via a use statement.
Loading history...
367 1
                    if (!is_array($ll)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
introduced by
Function is_array() should not be referenced via a fallback global name, but via a use statement.
Loading history...
368
                        $ll = array($ll => $ll);
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
369
                    }
370
                }
371
372 10
                foreach ($ll as $task_id) {
373 10
                    $this->_prepareLoopQue($task_id);
374
                }
375
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
376
            }// if add
377
        }// if can add
378 10
    }
379
380
    /**
381
     * @param string $task_id
382
     */
383 10
    private function _prepareLoopQue($task_id)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::_prepareLoopQue() does not have parameter type hint for its parameter $task_id but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::_prepareLoopQue() does not have void return type hint.
Loading history...
384
    {
385 10
        $this->activeRequests[$task_id] = 1;
386 10
        $this->waitRequests++;
387
388 10
        $h = $this->pendingRequests[$task_id]->handle();
389
390
        // pool
391 10
        curl_multi_add_handle($this->handlerMulti(), $h);
0 ignored issues
show
introduced by
Function curl_multi_add_handle() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Bug introduced by
It seems like $h can also be of type false; however, parameter $ch of curl_multi_add_handle() does only seem to accept 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

391
        curl_multi_add_handle($this->handlerMulti(), /** @scrutinizer ignore-type */ $h);
Loading history...
392
393 10
        $key = (string) $h;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 24 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
394 10
        $this->handleMapTasks[$key] = $task_id;
395 10
    }
396
}
397