Passed
Push — master ( 48f8d7...e8e671 )
by Igor
09:46
created

Http::streaming()   C

Complexity

Conditions 9
Paths 322

Size

Total Lines 56
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 9.5599

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 29
c 1
b 0
f 0
nc 322
nop 2
dl 0
loc 56
ccs 17
cts 21
cp 0.8095
crap 9.5599
rs 5.6971

How to fix   Long Method   

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
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
use ClickHouseDB\Query\Degeneration;
7
use ClickHouseDB\Query\Query;
8
use ClickHouseDB\Query\WhereInFile;
9
use ClickHouseDB\Query\WriteToFile;
10
use ClickHouseDB\Settings;
11
use ClickHouseDB\Statement;
12
use const PHP_EOL;
0 ignored issues
show
introduced by
Expected 1 lines between different types of use statement, found 0.
Loading history...
13
14
class Http
15
{
16
    const AUTH_METHOD_HEADER       = 1;
0 ignored issues
show
introduced by
Constant \ClickHouseDB\Transport\Http::AUTH_METHOD_HEADER visibility missing.
Loading history...
17
    const AUTH_METHOD_QUERY_STRING = 2;
0 ignored issues
show
introduced by
Constant \ClickHouseDB\Transport\Http::AUTH_METHOD_QUERY_STRING visibility missing.
Loading history...
18
    const AUTH_METHOD_BASIC_AUTH   = 3;
0 ignored issues
show
introduced by
Constant \ClickHouseDB\Transport\Http::AUTH_METHOD_BASIC_AUTH visibility missing.
Loading history...
19
20
    const AUTH_METHODS_LIST = [
0 ignored issues
show
introduced by
Constant \ClickHouseDB\Transport\Http::AUTH_METHODS_LIST visibility missing.
Loading history...
21
        self::AUTH_METHOD_HEADER,
22
        self::AUTH_METHOD_QUERY_STRING,
23
        self::AUTH_METHOD_BASIC_AUTH,
24
    ];
25
26
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\Http::$_username with single line content, use one-line comment instead.
Loading history...
27
     * @var string
28
     */
29
    private $_username = null;
30
31
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\Http::$_password with single line content, use one-line comment instead.
Loading history...
32
     * @var string
33
     */
34
    private $_password = null;
35
36
    /**
37
     * The username and password can be indicated in one of three ways:
38
     *  - Using HTTP Basic Authentication.
39
     *  - In the ‘user’ and ‘password’ URL parameters.
40
     *  - Using ‘X-ClickHouse-User’ and ‘X-ClickHouse-Key’ headers (by default)
41
     *
42
     * @see https://clickhouse.tech/docs/en/interfaces/http/
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
43
     * @var int
44
     */
45
    private $_authMethod = self::AUTH_METHOD_HEADER;
46
47
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\Http::$_host with single line content, use one-line comment instead.
Loading history...
48
     * @var string
49
     */
50
    private $_host = '';
51
52
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\Http::$_port with single line content, use one-line comment instead.
Loading history...
53
     * @var int
54
     */
55
    private $_port = 0;
56
57
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\Http::$_verbose with single line content, use one-line comment instead.
Loading history...
58
     * @var bool|int
59
     */
60
    private $_verbose = false;
61
62
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\Http::$_curler with single line content, use one-line comment instead.
Loading history...
63
     * @var CurlerRolling
64
     */
65
    private $_curler = null;
66
67
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\Http::$_settings with single line content, use one-line comment instead.
Loading history...
68
     * @var Settings
69
     */
70
    private $_settings = null;
71
72
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\Http::$_query_degenerations with single line content, use one-line comment instead.
Loading history...
73
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \ClickHouseDB\Transport\Http::$_query_degenerations does not specify type hint for its items.
Loading history...
74
     */
75
    private $_query_degenerations = [];
0 ignored issues
show
Coding Style introduced by
Member variable "_query_degenerations" is not in valid camel caps format
Loading history...
76
77
    /**
78
     * Count seconds (int)
79
     *
80
     * @var int
81
     */
82
    private $_connectTimeOut = 5;
83
84
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\Http::$xClickHouseProgress with single line content, use one-line comment instead.
Loading history...
85
     * @var callable
86
     */
87
    private $xClickHouseProgress = null;
88
89
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\Http::$sslCA with single line content, use one-line comment instead.
Loading history...
90
     * @var null|string
0 ignored issues
show
introduced by
Null type hint should be on last position in "null|string".
Loading history...
91
     */
92
    private $sslCA = null;
93
94
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\Http::$stdErrOut with single line content, use one-line comment instead.
Loading history...
95
     * @var null|resource
0 ignored issues
show
introduced by
Null type hint should be on last position in "null|resource".
Loading history...
96
     */
97
    private $stdErrOut = null;
98
    /**
99
     * Http constructor.
0 ignored issues
show
introduced by
Documentation comment contains forbidden comment "Http constructor.".
Loading history...
100
     * @param string $host
0 ignored issues
show
introduced by
Expected 1 lines between description and annotations, found 0.
Loading history...
101
     * @param int $port
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
102 68
     * @param string $username
103
     * @param string $password
104 68
     * @param int $authMethod
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
105
     */
106 68
    public function __construct($host, $port, $username, $password, $authMethod = null)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::__construct() does not have native type hint for its parameter $host but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::__construct() does not have native type hint for its parameter $port but it should be possible to add it based on @param annotation "int".
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::__construct() does not have native type hint for its parameter $username but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::__construct() does not have native type hint for its parameter $password but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::__construct() does not have native type hint for its parameter $authMethod but it should be possible to add it based on @param annotation "int".
Loading history...
Coding Style introduced by
Expected 1 blank line before function; 0 found
Loading history...
107 68
    {
108 68
        $this->setHost($host, $port);
109
110
        $this->_username = $username;
111
        $this->_password = $password;
112 68
        if ($authMethod) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $authMethod of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
113
            $this->_authMethod = $authMethod;
114 68
        }
115 68
116
        $this->_settings = new Settings($this);
117
118 68
        $this->setCurler();
119
    }
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line after function; 2 found
Loading history...
120 68
121 68
122
    public function setCurler() : void
0 ignored issues
show
introduced by
There must be no whitespace between closing parenthesis and return type colon.
Loading history...
123
    {
124
        $this->_curler = new CurlerRolling();
125
    }
126
127
    /**
128
     * @param CurlerRolling $curler
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::setDirtyCurler() has useless @param annotation for parameter $curler.
Loading history...
129
     */
130
    public function setDirtyCurler(CurlerRolling $curler) : void
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::setDirtyCurler() does not need documentation comment.
Loading history...
introduced by
There must be no whitespace between closing parenthesis and return type colon.
Loading history...
131
    {
132
        if ($curler instanceof CurlerRolling) {
0 ignored issues
show
introduced by
$curler is always a sub-type of ClickHouseDB\Transport\CurlerRolling.
Loading history...
introduced by
Use early exit to reduce code nesting.
Loading history...
133
            $this->_curler = $curler;
134
        }
135
    }
136
137
    /**
138
     * @return CurlerRolling
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getCurler() has useless @return annotation.
Loading history...
139
     */
140
    public function getCurler(): ?CurlerRolling
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getCurler() does not need documentation comment.
Loading history...
141
    {
142
        return $this->_curler;
143
    }
144
145 68
    /**
146
     * @param string $host
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::setHost() has useless @param annotation for parameter $host.
Loading history...
147 68
     * @param int $port
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::setHost() has useless @param annotation for parameter $port.
Loading history...
148 68
     */
149
    public function setHost(string $host, int $port = -1) : void
0 ignored issues
show
introduced by
There must be no whitespace between closing parenthesis and return type colon.
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::setHost() does not need documentation comment.
Loading history...
150
    {
151 68
        if ($port > 0) {
152 68
            $this->_port = $port;
153
        }
154
155
        $this->_host = $host;
156
    }
157
158
    /**
159
     * Sets client SSL certificate for Yandex Cloud
160
     *
161
     * @param string $caPath
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::setSslCa() has useless @param annotation for parameter $caPath.
Loading history...
162
     */
163
    public function setSslCa(string $caPath) : void
0 ignored issues
show
introduced by
There must be no whitespace between closing parenthesis and return type colon.
Loading history...
164
    {
165
        $this->sslCA = $caPath;
166
    }
167 55
168
    /**
169 55
     * @return string
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getUri() has useless @return annotation.
Loading history...
170 55
     */
171 1
    public function getUri(): string
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getUri() does not need documentation comment.
Loading history...
172
    {
173 55
        $proto = 'http';
174 55
        if ($this->settings()->isHttps()) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
175 1
            $proto = 'https';
176
        }
177 55
        $uri = $proto . '://' . $this->_host;
178 55
        if (stripos($this->_host, '/') !== false || stripos($this->_host, ':') !== false) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
introduced by
Function stripos() should not be referenced via a fallback global name, but via a use statement.
Loading history...
179
            return $uri;
180 1
        }
181
        if (intval($this->_port) > 0) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
introduced by
Function intval() should not be referenced via a fallback global name, but via a use statement.
Loading history...
182
            return $uri . ':' . $this->_port;
183
        }
184
        return $uri;
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
185
    }
186 68
187
    /**
188 68
     * @return Settings
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::settings() has useless @return annotation.
Loading history...
189
     */
190
    public function settings(): Settings
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::settings() does not need documentation comment.
Loading history...
191
    {
192
        return $this->_settings;
193
    }
194
195
    /**
196
     * @param bool $flag
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::verbose() has useless @param annotation for parameter $flag.
Loading history...
197
     * @return bool
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::verbose() has useless @return annotation.
Loading history...
198
     */
199
    public function verbose(bool $flag): bool
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::verbose() does not need documentation comment.
Loading history...
200
    {
201
        $this->_verbose = $flag;
202
        return $flag;
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
203
    }
204
205 45
    /**
206
     * @param array $params
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
introduced by
@param annotation of method \ClickHouseDB\Transport\Http::getUrl() does not specify type hint for items of its traversable parameter $params.
Loading history...
207 45
     * @return string
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getUrl() has useless @return annotation.
Loading history...
208
     */
209 45
    private function getUrl($params = []): string
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getUrl() does not have native type hint for its parameter $params but it should be possible to add it based on @param annotation "array".
Loading history...
210 45
    {
211
        $settings = $this->settings()->getSettings();
212
213
        if (is_array($params) && sizeof($params)) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 2.
Loading history...
introduced by
Function is_array() should not be referenced via a fallback global name, but via a use statement.
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...
214 45
            $settings = array_merge($settings, $params);
0 ignored issues
show
introduced by
Function array_merge() should not be referenced via a fallback global name, but via a use statement.
Loading history...
215
        }
216
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
217
218
        if ($this->settings()->isReadOnlyUser()) {
219
            unset($settings['extremes']);
220
            unset($settings['readonly']);
221
            unset($settings['enable_http_compression']);
222 45
            unset($settings['max_execution_time']);
223
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
224
        }
225 45
226
        unset($settings['https']);
227
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
228
229
        return $this->getUri() . '?' . http_build_query($settings);
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 2.
Loading history...
introduced by
Function http_build_query() should not be referenced via a fallback global name, but via a use statement.
Loading history...
230
    }
231
232 45
    /**
233
     * @param array $extendinfo
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
introduced by
@param annotation of method \ClickHouseDB\Transport\Http::newRequest() does not specify type hint for items of its traversable parameter $extendinfo.
Loading history...
234 45
     * @return CurlerRequest
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::newRequest() has useless @return annotation.
Loading history...
235
     */
236 45
    private function newRequest($extendinfo): CurlerRequest
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::newRequest() does not have native type hint for its parameter $extendinfo but it should be possible to add it based on @param annotation "array".
Loading history...
237 45
    {
238
        $new = new CurlerRequest();
239
240
        switch ($this->_authMethod) {
241
            case self::AUTH_METHOD_QUERY_STRING:
242
                /* @todo: Move this implementation to CurlerRequest class. Possible options: the authentication method
243
                 *        should be applied in method `CurlerRequest:prepareRequest()`.
244 45
                 */
245
                $this->settings()->set('user', $this->_username);
246
                $this->settings()->set('password', $this->_password);
247
                break;
248
            case self::AUTH_METHOD_BASIC_AUTH:
249 45
                $new->authByBasicAuth($this->_username, $this->_password);
250 45
                break;
251
            default:
252
                // Auth with headers by default
253 45
                $new->authByHeaders($this->_username, $this->_password);
254
                break;
255 45
        }
256 45
257
        $new->POST()->setRequestExtendedInfo($extendinfo);
258 45
259 1
        $new->httpCompression($this->settings()->isEnableHttpCompression());
0 ignored issues
show
Bug introduced by
It seems like $this->settings()->isEnableHttpCompression() can also be of type null; however, parameter $flag of ClickHouseDB\Transport\C...uest::httpCompression() does only seem to accept boolean, 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

259
        $new->httpCompression(/** @scrutinizer ignore-type */ $this->settings()->isEnableHttpCompression());
Loading history...
260
261 45
        if ($this->settings()->getSessionId()) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
262
            $new->persistent();
263
        }
264
        if ($this->sslCA) {
265 45
            $new->setSslCa($this->sslCA);
266 45
        }
267 45
268
        $new->timeOut($this->settings()->getTimeOut());
269 45
        $new->connectTimeOut($this->_connectTimeOut);//->keepAlive(); // one sec
270
        $new->verbose(boolval($this->_verbose));
0 ignored issues
show
introduced by
Function boolval() should not be referenced via a fallback global name, but via a use statement.
Loading history...
271
272
        return $new;
273
    }
274
275
    /**
276
     * @param Query $query
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::makeRequest() has useless @param annotation for parameter $query.
Loading history...
277
     * @param array $urlParams
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Transport\Http::makeRequest() does not specify type hint for items of its traversable parameter $urlParams.
Loading history...
278
     * @param bool $query_as_string
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::makeRequest() has useless @param annotation for parameter $query_as_string.
Loading history...
279 45
     * @return CurlerRequest
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::makeRequest() has useless @return annotation.
Loading history...
280
     * @throws \ClickHouseDB\Exception\TransportException
0 ignored issues
show
introduced by
Class \ClickHouseDB\Exception\TransportException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
281 45
     */
282
    private function makeRequest(Query $query, array $urlParams = [], bool $query_as_string = false): CurlerRequest
0 ignored issues
show
Coding Style introduced by
The variable $query_as_string should be in camel caps format.
Loading history...
283 45
    {
284 1
        $sql = $query->toSql();
285
286
        if ($query_as_string) {
0 ignored issues
show
Coding Style introduced by
The variable $query_as_string should be in camel caps format.
Loading history...
287
            $urlParams['query'] = $sql;
288 45
        }
289 45
290 45
        $extendInfo = [
291
            'sql' => $sql,
292
            'query' => $query,
293 45
            'format' => $query->getFormat()
0 ignored issues
show
introduced by
Multi-line arrays must have a trailing comma after the last element.
Loading history...
294
        ];
295
296
        $new = $this->newRequest($extendInfo);
297
298
        /*
299 45
         * Build URL after request making, since URL may contain auth data. This will not matter after the
300 45
         * implantation of the todo in the `HTTP:newRequest()` method.
301
         */
302
        $url = $this->getUrl($urlParams);
303 45
        $new->url($url);
304 45
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
305
306 45
        if (!$query_as_string) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
Coding Style introduced by
The variable $query_as_string should be in camel caps format.
Loading history...
307 45
            $new->parameters_json($sql);
308
        }
309
        $new->httpCompression($this->settings()->isEnableHttpCompression());
0 ignored issues
show
Bug introduced by
It seems like $this->settings()->isEnableHttpCompression() can also be of type null; however, parameter $flag of ClickHouseDB\Transport\C...uest::httpCompression() does only seem to accept boolean, 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

309
        $new->httpCompression(/** @scrutinizer ignore-type */ $this->settings()->isEnableHttpCompression());
Loading history...
310 45
311
        return $new;
312
    }
313
314
    /**
315
     * @param resource $stream
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
316
     * @return void
317 3
     */
318
    public function setStdErrOut($stream)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::setStdErrOut() does not have native return type hint for its return value but it should be possible to add it based on @return annotation "void".
Loading history...
319
    {
320 3
        if (is_resource($stream)) {
0 ignored issues
show
introduced by
Expected 0 lines after "if", found 1.
Loading history...
introduced by
Use early exit to reduce code nesting.
Loading history...
introduced by
Function is_resource() should not be referenced via a fallback global name, but via a use statement.
Loading history...
321 1
            $this->stdErrOut=$stream;
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "="; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "="; 0 found
Loading history...
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces
Loading history...
322
        }
323 2
324
    }
0 ignored issues
show
Coding Style introduced by
Function closing brace must go on the next line following the body; found 1 blank lines before brace
Loading history...
325
326
    /**
327 3
     * @param string|Query $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
328 3
     * @return CurlerRequest
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::writeStreamData() has useless @return annotation.
Loading history...
329 3
     */
330
    public function writeStreamData($sql): CurlerRequest
331
    {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
332 3
333
        if ($sql instanceof Query) {
334
            $query = $sql;
335
        } else {
336
            $query = new Query($sql);
337
        }
338 3
339 3
        $extendInfo = [
340 3
            'sql' => $sql,
341
            'query' => $query,
342
            'format' => $query->getFormat()
0 ignored issues
show
introduced by
Multi-line arrays must have a trailing comma after the last element.
Loading history...
343 3
        ];
344 3
345
        $request = $this->newRequest($extendInfo);
346
347
        /*
348
         * Build URL after request making, since URL may contain auth data. This will not matter after the
349
         * implantation of the todo in the `HTTP:newRequest()` method.
350
         */
351
        $url = $this->getUrl([
352
            'readonly' => 0,
353
            'query' => $query->toSql()
0 ignored issues
show
introduced by
Multi-line arrays must have a trailing comma after the last element.
Loading history...
354 8
        ]);
355
356 8
        $request->url($url);
357
        return $request;
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
358
    }
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line after function; 2 found
Loading history...
359 8
360 8
361 8
    /**
362
     * @param string $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
363
     * @param string $file_name
364 8
     * @return Statement
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::writeAsyncCSV() has useless @return annotation.
Loading history...
365
     * @throws \ClickHouseDB\Exception\TransportException
0 ignored issues
show
introduced by
Class \ClickHouseDB\Exception\TransportException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
366
     */
367
    public function writeAsyncCSV($sql, $file_name): Statement
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::writeAsyncCSV() does not have native type hint for its parameter $sql but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::writeAsyncCSV() does not have native type hint for its parameter $file_name but it should be possible to add it based on @param annotation "string".
Loading history...
Coding Style introduced by
The variable $file_name should be in camel caps format.
Loading history...
368
    {
369
        $query = new Query($sql);
370 8
371 8
        $extendinfo = [
372 8
            'sql' => $sql,
373
            'query' => $query,
374
            'format' => $query->getFormat()
0 ignored issues
show
introduced by
Multi-line arrays must have a trailing comma after the last element.
Loading history...
375 8
        ];
376
377 8
        $request = $this->newRequest($extendinfo);
378 8
379 8
        /*
380 8
         * Build URL after request making, since URL may contain auth data. This will not matter after the
381
         * implantation of the todo in the `HTTP:newRequest()` method.
382 8
         */
383
        $url = $this->getUrl([
384 8
            'readonly' => 0,
385 8
            'query' => $query->toSql()
0 ignored issues
show
introduced by
Multi-line arrays must have a trailing comma after the last element.
Loading history...
386
        ]);
387 8
388
        $request->url($url);
389
390
        $request->setCallbackFunction(function (CurlerRequest $request) {
0 ignored issues
show
introduced by
Closure not using "$this" should be declared static.
Loading history...
introduced by
Closure does not have void return type hint.
Loading history...
391
            $handle = $request->getInfileHandle();
392
            if (is_resource($handle)) {
0 ignored issues
show
introduced by
Function is_resource() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Use early exit to reduce code nesting.
Loading history...
393
                fclose($handle);
0 ignored issues
show
introduced by
Function fclose() should not be referenced via a fallback global name, but via a use statement.
Loading history...
394
            }
395 12
        });
396
397 12
        $request->setInfile($file_name);
0 ignored issues
show
Coding Style introduced by
The variable $file_name should be in camel caps format.
Loading history...
398
        $this->_curler->addQueLoop($request);
399
400
        return new Statement($request);
401
    }
402
403
    /**
404
     * get Count Pending Query in Queue
405 2
     *
406
     * @return int
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getCountPendingQueue() has useless @return annotation.
Loading history...
407 2
     */
408 2
    public function getCountPendingQueue(): int
409
    {
410
        return $this->_curler->countPending();
411
    }
412
413
    /**
414
     * set Connect TimeOut in seconds [CURLOPT_CONNECTTIMEOUT] ( int )
415 38
     *
416
     * @param int $connectTimeOut
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::setConnectTimeOut() has useless @param annotation for parameter $connectTimeOut.
Loading history...
417 38
     */
418
    public function setConnectTimeOut(int $connectTimeOut)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::setConnectTimeOut() does not have void return type hint.
Loading history...
419
    {
420
        $this->_connectTimeOut = $connectTimeOut;
421 1
    }
422
423 1
    /**
424
     * get ConnectTimeOut in seconds
425
     *
426 1
     * @return int
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getConnectTimeOut() has useless @return annotation.
Loading history...
427 1
     */
428 1
    public function getConnectTimeOut(): int
429 1
    {
430
        return $this->_connectTimeOut;
431
    }
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line after function; 2 found
Loading history...
432
433 1
434 1
    public function __findXClickHouseProgress($handle): bool
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::__findXClickHouseProgress() does not have parameter type hint nor @param annotation for its parameter $handle.
Loading history...
435
    {
436
        $code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
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_HTTP_CODE should not be referenced via a fallback global name, but via a use statement.
Loading history...
437
438 1
        // Search X-ClickHouse-Progress
439 1
        if ($code == 200) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
introduced by
Operator == is disallowed, use === instead.
Loading history...
440
            $response = curl_multi_getcontent($handle);
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...
441
            $header_size = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
0 ignored issues
show
Coding Style introduced by
The variable $header_size should be in camel caps format.
Loading history...
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...
442
            if (!$header_size) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
Coding Style introduced by
The variable $header_size should be in camel caps format.
Loading history...
443 1
                return false;
444 1
            }
445
446 1
            $header = substr($response, 0, $header_size);
0 ignored issues
show
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...
447
            if (!$header_size) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
Coding Style introduced by
The variable $header_size should be in camel caps format.
Loading history...
448 1
                return false;
449
            }
450
451 1
            $pos = strrpos($header, 'X-ClickHouse-Summary:');
0 ignored issues
show
introduced by
Function strrpos() should not be referenced via a fallback global name, but via a use statement.
Loading history...
452
            if (!$pos) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
453
                return false;
454
            }
455
456
            $last = substr($header, $pos);
0 ignored issues
show
introduced by
Function substr() should not be referenced via a fallback global name, but via a use statement.
Loading history...
457
            $data = @json_decode(str_ireplace('X-ClickHouse-Summary:', '', $last), true);
0 ignored issues
show
Bug introduced by
It seems like str_ireplace('X-ClickHouse-Summary:', '', $last) can also be of type array; however, parameter $json of json_decode() does only seem to accept string, 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

457
            $data = @json_decode(/** @scrutinizer ignore-type */ str_ireplace('X-ClickHouse-Summary:', '', $last), true);
Loading history...
introduced by
Function json_decode() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Function str_ireplace() should not be referenced via a fallback global name, but via a use statement.
Loading history...
458 1
459
            if ($data && is_callable($this->xClickHouseProgress)) {
0 ignored issues
show
introduced by
Expected 0 lines after "if", found 1.
Loading history...
introduced by
Function is_callable() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Coding Style introduced by
Blank line found at start of control structure
Loading history...
460
461
                if (is_array($this->xClickHouseProgress)) {
0 ignored issues
show
introduced by
Expected 0 lines after "if", found 2.
Loading history...
introduced by
Function is_array() should not be referenced via a fallback global name, but via a use statement.
Loading history...
462
                    call_user_func_array($this->xClickHouseProgress, [$data]);
0 ignored issues
show
introduced by
Function call_user_func_array() should not be referenced via a fallback global name, but via a use statement.
Loading history...
463
                } else {
464
                    call_user_func($this->xClickHouseProgress, $data);
0 ignored issues
show
introduced by
Function call_user_func() should not be referenced via a fallback global name, but via a use statement.
Loading history...
465
                }
466
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
467
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
468 40
            }
469
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
470 40
        }
471 40
        return false;
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
472
    }
473 40
474
    /**
475 1
     * @param Query $query
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::getRequestRead() has useless @param annotation for parameter $query.
Loading history...
Coding Style introduced by
Expected 12 spaces after parameter type; 1 found
Loading history...
476
     * @param null|WhereInFile $whereInFile
0 ignored issues
show
introduced by
Null type hint should be on last position in "null|WhereInFile".
Loading history...
477 1
     * @param null|WriteToFile $writeToFile
0 ignored issues
show
introduced by
Null type hint should be on last position in "null|WriteToFile".
Loading history...
478 1
     * @return CurlerRequest
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getRequestRead() has useless @return annotation.
Loading history...
479
     * @throws \Exception
0 ignored issues
show
introduced by
Class \Exception should not be referenced via a fully qualified name, but via a use statement.
Loading history...
480
     */
481
    public function getRequestRead(Query $query, $whereInFile = null, $writeToFile = null): CurlerRequest
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getRequestRead() does not have native type hint for its parameter $whereInFile but it should be possible to add it based on @param annotation "null|WhereInFile".
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::getRequestRead() does not have native type hint for its parameter $writeToFile but it should be possible to add it based on @param annotation "null|WriteToFile".
Loading history...
482 40
    {
483 1
        $urlParams = ['readonly' => 2];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 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...
484 1
        $query_as_string = false;
0 ignored issues
show
Coding Style introduced by
The variable $query_as_string should be in camel caps format.
Loading history...
485
        // ---------------------------------------------------------------------------------
486
        if ($whereInFile instanceof WhereInFile && $whereInFile->size()) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
487
            // $request = $this->prepareSelectWhereIn($request, $whereInFile);
488 40
            $structure = $whereInFile->fetchUrlParams();
489
            // $structure = [];
490
            $urlParams = array_merge($urlParams, $structure);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 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 array_merge() should not be referenced via a fallback global name, but via a use statement.
Loading history...
491 40
            $query_as_string = true;
0 ignored issues
show
Coding Style introduced by
The variable $query_as_string should be in camel caps format.
Loading history...
492 1
        }
493
        // ---------------------------------------------------------------------------------
494
        // if result to file
495
        if ($writeToFile instanceof WriteToFile && $writeToFile->fetchFormat()) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
496 40
            $query->setFormat($writeToFile->fetchFormat());
497
            unset($urlParams['extremes']);
498 1
        }
499 1
        // ---------------------------------------------------------------------------------
500
        // makeRequest read
501 1
        $request = $this->makeRequest($query, $urlParams, $query_as_string);
0 ignored issues
show
Coding Style introduced by
The variable $query_as_string should be in camel caps format.
Loading history...
502
        // ---------------------------------------------------------------------------------
503 1
        // attach files
504
        if ($whereInFile instanceof WhereInFile && $whereInFile->size()) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
505
            $request->attachFiles($whereInFile->fetchFiles());
506
        }
507
        // ---------------------------------------------------------------------------------
508
        // result to file
509
        if ($writeToFile instanceof WriteToFile && $writeToFile->fetchFormat()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
510
511
            $fout = fopen($writeToFile->fetchFile(), 'w');
0 ignored issues
show
introduced by
Function fopen() should not be referenced via a fallback global name, but via a use statement.
Loading history...
512
            if (is_resource($fout)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
introduced by
Function is_resource() should not be referenced via a fallback global name, but via a use statement.
Loading history...
513
514
                $isGz = $writeToFile->getGzip();
515
516 1
                if ($isGz) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 2.
Loading history...
517
                    // write gzip header
518 1
                    // "\x1f\x8b\x08\x00\x00\x00\x00\x00"
519
                    // fwrite($fout, "\x1F\x8B\x08\x08".pack("V", time())."\0\xFF", 10);
520
                    // write the original file name
521 40
                    // $oname = str_replace("\0", "", basename($writeToFile->fetchFile()));
522 1
                    // fwrite($fout, $oname."\0", 1+strlen($oname));
523
524
                    fwrite($fout, "\x1f\x8b\x08\x00\x00\x00\x00\x00");
0 ignored issues
show
introduced by
Function fwrite() should not be referenced via a fallback global name, but via a use statement.
Loading history...
525 40
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
526
                }
527
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
528
529 1
                $request->setResultFileHandle($fout, $isGz)->setCallbackFunction(function (CurlerRequest $request) {
0 ignored issues
show
introduced by
Closure not using "$this" should be declared static.
Loading history...
introduced by
Closure does not have void return type hint.
Loading history...
530
                    fclose($request->getResultFileHandle());
0 ignored issues
show
introduced by
Function fclose() should not be referenced via a fallback global name, but via a use statement.
Loading history...
531 1
                });
532 1
            }
533
        }
534
535 68
        if ($this->stdErrOut) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
536
            $request->setStdErrOut($this->stdErrOut);
537 68
        }
538 68
        if ($this->xClickHouseProgress) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
539
            $request->setFunctionProgress([$this, '__findXClickHouseProgress']);
540
        }
541
        // ---------------------------------------------------------------------------------
542
        return $request;
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
introduced by
Expected 0 lines after "return", found 1.
Loading history...
543
544
    }
0 ignored issues
show
Coding Style introduced by
Function closing brace must go on the next line following the body; found 1 blank lines before brace
Loading history...
545
546 27
    public function cleanQueryDegeneration(): bool
547
    {
548 27
        $this->_query_degenerations = [];
0 ignored issues
show
Coding Style introduced by
Member variable "_query_degenerations" is not in valid camel caps format
Loading history...
549 27
        return true;
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
550
    }
551
552
    public function addQueryDegeneration(Degeneration $degeneration): bool
553
    {
554
        $this->_query_degenerations[] = $degeneration;
0 ignored issues
show
Coding Style introduced by
Member variable "_query_degenerations" is not in valid camel caps format
Loading history...
555 38
        return true;
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
556
    }
557 38
558 38
    /**
559 38
     * @param Query $query
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::getRequestWrite() has useless @param annotation for parameter $query.
Loading history...
560
     * @return CurlerRequest
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getRequestWrite() has useless @return annotation.
Loading history...
561 38
     * @throws \ClickHouseDB\Exception\TransportException
0 ignored issues
show
introduced by
Class \ClickHouseDB\Exception\TransportException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
562
     */
563
    public function getRequestWrite(Query $query): CurlerRequest
564
    {
565
        $urlParams = ['readonly' => 0];
566
        return $this->makeRequest($query, $urlParams);
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
567
    }
568
569 46
    /**
570
     * @throws TransportException
571
     */
572
    public function ping(): bool
573 46
    {
574 46
        $request = new CurlerRequest();
575
        $request->url($this->getUri())->verbose(false)->GET()->connectTimeOut($this->getConnectTimeOut());
576
        $this->_curler->execOne($request);
577 46
578
        return $request->response()->body() === 'Ok.' . PHP_EOL;
579
    }
580
581
    /**
582
     * @param string $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
583
     * @param mixed[] $bindings
584
     * @return Query
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::prepareQuery() has useless @return annotation.
Loading history...
585
     */
586
    private function prepareQuery($sql, $bindings): Query
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::prepareQuery() does not have native type hint for its parameter $sql but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::prepareQuery() does not have native type hint for its parameter $bindings but it should be possible to add it based on @param annotation "mixed[]".
Loading history...
587
    {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
588
589 39
        // add Degeneration query
590
        foreach ($this->_query_degenerations as $degeneration) {
0 ignored issues
show
Coding Style introduced by
Member variable "_query_degenerations" is not in valid camel caps format
Loading history...
591 39
            $degeneration->bindParams($bindings);
592
        }
593
594 39
        return new Query($sql, $this->_query_degenerations);
0 ignored issues
show
Coding Style introduced by
Member variable "_query_degenerations" is not in valid camel caps format
Loading history...
595 39
    }
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line after function; 2 found
Loading history...
596 39
597
598
    /**
599
     * @param Query|string $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
600
     * @param mixed[] $bindings
0 ignored issues
show
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
601
     * @param null|WhereInFile $whereInFile
0 ignored issues
show
introduced by
Null type hint should be on last position in "null|WhereInFile".
Loading history...
602
     * @param null|WriteToFile $writeToFile
0 ignored issues
show
introduced by
Null type hint should be on last position in "null|WriteToFile".
Loading history...
603
     * @return CurlerRequest
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::prepareSelect() has useless @return annotation.
Loading history...
604
     * @throws \Exception
0 ignored issues
show
introduced by
Class \Exception should not be referenced via a fully qualified name, but via a use statement.
Loading history...
605
     */
606 28
    private function prepareSelect($sql, $bindings, $whereInFile, $writeToFile = null): CurlerRequest
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::prepareSelect() does not have native type hint for its parameter $bindings but it should be possible to add it based on @param annotation "mixed[]".
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::prepareSelect() does not have native type hint for its parameter $whereInFile but it should be possible to add it based on @param annotation "null|WhereInFile".
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::prepareSelect() does not have native type hint for its parameter $writeToFile but it should be possible to add it based on @param annotation "null|WriteToFile".
Loading history...
607
    {
608 28
        if ($sql instanceof Query) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
609
            return $this->getRequestWrite($sql);
610
        }
611
        $query = $this->prepareQuery($sql, $bindings);
612 28
        $query->setFormat('JSON');
613 27
        return $this->getRequestRead($query, $whereInFile, $writeToFile);
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
614
    }
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line after function; 2 found
Loading history...
615
616
617
    /**
618
     * @param Query|string $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
619
     * @param mixed[] $bindings
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
620 10
     * @return CurlerRequest
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::prepareWrite() has useless @return annotation.
Loading history...
621
     * @throws \ClickHouseDB\Exception\TransportException
0 ignored issues
show
introduced by
Class \ClickHouseDB\Exception\TransportException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
622 10
     */
623
    private function prepareWrite($sql, $bindings = []): CurlerRequest
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::prepareWrite() does not have native type hint for its parameter $bindings but it should be possible to add it based on @param annotation "mixed[]".
Loading history...
624
    {
625
        if ($sql instanceof Query) {
626
            return $this->getRequestWrite($sql);
627
        }
628
629
        $query = $this->prepareQuery($sql, $bindings);
630
        return $this->getRequestWrite($query);
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
631
    }
632
633
    /**
634 32
     * @return bool
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::executeAsync() has useless @return annotation.
Loading history...
635
     * @throws \ClickHouseDB\Exception\TransportException
0 ignored issues
show
introduced by
Class \ClickHouseDB\Exception\TransportException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
636 32
     */
637 32
    public function executeAsync(): bool
638 32
    {
639
        return $this->_curler->execLoopWait();
640
    }
641
642
    /**
643
     * @param Query|string $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
644
     * @param mixed[] $bindings
0 ignored issues
show
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
645
     * @param null|WhereInFile $whereInFile
0 ignored issues
show
introduced by
Null type hint should be on last position in "null|WhereInFile".
Loading history...
646
     * @param null|WriteToFile $writeToFile
0 ignored issues
show
introduced by
Null type hint should be on last position in "null|WriteToFile".
Loading history...
647
     * @return Statement
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::select() has useless @return annotation.
Loading history...
648
     * @throws \ClickHouseDB\Exception\TransportException
0 ignored issues
show
introduced by
Class \ClickHouseDB\Exception\TransportException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
649
     * @throws \Exception
0 ignored issues
show
introduced by
Class \Exception should not be referenced via a fully qualified name, but via a use statement.
Loading history...
650 7
     */
651
    public function select($sql, array $bindings = [], $whereInFile = null, $writeToFile = null): Statement
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::select() does not have native type hint for its parameter $whereInFile but it should be possible to add it based on @param annotation "null|WhereInFile".
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::select() does not have native type hint for its parameter $writeToFile but it should be possible to add it based on @param annotation "null|WriteToFile".
Loading history...
652 7
    {
653 7
        $request = $this->prepareSelect($sql, $bindings, $whereInFile, $writeToFile);
654 7
        $this->_curler->execOne($request);
655
        return new Statement($request);
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
656
    }
657
658
    /**
659
     * @param Query|string $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
660 1
     * @param mixed[] $bindings
0 ignored issues
show
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
661
     * @param null|WhereInFile $whereInFile
0 ignored issues
show
introduced by
Null type hint should be on last position in "null|WhereInFile".
Loading history...
662 1
     * @param null|WriteToFile $writeToFile
0 ignored issues
show
introduced by
Null type hint should be on last position in "null|WriteToFile".
Loading history...
663 1
     * @return Statement
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::selectAsync() has useless @return annotation.
Loading history...
664
     * @throws \ClickHouseDB\Exception\TransportException
0 ignored issues
show
introduced by
Class \ClickHouseDB\Exception\TransportException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
665
     * @throws \Exception
0 ignored issues
show
introduced by
Class \Exception should not be referenced via a fully qualified name, but via a use statement.
Loading history...
666
     */
667
    public function selectAsync($sql, array $bindings = [], $whereInFile = null, $writeToFile = null): Statement
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::selectAsync() does not have native type hint for its parameter $whereInFile but it should be possible to add it based on @param annotation "null|WhereInFile".
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::selectAsync() does not have native type hint for its parameter $writeToFile but it should be possible to add it based on @param annotation "null|WriteToFile".
Loading history...
668
    {
669
        $request = $this->prepareSelect($sql, $bindings, $whereInFile, $writeToFile);
670
        $this->_curler->addQueLoop($request);
671
        return new Statement($request);
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
672 28
    }
673
674 28
    /**
675 27
     * @param callable $callback
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::setProgressFunction() has useless @param annotation for parameter $callback.
Loading history...
676 27
     */
677 27
    public function setProgressFunction(callable $callback) : void
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::setProgressFunction() does not need documentation comment.
Loading history...
introduced by
There must be no whitespace between closing parenthesis and return type colon.
Loading history...
678 27
    {
679 3
        $this->xClickHouseProgress = $callback;
680
    }
681
682 25
    /**
683
     * @param string $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
684
     * @param mixed[] $bindings
685
     * @param bool $exception
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
686
     * @return Statement
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::write() has useless @return annotation.
Loading history...
687
     * @throws \ClickHouseDB\Exception\TransportException
0 ignored issues
show
introduced by
Class \ClickHouseDB\Exception\TransportException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
688
     */
689
    public function write($sql, array $bindings = [], $exception = true): Statement
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::write() does not have native type hint for its parameter $sql but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::write() does not have native type hint for its parameter $exception but it should be possible to add it based on @param annotation "bool".
Loading history...
690
    {
691 2
        $request = $this->prepareWrite($sql, $bindings);
692
        $this->_curler->execOne($request);
693 2
        $response = new Statement($request);
694 2
        if ($exception) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
695
            if ($response->isError()) {
696
                $response->error();
697
            }
698
        }
699
        return $response;
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
700 2
    }
701
702
    /**
703
     * @param Stream $streamRW
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::streaming() has useless @param annotation for parameter $streamRW.
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
704
     * @param CurlerRequest $request
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::streaming() has useless @param annotation for parameter $request.
Loading history...
705
     * @return Statement
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::streaming() has useless @return annotation.
Loading history...
706
     * @throws \ClickHouseDB\Exception\TransportException
0 ignored issues
show
introduced by
Class \ClickHouseDB\Exception\TransportException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
707
     */
708
    private function streaming(Stream $streamRW, CurlerRequest $request): Statement
709
    {
710
        $callable = $streamRW->getClosure();
711
        $stream = $streamRW->getStream();
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...
712
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
713 2
714
        try {
0 ignored issues
show
introduced by
Expected 0 lines after "try", found 2.
Loading history...
Coding Style introduced by
Blank line found at start of control structure
Loading history...
715 1
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
716 1
717 1
            if (!is_callable($callable)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
introduced by
Function is_callable() should not be referenced via a fallback global name, but via a use statement.
Loading history...
718
                if ($streamRW->isWrite()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
719
720
                    $callable = function ($ch, $fd, $length) use ($stream) {
0 ignored issues
show
introduced by
Closure not using "$this" should be declared static.
Loading history...
721
                        return ($line = fread($stream, $length)) ? $line : '';
0 ignored issues
show
introduced by
Function fread() should not be referenced via a fallback global name, but via a use statement.
Loading history...
722
                    };
723
                } else {
724
                    $callable = function ($ch, $fd) use ($stream) {
0 ignored issues
show
introduced by
Closure not using "$this" should be declared static.
Loading history...
725 2
                        return fwrite($stream, $fd);
0 ignored issues
show
introduced by
Function fwrite() should not be referenced via a fallback global name, but via a use statement.
Loading history...
726
                    };
727
                }
728 2
            }
729 1
730
            if ($streamRW->isGzipHeader()) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 2.
Loading history...
Coding Style introduced by
Blank line found at start of control structure
Loading history...
731 1
732
                if ($streamRW->isWrite()) {
0 ignored issues
show
introduced by
Expected 0 lines after "if", found 1.
Loading history...
733
                    $request->header('Content-Encoding', 'gzip');
734
                    $request->header('Content-Type', 'application/x-www-form-urlencoded');
735
                } else {
736
                    $request->header('Accept-Encoding', 'gzip');
737
                }
738 2
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
739 2
            }
740 2
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
741
742
            $request->header('Transfer-Encoding', 'chunked');
743 2
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
744
745 2
            if ($streamRW->isWrite()) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 2.
Loading history...
746 2
                $request->setReadFunction($callable);
747
            } else {
748
                $request->setWriteFunction($callable);
749
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
750
751
//                $request->setHeaderFunction($callableHead);
752
            }
753
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
754
755
            $this->_curler->execOne($request, true);
756
            $response = new Statement($request);
757
            if ($response->isError()) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
758
                $response->error();
759
            }
760 1
            return $response;
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
761
        } finally {
762 1
            if ($streamRW->isWrite())
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...
763 1
                fclose($stream);
0 ignored issues
show
introduced by
Function fclose() should not be referenced via a fallback global name, but via a use statement.
Loading history...
764 1
        }
765
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
766
767
    }
0 ignored issues
show
Coding Style introduced by
Function closing brace must go on the next line following the body; found 2 blank lines before brace
Loading history...
Coding Style introduced by
Expected 1 blank line after function; 2 found
Loading history...
768
769
770
    /**
771
     * @param Stream $streamRead
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::streamRead() has useless @param annotation for parameter $streamRead.
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
772
     * @param string $sql
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
773
     * @param mixed[] $bindings
774
     * @return Statement
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::streamRead() has useless @return annotation.
Loading history...
775 1
     * @throws \ClickHouseDB\Exception\TransportException
0 ignored issues
show
introduced by
Class \ClickHouseDB\Exception\TransportException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
776
     */
777 1
    public function streamRead(Stream $streamRead, $sql, $bindings = []): Statement
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::streamRead() does not have native type hint for its parameter $sql but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::streamRead() does not have native type hint for its parameter $bindings but it should be possible to add it based on @param annotation "mixed[]".
Loading history...
778 1
    {
779 1
        $sql = $this->prepareQuery($sql, $bindings);
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...
780
        $request = $this->getRequestRead($sql);
781
        return $this->streaming($streamRead, $request);
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
introduced by
Expected 0 lines after "return", found 1.
Loading history...
782
783
    }
0 ignored issues
show
Coding Style introduced by
Function closing brace must go on the next line following the body; found 1 blank lines before brace
Loading history...
784
785
    /**
786
     * @param Stream $streamWrite
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::streamWrite() has useless @param annotation for parameter $streamWrite.
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
introduced by
Incorrect annotations group.
Loading history...
787
     * @param string $sql
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
788
     * @param mixed[] $bindings
789
     * @return Statement
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::streamWrite() has useless @return annotation.
Loading history...
790
     * @throws \ClickHouseDB\Exception\TransportException
0 ignored issues
show
introduced by
Class \ClickHouseDB\Exception\TransportException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
791
     */
792
    public function streamWrite(Stream $streamWrite, $sql, $bindings = []): Statement
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::streamWrite() does not have native type hint for its parameter $sql but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::streamWrite() does not have native type hint for its parameter $bindings but it should be possible to add it based on @param annotation "mixed[]".
Loading history...
793
    {
794
        $sql = $this->prepareQuery($sql, $bindings);
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...
795
        $request = $this->writeStreamData($sql);
796
        return $this->streaming($streamWrite, $request);
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
797
    }
798
}
799