CurlerRolling   B
last analyzed

Complexity

Total Complexity 45

Size/Duplication

Total Lines 376
Duplicated Lines 0 %

Test Coverage

Coverage 80.95%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 120
c 5
b 0
f 0
dl 0
loc 376
ccs 102
cts 126
cp 0.8095
rs 8.8
wmc 45

18 Methods

Rating   Name   Duplication   Size   Complexity  
A execOne() 0 12 2
A getRunningRequests() 0 3 1
A close() 0 4 2
A execLoopWait() 0 8 2
A handlerMulti() 0 11 3
A countActive() 0 3 1
A countPending() 0 3 1
A setSimultaneousLimit() 0 8 3
A countCompleted() 0 3 1
A getSimultaneousLimit() 0 3 1
A getInfo() 0 3 1
A addQueLoop() 0 18 5
A parse_headers_from_curl_response() 0 17 4
B exec() 0 53 6
A _prepareLoopQue() 0 17 2
B makePendingRequestsQue() 0 32 8
A makeResponse() 0 16 1
A __destruct() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like CurlerRolling often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CurlerRolling, and based on these observations, apply Extract Interface, too.

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
    const SLEEP_DELAY = 1000; // 1ms
0 ignored issues
show
introduced by
Constant \ClickHouseDB\Transport\CurlerRolling::SLEEP_DELAY visibility missing.
Loading history...
10
11
    /**
12
     * @var int
13
     *
14
     * Max number of simultaneous requests.
15
     */
16
    private $simultaneousLimit = 10;
17
18
    /**
19
     * @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...
20
     *
21
     * Requests currently being processed by curl
22
     */
23
    private $activeRequests = [];
24
25
    /**
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...
26
     * @var int
27
     */
28
    private $runningRequests = 0;
29
30
    /**
31
     * @var CurlerRequest[]
32
     *
33
     * Requests queued to be processed
34
     */
35
    private $pendingRequests = [];
36
37
    /**
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...
38
     * @return int
39
     */
40
    private $completedRequestCount = 0;
0 ignored issues
show
introduced by
Property \ClickHouseDB\Transport\CurlerRolling::$completedRequestCount does not have @var annotation for its value.
Loading history...
41
42
    /**
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...
43
     * @var null|resource
0 ignored issues
show
introduced by
Null type hint should be on last position in "null|resource".
Loading history...
44
     */
45
    private $_pool_master = null;
0 ignored issues
show
Coding Style introduced by
Member variable "_pool_master" is not in valid camel caps format
Loading history...
46
47
    /**
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...
48
     * @var int
49
     */
50
    private $waitRequests = 0;
51
52
    /**
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...
53
     * @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...
54
     */
55
    private $handleMapTasks = [];
56
57
    /**
0 ignored issues
show
introduced by
Empty comment
Loading history...
58
     *
59
     */
60
    public function __destruct()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::__destruct() does not need documentation comment.
Loading history...
61
    {
62
        $this->close();
63
    }
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line after function; 2 found
Loading history...
64
65
66
    /**
67
     * @return resource
68
     */
69 10
    private function handlerMulti()
70
    {
71 10
        if (!$this->_pool_master) {
0 ignored issues
show
Coding Style introduced by
Member variable "_pool_master" is not in valid camel caps format
Loading history...
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
72 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...
Documentation Bug introduced by
It seems like curl_multi_init() of type CurlMultiHandle or true is incompatible with the declared type null|resource of property $_pool_master.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Coding Style introduced by
Member variable "_pool_master" is not in valid camel caps format
Loading history...
73
74 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...
75 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...
Bug introduced by
It seems like $this->_pool_master can also be of type true; however, parameter $multi_handle of curl_multi_setopt() 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

75
                curl_multi_setopt(/** @scrutinizer ignore-type */ $this->_pool_master, CURLMOPT_MAXCONNECTS, $this->simultaneousLimit);
Loading history...
Coding Style introduced by
Member variable "_pool_master" is not in valid camel caps format
Loading history...
76
            }
77
        }
78
79 10
        return $this->_pool_master;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_pool_master also could return the type CurlMultiHandle|true which is incompatible with the documented return type resource.
Loading history...
Coding Style introduced by
Member variable "_pool_master" is not in valid camel caps format
Loading history...
80
    }
81
82
    /**
0 ignored issues
show
introduced by
Empty comment
Loading history...
83
     *
84
     */
85
    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...
86
    {
87
        if ($this->_pool_master) {
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
Coding Style introduced by
Member variable "_pool_master" is not in valid camel caps format
Loading history...
88
            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...
89
        }
90
    }
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line after function; 2 found
Loading history...
91
92
93
    /**
94
     * @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...
95
     * @param bool $checkMultiAdd
0 ignored issues
show
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
96
     * @param bool $force
0 ignored issues
show
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
97
     * @return bool
98
     * @throws TransportException
99
     */
100 15
    public function addQueLoop(CurlerRequest $req, $checkMultiAdd = true, $force = false)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::addQueLoop() does not have native 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 native 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 native return type hint for its return value but it should be possible to add it based on @return annotation "bool".
Loading history...
101
    {
102 15
        $id = $req->getId();
103
104 15
        if (!$id) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
105 15
            $id = $req->getUniqHash($this->completedRequestCount);
106
        }
107
108 15
        if (!$force && isset($this->pendingRequests[$id])) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
109
            if (!$checkMultiAdd) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
110
                return false;
111
            }
112
113
            throw new TransportException("Cant add exists que - cant overwrite : $id!\n");
0 ignored issues
show
Coding Style Best Practice introduced by
Variable "$id" 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...
114
        }
115
116 15
        $this->pendingRequests[$id] = $req;
117 15
        return true;
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
118
    }
119
120
    /**
121
     * @param resource $oneHandle
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
122
     * @return CurlerResponse
123
     */
124 49
    private function makeResponse($oneHandle)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::makeResponse() does not have native return type hint for its return value but it should be possible to add it based on @return annotation "CurlerResponse".
Loading history...
125
    {
126 49
        $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...
127 49
        $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...
Coding Style introduced by
The variable $header_size should be in camel caps format.
Loading history...
128 49
        $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...
Coding Style introduced by
The variable $header_size should be in camel caps format.
Loading history...
129 49
        $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...
Coding Style introduced by
The variable $header_size should be in camel caps format.
Loading history...
130
131 49
        $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...
132 49
        $n->_headers = $this->parse_headers_from_curl_response($header);
133 49
        $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...
134 49
        $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...
135 49
        $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...
136 49
        $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...
137 49
        $n->_useTime = 0;
138
139 49
        return $n;
140
    }
141
142
    /**
143
     * @return bool
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
144
     * @throws TransportException
145
     */
146 10
    public function execLoopWait()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::execLoopWait() does not have native return type hint for its return value but it should be possible to add it based on @return annotation "bool".
Loading history...
147
    {
148
        do {
149 10
            $this->exec();
150 10
            usleep(self::SLEEP_DELAY);
0 ignored issues
show
introduced by
Function usleep() should not be referenced via a fallback global name, but via a use statement.
Loading history...
151 10
        } while (($this->countActive() + $this->countPending()) > 0);
0 ignored issues
show
introduced by
Useless parentheses.
Loading history...
152
153 10
        return true;
154
    }
155
156
    /**
157
     * @param string $response
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
158
     * @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...
159
     */
160 49
    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 native 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 native return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
161
    {
162 49
        $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...
163 49
        $header_text = $response;
0 ignored issues
show
Coding Style introduced by
The variable $header_text should be in camel caps format.
Loading history...
164
165 49
        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...
Coding Style introduced by
The variable $header_text should be in camel caps format.
Loading history...
166 49
            if ($i === 0) {
167 49
                $headers['http_code'] = $line;
168
            } else {
169 48
                $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...
170 48
                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...
171 48
                    $headers[$r[0]] = $r[1];
172
                }
173
            }
174
        }
175
176 49
        return $headers;
177
    }
178
179
    /**
180
     * @return int
181
     */
182 14
    public function countPending()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::countPending() does not have native return type hint for its return value but it should be possible to add it based on @return annotation "int".
Loading history...
183
    {
184 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...
185
    }
186
187
    /**
188
     * @return int
189
     */
190 10
    public function countActive()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::countActive() does not have native return type hint for its return value but it should be possible to add it based on @return annotation "int".
Loading history...
191
    {
192 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...
193
    }
194
195
    /**
196
     * @return int
197
     */
198
    public function countCompleted()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::countCompleted() does not have native return type hint for its return value but it should be possible to add it based on @return annotation "int".
Loading history...
199
    {
200
        return $this->completedRequestCount;
201
    }
202
203
    /**
204
     * Set the limit for how many cURL requests will be execute simultaneously.
205
     *
206
     * Please be mindful that if you set this too high, requests are likely to fail
207
     * more frequently or automated software may perceive you as a DOS attack and
208
     * automatically block further requests.
209
     *
210
     * @param int $count
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
211
     * @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...
212
     * @return $this
213
     */
214
    public function setSimultaneousLimit($count)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::setSimultaneousLimit() does not have native type hint for its parameter $count but it should be possible to add it based on @param annotation "int".
Loading history...
215
    {
216
        if (!is_int($count) || $count < 2) {
0 ignored issues
show
introduced by
The condition is_int($count) is always true.
Loading history...
introduced by
Function is_int() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
217
            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...
218
        }
219
220
        $this->simultaneousLimit = $count;
221
        return $this;
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
222
    }
223
224
    /**
225
     * @return int
226
     */
227 10
    public function getSimultaneousLimit()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::getSimultaneousLimit() does not have native return type hint for its return value but it should be possible to add it based on @return annotation "int".
Loading history...
228
    {
229 10
        return $this->simultaneousLimit;
230
    }
231
232
    /**
233
     * @return int
234
     */
235
    public function getRunningRequests()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::getRunningRequests() does not have native return type hint for its return value but it should be possible to add it based on @return annotation "int".
Loading history...
236
    {
237
        return $this->runningRequests;
238
    }
239
240
    /**
241
     * @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...
242
     * @param bool $auto_close
0 ignored issues
show
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
243
     * @return mixed
244
     * @throws TransportException
245
     */
246 49
    public function execOne(CurlerRequest $request, $auto_close = false)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::execOne() does not have native type hint for its parameter $auto_close but it should be possible to add it based on @param annotation "bool".
Loading history...
Coding Style introduced by
The variable $auto_close should be in camel caps format.
Loading history...
247
    {
248 49
        $h = $request->handle();
249 49
        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...
250
251 49
        $request->setResponse($this->makeResponse($h));
0 ignored issues
show
Bug introduced by
It seems like $h can also be of type CurlHandle; 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

251
        $request->setResponse($this->makeResponse(/** @scrutinizer ignore-type */ $h));
Loading history...
252
253 49
        if ($auto_close) {
0 ignored issues
show
Coding Style introduced by
The variable $auto_close should be in camel caps format.
Loading history...
254 4
            $request->close();
255
        }
256
257 49
        return $request->response()->http_code();
258
    }
259
260
    /**
261
     * @return string
262
     */
263
    public function getInfo()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::getInfo() does not have native return type hint for its return value but it should be possible to add it based on @return annotation "string".
Loading history...
264
    {
265
        return "runningRequests = {$this->runningRequests} , pending=" . sizeof($this->pendingRequests) . " ";
0 ignored issues
show
Coding Style Best Practice introduced by
Variable "$this" 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...
266
    }
267
268
    /**
269
     * @throws TransportException
270
     */
271 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...
272
    {
273 10
        $this->makePendingRequestsQue();
274
275
        // ensure we're running
276
        // a request was just completed -- find out which one
277
278 10
        while (($execrun = curl_multi_exec($this->handlerMulti(), $running)) == CURLM_CALL_MULTI_PERFORM);
0 ignored issues
show
introduced by
Function curl_multi_exec() should not be referenced via a fallback global name, but via a use statement.
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...
introduced by
Operator == is disallowed, use === instead.
Loading history...
279
280 10
        if ($execrun != CURLM_OK) {
0 ignored issues
show
introduced by
Constant CURLM_OK 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...
281
            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...
282
        }
283
284 10
        $this->runningRequests = $running;
285
286 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...
287 10
            $response = $this->makeResponse($done['handle']);
288
289
            // send the return values to the callback function.
290
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
291
292 10
            if (is_object($done['handle'])) {
0 ignored issues
show
introduced by
Function is_object() should not be referenced via a fallback global name, but via a use statement.
Loading history...
293
                $key = spl_object_id( $done['handle'] );
0 ignored issues
show
introduced by
Function spl_object_id() should not be referenced via a fallback global name, but via a use statement.
Loading history...
294
            } else {
295 10
                $key = (string) $done['handle'] ;
0 ignored issues
show
Coding Style introduced by
Space found before semicolon; expected "];" but found "] ;"
Loading history...
296
            }
297
298 10
            $task_id = $this->handleMapTasks[$key];
0 ignored issues
show
Coding Style introduced by
The variable $task_id should be in camel caps format.
Loading history...
299 10
            $request = $this->pendingRequests[$this->handleMapTasks[$key]];
300
301 10
            unset($this->handleMapTasks[$key]);
302 10
            unset($this->activeRequests[$task_id]);
0 ignored issues
show
Coding Style introduced by
The variable $task_id should be in camel caps format.
Loading history...
303
304 10
            $this->pendingRequests[$task_id]->setResponse($response);
0 ignored issues
show
Coding Style introduced by
The variable $task_id should be in camel caps format.
Loading history...
305 10
            $this->pendingRequests[$task_id]->onCallback();
0 ignored issues
show
Coding Style introduced by
The variable $task_id should be in camel caps format.
Loading history...
306
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
307
308 10
            if (!$request->isPersistent()) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
309 10
                unset($this->pendingRequests[$task_id]);
0 ignored issues
show
Coding Style introduced by
The variable $task_id should be in camel caps format.
Loading history...
310
            }
311
312 10
            $this->completedRequestCount++;
313
314
            // remove the curl handle that just completed
315 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...
316
317
            // if something was requeued, this will get it running/update our loop check values
318 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...
319
        }
320
321
        // see if there is anything to read
322 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...
323 10
        return $this->countActive();
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
324
    }
325
326 10
    public function makePendingRequestsQue()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::makePendingRequestsQue() does not have void return type hint.
Loading history...
327
    {
328 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...
329 10
        $active = $this->countActive();
330
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
331
332 10
        if ($active < $max) {
333 10
            $canAdd = $max - $active;
334
//            $pending = sizeof($this->pendingRequests);
335
336 10
            $add = [];
337
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
338
339 10
            foreach ($this->pendingRequests as $task_id => $params) {
0 ignored issues
show
introduced by
Expected 1 lines after "foreach", found 2.
Loading history...
Coding Style introduced by
The variable $task_id should be in camel caps format.
Loading history...
340 10
                if (empty($this->activeRequests[$task_id])) {
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
Coding Style introduced by
The variable $task_id should be in camel caps format.
Loading history...
341 10
                    $add[$task_id] = $task_id;
0 ignored issues
show
Coding Style introduced by
The variable $task_id should be in camel caps format.
Loading history...
342
                }
343
            }
344
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
345
346 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...
347 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...
348 10
                    $ll = $add;
349
                } else {
350 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...
351 1
                    if (!is_array($ll)) {
0 ignored issues
show
introduced by
Function is_array() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
352 1
                        $ll = array($ll => $ll);
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
353
                    }
354
                }
355
356 10
                foreach ($ll as $task_id) {
0 ignored issues
show
Coding Style introduced by
The variable $task_id should be in camel caps format.
Loading history...
357 10
                    $this->_prepareLoopQue($task_id);
0 ignored issues
show
Coding Style introduced by
The variable $task_id should be in camel caps format.
Loading history...
358
                }
359
            }// if add
360
        }// if can add
361 10
    }
362
363
    /**
364
     * @param string $task_id
365
     */
366 10
    private function _prepareLoopQue($task_id)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerRolling::_prepareLoopQue() does not have native 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...
Coding Style introduced by
The variable $task_id should be in camel caps format.
Loading history...
367
    {
368 10
        $this->activeRequests[$task_id] = 1;
0 ignored issues
show
Coding Style introduced by
The variable $task_id should be in camel caps format.
Loading history...
369 10
        $this->waitRequests++;
370
371 10
        $h = $this->pendingRequests[$task_id]->handle();
0 ignored issues
show
Coding Style introduced by
The variable $task_id should be in camel caps format.
Loading history...
372
373
        // pool
374 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...
375
376 10
        if (is_object($h)) {
0 ignored issues
show
introduced by
Function is_object() should not be referenced via a fallback global name, but via a use statement.
Loading history...
377
            $key = spl_object_id( $h );
0 ignored issues
show
introduced by
Function spl_object_id() should not be referenced via a fallback global name, but via a use statement.
Loading history...
378
        } else {
379 10
            $key = (string) $h ;
0 ignored issues
show
Coding Style introduced by
Space found before semicolon; expected "$h;" but found "$h ;"
Loading history...
380
        }
381
382 10
        $this->handleMapTasks[$key] = $task_id;
0 ignored issues
show
Coding Style introduced by
The variable $task_id should be in camel caps format.
Loading history...
383 10
    }
384
}
385