Failed Conditions
Pull Request — master (#220)
by
unknown
09:19
created

Http::getRequestWrite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 2
b 0
f 1
nc 1
nop 1
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
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...
introduced by
Type PHP_EOL is not used in this file.
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 float
81
     */
82
    private $_connectTimeOut = 5.0;
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
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\Http::$handle with single line content, use one-line comment instead.
Loading history...
100
     * @var null|resource
0 ignored issues
show
introduced by
Null type hint should be on last position in "null|resource".
Loading history...
101
     */
102 68
    private $handle = null;
103
104 68
    /**
105
     * Http constructor.
0 ignored issues
show
introduced by
Documentation comment contains forbidden comment "Http constructor.".
Loading history...
106 68
     * @param string $host
0 ignored issues
show
introduced by
Expected 1 lines between description and annotations, found 0.
Loading history...
107 68
     * @param int $port
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
108 68
     * @param string $username
109
     * @param string $password
110
     * @param int $authMethod
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
111
     */
112 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...
113
    {
114 68
        $this->setHost($host, $port);
115 68
116
        $this->_username = $username;
117
        $this->_password = $password;
118 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...
119
            $this->_authMethod = $authMethod;
120 68
        }
121 68
122
        $this->_settings = new Settings();
123
124
        $this->setCurler();
125
    }
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line after function; 2 found
Loading history...
126
127
128
    public function setCurler() : void
0 ignored issues
show
introduced by
There must be no whitespace between closing parenthesis and return type colon.
Loading history...
129
    {
130
        $this->_curler = new CurlerRolling();
131
    }
132
133
    /**
134
     * @param CurlerRolling $curler
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::setDirtyCurler() has useless @param annotation for parameter $curler.
Loading history...
135
     */
136
    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...
137
    {
138
        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...
139
            $this->_curler = $curler;
140
        }
141
    }
142
143
    /**
144
     * @return CurlerRolling
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getCurler() has useless @return annotation.
Loading history...
145 68
     */
146
    public function getCurler(): ?CurlerRolling
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getCurler() does not need documentation comment.
Loading history...
147 68
    {
148 68
        return $this->_curler;
149
    }
150
151 68
    /**
152 68
     * @param string $host
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::setHost() has useless @param annotation for parameter $host.
Loading history...
153
     * @param int $port
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::setHost() has useless @param annotation for parameter $port.
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
154
     */
155
    public function setHost(string $host, int $port = -1) : void
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::setHost() does not need documentation comment.
Loading history...
introduced by
There must be no whitespace between closing parenthesis and return type colon.
Loading history...
156
    {
157
        if ($port > 0) {
158
            $this->_port = $port;
159
        }
160
161
        $this->_host = $host;
162
    }
163
164
    /**
165
     * Sets client SSL certificate for Yandex Cloud
166
     *
167 55
     * @param string $caPath
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::setSslCa() has useless @param annotation for parameter $caPath.
Loading history...
168
     */
169 55
    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...
170 55
    {
171 1
        $this->sslCA = $caPath;
172
    }
173 55
174 55
    /**
175 1
     * @return string
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getUri() has useless @return annotation.
Loading history...
176
     */
177 55
    public function getUri(): string
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getUri() does not need documentation comment.
Loading history...
178 55
    {
179
        $proto = 'http';
180 1
        if ($this->settings()->isHttps()) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
181
            $proto = 'https';
182
        }
183
        $uri = $proto . '://' . $this->_host;
184
        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...
185
            return $uri;
186 68
        }
187
        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...
188 68
            return $uri . ':' . $this->_port;
189
        }
190
        return $uri;
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
191
    }
192
193
    /**
194
     * @return Settings
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::settings() has useless @return annotation.
Loading history...
195
     */
196
    public function settings(): Settings
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::settings() does not need documentation comment.
Loading history...
197
    {
198
        return $this->_settings;
199
    }
200
201
    /**
202
     * @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...
203
     * @return bool
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::verbose() has useless @return annotation.
Loading history...
204
     */
205 45
    public function verbose(bool $flag): bool
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::verbose() does not need documentation comment.
Loading history...
206
    {
207 45
        $this->_verbose = $flag;
208
        return $flag;
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
209 45
    }
210 45
211
    /**
212
     * @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...
213
     * @return string
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getUrl() has useless @return annotation.
Loading history...
214 45
     */
215
    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...
216
    {
217
        $settings = $this->settings()->getSettings();
218
219
        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...
220
            $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...
221
        }
222 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...
223
224
        if ($this->settings()->isReadOnlyUser()) {
225 45
            unset($settings['extremes']);
226
            unset($settings['readonly']);
227
            unset($settings['enable_http_compression']);
228
            unset($settings['max_execution_time']);
229
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
230
        }
231
232 45
        unset($settings['https']);
233
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
234 45
235
        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...
236 45
    }
237 45
238
    /**
239
     * @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...
240
     * @return CurlerRequest
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::newRequest() has useless @return annotation.
Loading history...
241
     */
242
    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...
243
    {
244 45
        $new = new CurlerRequest(false, $this->getHandle());
245
246
        switch ($this->_authMethod) {
247
            case self::AUTH_METHOD_QUERY_STRING:
248
                /* @todo: Move this implementation to CurlerRequest class. Possible options: the authentication method
249 45
                 *        should be applied in method `CurlerRequest:prepareRequest()`.
250 45
                 */
251
                $this->settings()->set('user', $this->_username);
252
                $this->settings()->set('password', $this->_password);
253 45
                break;
254
            case self::AUTH_METHOD_BASIC_AUTH:
255 45
                $new->authByBasicAuth($this->_username, $this->_password);
256 45
                break;
257
            default:
258 45
                // Auth with headers by default
259 1
                $new->authByHeaders($this->_username, $this->_password);
260
                break;
261 45
        }
262
263
        $new->POST()->setRequestExtendedInfo($extendinfo);
264
265 45
        $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

265
        $new->httpCompression(/** @scrutinizer ignore-type */ $this->settings()->isEnableHttpCompression());
Loading history...
266 45
267 45
        if ($this->settings()->getSessionId()) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
268
            $new->persistent();
269 45
        }
270
        if ($this->sslCA) {
271
            $new->setSslCa($this->sslCA);
272
        }
273
274
        $new->timeOut($this->settings()->getTimeOut());
275
        $new->connectTimeOut($this->_connectTimeOut);
276
        $new->keepAlive();
277
        $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...
278
279 45
        return $new;
280
    }
281 45
282
    /**
283 45
     * @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...
284 1
     * @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...
285
     * @param bool $query_as_string
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::makeRequest() has useless @param annotation for parameter $query_as_string.
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
286
     * @return CurlerRequest
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::makeRequest() has useless @return annotation.
Loading history...
287
     * @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...
288 45
     */
289 45
    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...
290 45
    {
291
        $sql = $query->toSql();
292
293 45
        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...
294
            $urlParams['query'] = $sql;
295
        }
296
297
        $extendInfo = [
298
            'sql' => $sql,
299 45
            'query' => $query,
300 45
            'format' => $query->getFormat()
0 ignored issues
show
introduced by
Multi-line arrays must have a trailing comma after the last element.
Loading history...
301
        ];
302
303 45
        $new = $this->newRequest($extendInfo);
304 45
305
        /*
306 45
         * Build URL after request making, since URL may contain auth data. This will not matter after the
307 45
         * implantation of the todo in the `HTTP:newRequest()` method.
308
         */
309
310 45
        if ($query->isUseInUrlBindingsParams()) {
311
            $urlParams = array_replace_recursive($urlParams, $query->getUrlBindingsParams());
0 ignored issues
show
introduced by
Function array_replace_recursive() should not be referenced via a fallback global name, but via a use statement.
Loading history...
312
        }
313
314
        $url = $this->getUrl($urlParams);
315
        $new->url($url);
316
317 3
        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 after NOT operator; 0 found
Loading history...
Coding Style introduced by
The variable $query_as_string should be in camel caps format.
Loading history...
318
            $new->parameters_json($sql);
319
        }
320 3
        $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

320
        $new->httpCompression(/** @scrutinizer ignore-type */ $this->settings()->isEnableHttpCompression());
Loading history...
321 1
322
        return $new;
323 2
    }
324
325
    /**
326
     * @param resource $stream
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
327 3
     * @return void
328 3
     */
329 3
    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...
330
    {
331
        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...
332 3
            $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...
333
        }
334
335
    }
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...
336
337
    /**
338 3
     * @param string|Query $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
339 3
     * @return CurlerRequest
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::writeStreamData() has useless @return annotation.
Loading history...
340 3
     */
341
    public function writeStreamData($sql): CurlerRequest
342
    {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
343 3
344 3
        if ($sql instanceof Query) {
345
            $query = $sql;
346
        } else {
347
            $query = new Query($sql);
348
        }
349
350
        $extendInfo = [
351
            'sql' => $sql,
352
            'query' => $query,
353
            'format' => $query->getFormat()
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 = $this->newRequest($extendInfo);
357
358
        /*
359 8
         * Build URL after request making, since URL may contain auth data. This will not matter after the
360 8
         * implantation of the todo in the `HTTP:newRequest()` method.
361 8
         */
362
        $url = $this->getUrl([
363
            'readonly' => 0,
364 8
            'query' => $query->toSql()
0 ignored issues
show
introduced by
Multi-line arrays must have a trailing comma after the last element.
Loading history...
365
        ]);
366
367
        $request->url($url);
368
        return $request;
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
369
    }
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line after function; 2 found
Loading history...
370 8
371 8
372 8
    /**
373
     * @param string $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
374
     * @param string $file_name
375 8
     * @return Statement
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::writeAsyncCSV() has useless @return annotation.
Loading history...
376
     * @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...
377 8
     */
378 8
    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...
379 8
    {
380 8
        $query = new Query($sql);
381
382 8
        $extendinfo = [
383
            'sql' => $sql,
384 8
            'query' => $query,
385 8
            'format' => $query->getFormat()
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 = $this->newRequest($extendinfo);
389
390
        /*
391
         * Build URL after request making, since URL may contain auth data. This will not matter after the
392
         * implantation of the todo in the `HTTP:newRequest()` method.
393
         */
394
        $url = $this->getUrl([
395 12
            'readonly' => 0,
396
            'query' => $query->toSql()
0 ignored issues
show
introduced by
Multi-line arrays must have a trailing comma after the last element.
Loading history...
397 12
        ]);
398
399
        $request->url($url);
400
401
        $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...
402
            $handle = $request->getInfileHandle();
403
            if (is_resource($handle)) {
0 ignored issues
show
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...
404
                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...
405 2
            }
406
        });
407 2
408 2
        $request->setInfile($file_name);
0 ignored issues
show
Coding Style introduced by
The variable $file_name should be in camel caps format.
Loading history...
409
        $this->_curler->addQueLoop($request);
410
411
        return new Statement($request);
412
    }
413
414
    /**
415 38
     * get Count Pending Query in Queue
416
     *
417 38
     * @return int
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getCountPendingQueue() has useless @return annotation.
Loading history...
418
     */
419
    public function getCountPendingQueue(): int
420
    {
421 1
        return $this->_curler->countPending();
422
    }
423 1
424
    /**
425
     * set Connect TimeOut in seconds [CURLOPT_CONNECTTIMEOUT] ( int )
426 1
     *
427 1
     * @param float $connectTimeOut
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::setConnectTimeOut() has useless @param annotation for parameter $connectTimeOut.
Loading history...
428 1
     */
429 1
    public function setConnectTimeOut(float $connectTimeOut)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::setConnectTimeOut() does not have void return type hint.
Loading history...
430
    {
431
        $this->_connectTimeOut = $connectTimeOut;
432
    }
433 1
434 1
    /**
435
     * get ConnectTimeOut in seconds
436
     *
437
     * @return float
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getConnectTimeOut() has useless @return annotation.
Loading history...
438 1
     */
439 1
    public function getConnectTimeOut(): float
440
    {
441
        return $this->_connectTimeOut;
442
    }
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line after function; 2 found
Loading history...
443 1
444 1
445
    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...
446 1
    {
447
        $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...
448 1
449
        // Search X-ClickHouse-Progress
450
        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...
451 1
            $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...
452
            $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...
453
            if (!$header_size) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
Coding Style introduced by
The variable $header_size should be in camel caps format.
Loading history...
454
                return false;
455
            }
456
457
            $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...
458 1
            if (!$header) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
459
                return false;
460
            }
461
462
            $match = [];
463
            if (preg_match_all('/^X-ClickHouse-(?:Progress|Summary):(.*?)$/im', $header, $match)) {
0 ignored issues
show
introduced by
Function preg_match_all() should not be referenced via a fallback global name, but via a use statement.
Loading history...
464
                $data = @json_decode(end($match[1]), true);
0 ignored issues
show
introduced by
Function json_decode() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Function end() should not be referenced via a fallback global name, but via a use statement.
Loading history...
465
                if ($data && is_callable($this->xClickHouseProgress)) {
0 ignored issues
show
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...
466
467
                    if (is_array($this->xClickHouseProgress)) {
0 ignored issues
show
introduced by
Expected 0 lines after "if", found 1.
Loading history...
introduced by
Function is_array() should not be referenced via a fallback global name, but via a use statement.
Loading history...
468 40
                        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...
469
                    } else {
470 40
                        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...
471 40
                    }
472
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
473 40
                }
474
            }
475 1
        }
476
        return false;
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
477 1
    }
478 1
479
    /**
480
     * @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...
481
     * @param null|WhereInFile $whereInFile
0 ignored issues
show
introduced by
Null type hint should be on last position in "null|WhereInFile".
Loading history...
482 40
     * @param null|WriteToFile $writeToFile
0 ignored issues
show
introduced by
Null type hint should be on last position in "null|WriteToFile".
Loading history...
483 1
     * @return CurlerRequest
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getRequestRead() has useless @return annotation.
Loading history...
484 1
     * @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...
485
     */
486
    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...
487
    {
488 40
        $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...
489
        $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...
490
        // ---------------------------------------------------------------------------------
491 40
        if ($whereInFile instanceof WhereInFile && $whereInFile->size()) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
492 1
            // $request = $this->prepareSelectWhereIn($request, $whereInFile);
493
            $structure = $whereInFile->fetchUrlParams();
494
            // $structure = [];
495
            $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...
496 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...
497
        }
498 1
        // ---------------------------------------------------------------------------------
499 1
        // if result to file
500
        if ($writeToFile instanceof WriteToFile && $writeToFile->fetchFormat()) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
501 1
            $query->setFormat($writeToFile->fetchFormat());
502
            unset($urlParams['extremes']);
503 1
        }
504
        // ---------------------------------------------------------------------------------
505
        // makeRequest read
506
        $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...
507
        // ---------------------------------------------------------------------------------
508
        // attach files
509
        if ($whereInFile instanceof WhereInFile && $whereInFile->size()) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
510
            $request->attachFiles($whereInFile->fetchFiles());
511
        }
512
        // ---------------------------------------------------------------------------------
513
        // result to file
514
        if ($writeToFile instanceof WriteToFile && $writeToFile->fetchFormat()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
515
516 1
            $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...
517
            if (is_resource($fout)) {
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...
Coding Style introduced by
Blank line found at start of control structure
Loading history...
518 1
519
                $isGz = $writeToFile->getGzip();
520
521 40
                if ($isGz) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 2.
Loading history...
522 1
                    // write gzip header
523
                    // "\x1f\x8b\x08\x00\x00\x00\x00\x00"
524
                    // fwrite($fout, "\x1F\x8B\x08\x08".pack("V", time())."\0\xFF", 10);
525 40
                    // write the original file name
526
                    // $oname = str_replace("\0", "", basename($writeToFile->fetchFile()));
527
                    // fwrite($fout, $oname."\0", 1+strlen($oname));
528
529 1
                    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...
530
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
531 1
                }
532 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...
533
534
                $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...
535 68
                    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...
536
                });
537 68
            }
538 68
        }
539
540
        if ($this->stdErrOut) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
541
            $request->setStdErrOut($this->stdErrOut);
542
        }
543
        if ($this->xClickHouseProgress) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
544
            $request->setFunctionProgress([$this, '__findXClickHouseProgress']);
545
        }
546 27
        // ---------------------------------------------------------------------------------
547
        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...
548 27
549 27
    }
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...
550
551
    public function cleanQueryDegeneration(): bool
552
    {
553
        $this->_query_degenerations = [];
0 ignored issues
show
Coding Style introduced by
Member variable "_query_degenerations" is not in valid camel caps format
Loading history...
554
        return true;
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
555 38
    }
556
557 38
    public function addQueryDegeneration(Degeneration $degeneration): bool
558 38
    {
559 38
        $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...
560
        return true;
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
561 38
    }
562
563
    /**
564
     * @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...
565
     * @return CurlerRequest
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getRequestWrite() has useless @return annotation.
Loading history...
566
     * @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...
567
     */
568
    public function getRequestWrite(Query $query): CurlerRequest
569 46
    {
570
        $urlParams = ['readonly' => 0];
571
        return $this->makeRequest($query, $urlParams);
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
572
    }
573 46
574 46
    /**
575
     * @throws TransportException
576
     */
577 46
    public function ping(): bool
578
    {
579
        $request = new CurlerRequest(false, $this->getHandle());
580
        $request->url($this->getUri())->verbose(false)->GET()->connectTimeOut($this->getConnectTimeOut());
581
        $this->_curler->execOne($request);
582
583
        return trim($request->response()->body()) === 'Ok.';
0 ignored issues
show
introduced by
Function trim() should not be referenced via a fallback global name, but via a use statement.
Loading history...
584
    }
585
586
    /**
587
     * @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...
588
     * @param mixed[] $bindings
589 39
     * @return Query
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::prepareQuery() has useless @return annotation.
Loading history...
590
     */
591 39
    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...
592
    {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
593
594 39
        // add Degeneration query
595 39
        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...
596 39
            $degeneration->bindParams($bindings);
597
        }
598
599
        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...
600
    }
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line after function; 2 found
Loading history...
601
602
603
    /**
604
     * @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...
605
     * @param mixed[] $bindings
0 ignored issues
show
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
606 28
     * @param null|WhereInFile $whereInFile
0 ignored issues
show
introduced by
Null type hint should be on last position in "null|WhereInFile".
Loading history...
607
     * @param null|WriteToFile $writeToFile
0 ignored issues
show
introduced by
Null type hint should be on last position in "null|WriteToFile".
Loading history...
608 28
     * @return CurlerRequest
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::prepareSelect() has useless @return annotation.
Loading history...
609
     * @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...
610
     */
611
    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...
612 28
    {
613 27
        if ($sql instanceof Query) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
614
            return $this->getRequestWrite($sql);
615
        }
616
        $query = $this->prepareQuery($sql, $bindings);
617
        $query->setFormat('JSON');
618
        return $this->getRequestRead($query, $whereInFile, $writeToFile);
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
619
    }
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line after function; 2 found
Loading history...
620 10
621
622 10
    /**
623
     * @param Query|string $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
624
     * @param mixed[] $bindings
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
625
     * @return CurlerRequest
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::prepareWrite() has useless @return annotation.
Loading history...
626
     * @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...
627
     */
628
    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...
629
    {
630
        if ($sql instanceof Query) {
631
            return $this->getRequestWrite($sql);
632
        }
633
634 32
        $query = $this->prepareQuery($sql, $bindings);
635
636 32
        if (strpos($sql, 'ON CLUSTER') === false) {
0 ignored issues
show
introduced by
Function strpos() should not be referenced via a fallback global name, but via a use statement.
Loading history...
637 32
            return $this->getRequestWrite($query);
638 32
        }
639
640
        if (strpos($sql, 'CREATE') === 0 || strpos($sql, 'DROP') === 0 || strpos($sql, 'ALTER') === 0) {
0 ignored issues
show
introduced by
Function strpos() should not be referenced via a fallback global name, but via a use statement.
Loading history...
641
            $query->setFormat('JSON');
642
        }
643
644
        return $this->getRequestWrite($query);
645
    }
646
647
    /**
648
     * @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...
649
     * @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...
650 7
     */
651
    public function executeAsync(): bool
652 7
    {
653 7
        return $this->_curler->execLoopWait();
654 7
    }
655
656
    /**
657
     * @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...
658
     * @param mixed[] $bindings
0 ignored issues
show
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
659
     * @param null|WhereInFile $whereInFile
0 ignored issues
show
introduced by
Null type hint should be on last position in "null|WhereInFile".
Loading history...
660 1
     * @param null|WriteToFile $writeToFile
0 ignored issues
show
introduced by
Null type hint should be on last position in "null|WriteToFile".
Loading history...
661
     * @return Statement
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::select() has useless @return annotation.
Loading history...
662 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...
663 1
     * @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...
664
     */
665
    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...
666
    {
667
        $request = $this->prepareSelect($sql, $bindings, $whereInFile, $writeToFile);
668
        $this->_curler->execOne($request);
669
        return new Statement($request);
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
670
    }
671
672 28
    /**
673
     * @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...
674 28
     * @param mixed[] $bindings
0 ignored issues
show
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
675 27
     * @param null|WhereInFile $whereInFile
0 ignored issues
show
introduced by
Null type hint should be on last position in "null|WhereInFile".
Loading history...
676 27
     * @param null|WriteToFile $writeToFile
0 ignored issues
show
introduced by
Null type hint should be on last position in "null|WriteToFile".
Loading history...
677 27
     * @return Statement
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::selectAsync() has useless @return annotation.
Loading history...
678 27
     * @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...
679 3
     * @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...
680
     */
681
    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...
682 25
    {
683
        $request = $this->prepareSelect($sql, $bindings, $whereInFile, $writeToFile);
684
        $this->_curler->addQueLoop($request);
685
        return new Statement($request);
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
686
    }
687
688
    /**
689
     * @param callable $callback
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::setProgressFunction() has useless @param annotation for parameter $callback.
Loading history...
690
     */
691 2
    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...
692
    {
693 2
        $this->xClickHouseProgress = $callback;
694 2
    }
695
696
    /**
697
     * @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...
698
     * @param mixed[] $bindings
699
     * @param bool $exception
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
700 2
     * @return Statement
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::write() has useless @return annotation.
Loading history...
701
     * @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...
702
     */
703
    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...
704
    {
705
        $request = $this->prepareWrite($sql, $bindings);
706
        $this->_curler->execOne($request);
707
        $response = new Statement($request);
708
        if ($exception) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
709
            if ($response->isError()) {
710
                $response->error();
711
            }
712
        }
713 2
        return $response;
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
714
    }
715 1
716 1
    /**
717 1
     * @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...
718
     * @param CurlerRequest $request
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::streaming() has useless @param annotation for parameter $request.
Loading history...
719
     * @return Statement
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::streaming() has useless @return annotation.
Loading history...
720
     * @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...
721
     */
722
    private function streaming(Stream $streamRW, CurlerRequest $request): Statement
723
    {
724
        $callable = $streamRW->getClosure();
725 2
        $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...
726
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
727
728 2
        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...
729 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...
730
731 1
            if (!is_callable($callable)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space 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...
732
                if ($streamRW->isWrite()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
733
734
                    $callable = function ($ch, $fd, $length) use ($stream) {
0 ignored issues
show
introduced by
Closure not using "$this" should be declared static.
Loading history...
735
                        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...
736
                    };
737
                } else {
738 2
                    $callable = function ($ch, $fd) use ($stream) {
0 ignored issues
show
introduced by
Closure not using "$this" should be declared static.
Loading history...
739 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...
740 2
                    };
741
                }
742
            }
743 2
744
            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...
745 2
746 2
                if ($streamRW->isWrite()) {
0 ignored issues
show
introduced by
Expected 0 lines after "if", found 1.
Loading history...
747
                    $request->header('Content-Encoding', 'gzip');
748
                    $request->header('Content-Type', 'application/x-www-form-urlencoded');
749
                } else {
750
                    $request->header('Accept-Encoding', 'gzip');
751
                }
752
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
753
            }
754
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
755
756
            $request->header('Transfer-Encoding', 'chunked');
757
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
758
759
            if ($streamRW->isWrite()) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 2.
Loading history...
760 1
                $request->setReadFunction($callable);
761
            } else {
762 1
                $request->setWriteFunction($callable);
763 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...
764 1
765
//                $request->setHeaderFunction($callableHead);
766
            }
767
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
768
769
            $this->_curler->execOne($request, true);
770
            $response = new Statement($request);
771
            if ($response->isError()) {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
772
                $response->error();
773
            }
774
            return $response;
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
775 1
        } finally {
776
            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...
777 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...
778 1
        }
779 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...
780
781
    }
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...
782
783
784
    /**
785
     * @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...
786
     * @param string $sql
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
787
     * @param mixed[] $bindings
788
     * @return Statement
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::streamRead() has useless @return annotation.
Loading history...
789
     * @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...
790
     */
791
    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...
792
    {
793
        $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...
794
        $request = $this->getRequestRead($sql);
795
        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...
796
797
    }
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...
798
799
    /**
800
     * @param Stream $streamWrite
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
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...
801
     * @param string $sql
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
802
     * @param mixed[] $bindings
803
     * @return Statement
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::streamWrite() has useless @return annotation.
Loading history...
804
     * @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...
805
     */
806
    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...
807
    {
808
        $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...
809
        $request = $this->writeStreamData($sql);
810
        return $this->streaming($streamWrite, $request);
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
811
    }
812
813
    public function __destruct()
814
    {
815
        if ($this->handle) {
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
816
            curl_close($this->handle);
0 ignored issues
show
introduced by
Function curl_close() should not be referenced via a fallback global name, but via a use statement.
Loading history...
817
        }
818
    }
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line after function; 2 found
Loading history...
819
820
821
    public function getHandle()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getHandle() does not have return type hint nor @return annotation for its return value.
Loading history...
822
    {
823
        if (!$this->handle) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
824
            $this->handle = curl_init();
0 ignored issues
show
Documentation Bug introduced by
It seems like curl_init() can also be of type CurlHandle. However, the property $handle is declared as type null|resource. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
introduced by
Function curl_init() should not be referenced via a fallback global name, but via a use statement.
Loading history...
825
        }
826
827
        return $this->handle;
828
    }
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after function; 1 found
Loading history...
829
830
}
0 ignored issues
show
introduced by
There must be exactly 0 empty lines before class closing brace.
Loading history...
831