Completed
Pull Request — master (#102)
by Šimon
06:39
created

Http::getRequestRead()   C

Complexity

Conditions 12
Paths 64

Size

Total Lines 58
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 12.0737

Importance

Changes 0
Metric Value
cc 12
eloc 24
nc 64
nop 3
dl 0
loc 58
ccs 23
cts 25
cp 0.92
crap 12.0737
rs 6.9666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
introduced by
Missing declare(strict_types = 1).
Loading history...
introduced by
An error occurred during processing; checking has been aborted. The error message was: "if" without curly braces is not supported.
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 function array_merge;
13
use function fclose;
14
use function http_build_query;
15
use function is_callable;
16
use function is_resource;
17
use const PHP_EOL;
18
use function stripos;
0 ignored issues
show
Coding Style introduced by
Use statements should be sorted alphabetically. The first wrong one is stripos.
Loading history...
19
20
class Http
21
{
22
    /**
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...
23
     * @var string
24
     */
25
    private $username;
26
27
    /**
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...
28
     * @var string
29
     */
30
    private $password;
31
32
    /**
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...
33
     * @var string
34
     */
35
    private $host;
36
37
    /**
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...
38
     * @var int
39
     */
40
    private $port;
41
42
    /** @var bool */
43
    private $https;
44
45
    /**
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...
46
     * @var bool|int
47
     */
48
    private $verbose = false;
49
50
    /**
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...
51
     * @var CurlerRolling
52
     */
53
    private $_curler = null;
54
55
    /**
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...
56
     * @var Settings
57
     */
58
    private $settings;
59
60
    /**
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...
61
     * @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...
62
     */
63
    private $_query_degenerations = [];
64
65
    /**
66
     * Count seconds (float)
67
     *
68
     * @var float
69
     */
70
    private $timeout = 20;
71
72
    /**
73
     * Count seconds (float)
74
     *
75
     * @var float
76
     */
77
    private $connectTimeout = 5;
78
79
    /**
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...
80
     * @var callable|null
81
     */
82
    private $xClickHouseProgress;
83
84
    /** @var string */
85
    private $database;
86
87 63
    public function __construct(
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function __construct()
Loading history...
88
        string $host,
89
        int $port,
90
        string $username,
91
        string $password,
92
        string $database,
93
        bool $https = false
94
    ) {
95 63
        $this->host     = $host;
96 63
        $this->port     = $port;
97 63
        $this->username = $username;
98 63
        $this->password = $password;
99 63
        $this->setDatabase($database);
100 63
        $this->setHttps($https);
101 63
        $this->settings = new Settings();
102
103 63
        $this->setCurler();
104 63
    }
105
106 63
    public function setHttps(bool $https) : void
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function setHttps()
Loading history...
107
    {
108 63
        $this->https = $https;
109 63
    }
110
111 63
    public function setCurler()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::setCurler() does not have void return type hint.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function setCurler()
Loading history...
112
    {
113 63
        $this->_curler = new CurlerRolling();
114 63
    }
115
116
    /**
117
     * @return CurlerRolling
118
     */
119
    public function getCurler()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getCurler() does not have return type hint for its return value but it should be possible to add it based on @return annotation "CurlerRolling".
Loading history...
120
    {
121
        return $this->_curler;
122
    }
123
124 63
    public function setDatabase(string $database) : void
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function setDatabase()
Loading history...
125
    {
126 63
        $this->database = $database;
127 63
    }
128
129
    public function setHost(string $host) : void
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function setHost()
Loading history...
130
    {
131
        $this->host = $host;
132
    }
133
134
    /**
135
     * @return string
136
     */
137 63
    public function getUri()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getUri() does not have return type hint for its return value but it should be possible to add it based on @return annotation "string".
Loading history...
138
    {
139 63
        $proto = 'http';
140 63
        if ($this->https) {
141 1
            $proto = 'https';
142
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
143 63
        $uri = $proto . '://' . $this->host;
144 63
        if (stripos($this->host, '/') !== false || stripos($this->host, ':') !== false) {
145 1
            return $uri;
146
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
147 63
        if ($this->port > 0) {
148 63
            return $uri . ':' . $this->port;
149
        }
150
151 1
        return $uri;
152
    }
153
154 63
    public function getSettings() : Settings
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function getSettings()
Loading history...
155
    {
156 63
        return $this->settings;
157
    }
158
159
    public function setVerbose(bool $flag) : void
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function setVerbose()
Loading history...
160
    {
161
        $this->verbose = $flag;
162
    }
163
164
    /**
165
     * @param mixed[] $params
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
166
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
167 63
    private function getUrl(array $params = []) : string
168
    {
169 63
        $settings = $this->getSettings()->getQueryableSettings($params);
170
171 63
        return $this->getUri() . '?' . http_build_query(array_merge(['database' => $this->database], $settings));
172
    }
173
174
    /**
175
     * @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...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
176
     * @return CurlerRequest
177
     */
178 63
    private function newRequest($extendinfo)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::newRequest() does not have parameter type hint for its parameter $extendinfo but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::newRequest() does not have return type hint for its return value but it should be possible to add it based on @return annotation "CurlerRequest".
Loading history...
Coding Style introduced by
Type hint "array" missing for $extendinfo
Loading history...
179
    {
180 63
        $new = new CurlerRequest();
181 63
        $new->auth($this->username, $this->password)
182 63
            ->POST()
183 63
            ->setRequestExtendedInfo($extendinfo);
184
185 63
        if ($this->getSettings()->isHttpCompressionEnabled()) {
186 63
            $new->httpCompression(true);
187
        }
188
189 63
        if ($this->getSettings()->getSessionId()) {
190 1
            $new->persistent();
191
        }
192
193 63
        $new->setTimeout($this->timeout);
194 63
        $new->setConnectTimeout($this->connectTimeout); // one sec
195 63
        $new->keepAlive(); // one sec
196 63
        $new->verbose((bool) $this->verbose);
197
198 63
        return $new;
199
    }
200
201
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$query" missing
Loading history...
202
     * @param array $urlParams
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
introduced by
@param annotation of method \ClickHouseDB\Transport\Http::makeRequest() does not specify type hint for items of its traversable parameter $urlParams.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Doc comment for parameter $urlParams does not match actual variable name $query
Loading history...
203
     * @param bool  $query_as_string
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected "boolean" but found "bool" for parameter type
Loading history...
Coding Style introduced by
Doc comment for parameter $query_as_string does not match actual variable name $urlParams
Loading history...
204
     * @return CurlerRequest
205
     * @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...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
206
     */
207 63
    private function makeRequest(Query $query, array $urlParams = [], $query_as_string = false)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::makeRequest() does not have parameter type hint for its parameter $query_as_string but it should be possible to add it based on @param annotation "bool".
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::makeRequest() does not have return type hint for its return value but it should be possible to add it based on @return annotation "CurlerRequest".
Loading history...
Coding Style introduced by
Expected type hint "array"; found "Query" for $urlParams
Loading history...
Coding Style introduced by
Expected type hint "bool"; found "array" for $query_as_string
Loading history...
208
    {
209 63
        $sql = $query->toSql();
210
211 63
        if ($query_as_string) {
212 1
            $urlParams['query'] = $sql;
213
        }
214
215 63
        $url = $this->getUrl($urlParams);
216
217
        $extendinfo = [
218 63
            'sql'    => $sql,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 23 spaces, but found 12.
Loading history...
219 63
            'query'  => $query,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 23 spaces, but found 12.
Loading history...
220 63
            'format' => $query->getFormat(),
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 23 spaces, but found 12.
Loading history...
221
        ];
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 22 space(s), but found 8.
Loading history...
222
223 63
        $new = $this->newRequest($extendinfo);
224 63
        $new->url($url);
225
226 63
        if (! $query_as_string) {
227 63
            $new->parameters_json($sql);
228
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
229 63
        if ($this->getSettings()->isHttpCompressionEnabled()) {
230 63
            $new->httpCompression(true);
231
        }
232
233 63
        return $new;
234
    }
235
236
    /**
237
     * @param string|Query $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
238
     * @return CurlerRequest
239
     */
240 3
    public function writeStreamData($sql)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::writeStreamData() does not have return type hint for its return value but it should be possible to add it based on @return annotation "CurlerRequest".
Loading history...
241
    {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
242
243 3
        if ($sql instanceof Query) {
244 1
            $query = $sql;
245
        } else {
246 2
            $query = new Query($sql);
247
        }
248
249 3
        $url        = $this->getUrl([
250 3
            'query' => $query->toSql(),
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 37 spaces, but found 12.
Loading history...
251
        ]);
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 36 space(s), but found 8.
Loading history...
252
        $extendinfo = [
253 3
            'sql'    => $sql,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 23 spaces, but found 12.
Loading history...
254 3
            'query'  => $query,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 23 spaces, but found 12.
Loading history...
255 3
            'format' => $query->getFormat(),
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 23 spaces, but found 12.
Loading history...
256
        ];
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 22 space(s), but found 8.
Loading history...
257
258 3
        $request = $this->newRequest($extendinfo);
259 3
        $request->url($url);
260
261 3
        return $request;
262
    }
263
264
    /**
265
     * @param string $sql
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
introduced by
Incorrect annotations group.
Loading history...
266
     * @param string $file_name
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
267
     * @return Statement
268
     * @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...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
269
     */
270 8
    public function writeAsyncCSV($sql, $file_name)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::writeAsyncCSV() does not have parameter 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 parameter type hint for its parameter $file_name 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 return type hint for its return value but it should be possible to add it based on @return annotation "Statement".
Loading history...
Coding Style introduced by
Type hint "string" missing for $sql
Loading history...
Coding Style introduced by
Type hint "string" missing for $file_name
Loading history...
271
    {
272 8
        $query = new Query($sql);
273
274 8
        $url = $this->getUrl([
275 8
            'query' => $query->toSql(),
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 30 spaces, but found 12.
Loading history...
276
        ]);
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 29 space(s), but found 8.
Loading history...
277
278
        $extendinfo = [
279 8
            'sql'    => $sql,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 23 spaces, but found 12.
Loading history...
280 8
            'query'  => $query,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 23 spaces, but found 12.
Loading history...
281 8
            'format' => $query->getFormat(),
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 23 spaces, but found 12.
Loading history...
282
        ];
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 22 space(s), but found 8.
Loading history...
283
284 8
        $request = $this->newRequest($extendinfo);
285 8
        $request->url($url);
286
287
        $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...
288 8
            $handle = $request->getInfileHandle();
289 8
            if (is_resource($handle)) {
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
290 8
                fclose($handle);
291
            }
292 8
        });
293
294 8
        $request->setInfile($file_name);
295 8
        $this->_curler->addQueLoop($request);
296
297 8
        return new Statement($request);
298
    }
299
300
    /**
301
     * get Count Pending Query in Queue
302
     *
303
     * @return int
0 ignored issues
show
Coding Style introduced by
Expected "integer" but found "int" for function return type
Loading history...
304
     */
305 12
    public function getCountPendingQueue()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getCountPendingQueue() does not have return type hint for its return value but it should be possible to add it based on @return annotation "int".
Loading history...
306
    {
307 12
        return $this->_curler->countPending();
308
    }
309
310 1
    public function getTimeout() : float
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function getTimeout()
Loading history...
311
    {
312 1
        return $this->timeout;
313
    }
314
315 2
    public function setTimeout(float $timeout) : void
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function setTimeout()
Loading history...
316
    {
317 2
        $this->timeout = $timeout;
318 2
    }
319
320
    /**
321
     * Get ConnectTimeout in seconds
322
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
323 39
    public function getConnectTimeout() : float
324
    {
325 39
        return $this->connectTimeout;
326
    }
327
328
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$connectTimeOut" missing
Loading history...
329
     * Set Connect Timeout in seconds
330
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
331 2
    public function setConnectTimeout(float $connectTimeOut) : void
332
    {
333 2
        $this->connectTimeout = $connectTimeOut;
334 2
    }
335
336 1
    private function findXClickHouseProgress($handle)
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...
introduced by
Method \ClickHouseDB\Transport\Http::findXClickHouseProgress() does not have return type hint nor @return annotation for its return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function findXClickHouseProgress()
Loading history...
337
    {
338 1
        $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...
339
340
        // Search X-ClickHouse-Progress
341 1
        if ($code == 200) {
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
introduced by
Operator == is disallowed, use === instead.
Loading history...
342 1
            $response    = curl_multi_getcontent($handle);
0 ignored issues
show
introduced by
Function curl_multi_getcontent() should not be referenced via a fallback global name, but via a use statement.
Loading history...
343 1
            $header_size = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
0 ignored issues
show
introduced by
Function curl_getinfo() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Constant CURLINFO_HEADER_SIZE should not be referenced via a fallback global name, but via a use statement.
Loading history...
344 1
            if (! $header_size) {
345
                return false;
346
            }
347
348 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...
349 1
            if (! $header_size) {
350
                return false;
351
            }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
352 1
            $pos = strrpos($header, 'X-ClickHouse-Progress');
0 ignored issues
show
introduced by
Function strrpos() should not be referenced via a fallback global name, but via a use statement.
Loading history...
353
354 1
            if (! $pos) {
355
                return false;
356
            }
357
358 1
            $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...
359 1
            $data = @json_decode(str_ireplace('X-ClickHouse-Progress:', '', $last), 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 str_ireplace() should not be referenced via a fallback global name, but via a use statement.
Loading history...
360
361 1
            if ($data && $this->xClickHouseProgress !== null) {
362 1
                ($this->xClickHouseProgress)($data);
363
            }
364
        }
365 1
    }
366
367
    /**
368
     * @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 Documentation introduced by
Missing parameter comment
Loading history...
369
     * @param null|WhereInFile $whereInFile
0 ignored issues
show
introduced by
Null type hint should be on last position.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
370
     * @param null|WriteToFile $writeToFile
0 ignored issues
show
introduced by
Null type hint should be on last position.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
371
     * @return CurlerRequest
372
     * @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...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
373
     */
374 35
    public function getRequestRead(Query $query, $whereInFile = null, $writeToFile = null)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getRequestRead() does not have parameter 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 parameter type hint for its parameter $writeToFile but it should be possible to add it based on @param annotation "null|WriteToFile".
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::getRequestRead() does not have return type hint for its return value but it should be possible to add it based on @return annotation "CurlerRequest".
Loading history...
375
    {
376 35
        $query_as_string = false;
377 35
        $urlParams       = [];
378
        // ---------------------------------------------------------------------------------
379 35
        if ($whereInFile instanceof WhereInFile && $whereInFile->size()) {
380
            // $request = $this->prepareSelectWhereIn($request, $whereInFile);
381 1
            $structure       = $whereInFile->fetchUrlParams();
382 1
            $urlParams       = $structure;
383 1
            $query_as_string = true;
384
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
385
        // ---------------------------------------------------------------------------------
386
        // if result to file
387 35
        if ($writeToFile instanceof WriteToFile && $writeToFile->fetchFormat()) {
388 1
            $query->setFormat($writeToFile->fetchFormat());
389 1
            unset($urlParams['extremes']);
390
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
391
        // ---------------------------------------------------------------------------------
392
        // makeRequest read
393 35
        $request = $this->makeRequest($query, $urlParams, $query_as_string);
394
        // ---------------------------------------------------------------------------------
395
        // attach files
396 35
        if ($whereInFile instanceof WhereInFile && $whereInFile->size()) {
397 1
            $request->attachFiles($whereInFile->fetchFiles());
398
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
399
        // ---------------------------------------------------------------------------------
400
        // result to file
401 35
        if ($writeToFile instanceof WriteToFile && $writeToFile->fetchFormat()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
402
403 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...
404 1
            if (is_resource($fout)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
405
406 1
                $isGz = $writeToFile->getGzip();
407
408 1
                if ($isGz) {
409
                    // write gzip header
410
                    // "\x1f\x8b\x08\x00\x00\x00\x00\x00"
411
                    // fwrite($fout, "\x1F\x8B\x08\x08".pack("V", time())."\0\xFF", 10);
412
                    // write the original file name
413
                    // $oname = str_replace("\0", "", basename($writeToFile->fetchFile()));
414
                    // fwrite($fout, $oname."\0", 1+strlen($oname));
415
416
                    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...
417
                }
418
419
                $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...
420
                    fclose($request->getResultFileHandle());
421 1
                });
422
            }
423
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
424 35
        if ($this->xClickHouseProgress !== null) {
425
            $request->setFunctionProgress(function ($x) {
426 1
                return $this->findXClickHouseProgress($x);
427 1
            });
428
        }
429
430
        // ---------------------------------------------------------------------------------
431 35
        return $request;
432
    }
433
434 1
    public function cleanQueryDegeneration() : void
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function cleanQueryDegeneration()
Loading history...
435
    {
436 1
        $this->_query_degenerations = [];
437 1
    }
438
439 63
    public function addQueryDegeneration(Degeneration $degeneration) : void
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function addQueryDegeneration()
Loading history...
440
    {
441 63
        $this->_query_degenerations[] = $degeneration;
442 63
    }
443
444
    /**
445
     * @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...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
446
     * @return CurlerRequest
447
     * @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...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
448
     */
449 63
    public function getRequestWrite(Query $query)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::getRequestWrite() does not have return type hint for its return value but it should be possible to add it based on @return annotation "CurlerRequest".
Loading history...
450
    {
451 63
        return $this->makeRequest($query);
452
    }
453
454
    /**
455
     * @throws TransportException
0 ignored issues
show
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
456
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
457 39
    public function ping() : bool
458
    {
459 39
        $request = new CurlerRequest();
460 39
        $request->url($this->getUri())->verbose(false)->GET()->setConnectTimeout($this->getConnectTimeout());
461 39
        $this->_curler->execOne($request);
462
463 39
        return $request->response()->body() === 'Ok.' . PHP_EOL;
464
    }
465
466
    /**
467
     * @param string  $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
468
     * @param mixed[] $bindings
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
469
     * @return Query
470
     */
471 63
    private function prepareQuery($sql, $bindings)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::prepareQuery() does not have parameter 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 parameter 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::prepareQuery() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Query".
Loading history...
Coding Style introduced by
Type hint "string" missing for $sql
Loading history...
Coding Style introduced by
Type hint "array" missing for $bindings
Loading history...
472
    {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
473
474
        // add Degeneration query
475 63
        foreach ($this->_query_degenerations as $degeneration) {
476 63
            $degeneration->bindParams($bindings);
477
        }
478
479 63
        return new Query($sql, $this->_query_degenerations);
480
    }
481
482
    /**
483
     * @param Query|string     $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
484
     * @param mixed[]          $bindings
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
485
     * @param null|WhereInFile $whereInFile
0 ignored issues
show
introduced by
Null type hint should be on last position.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
486
     * @param null|WriteToFile $writeToFile
0 ignored issues
show
introduced by
Null type hint should be on last position.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
487
     * @return CurlerRequest
488
     * @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...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
489
     */
490 34
    private function prepareSelect($sql, $bindings, $whereInFile, $writeToFile = null)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::prepareSelect() does not have return type hint for its return value but it should be possible to add it based on @return annotation "CurlerRequest".
Loading history...
Coding Style introduced by
Type hint "array" missing for $bindings
Loading history...
491
    {
492 34
        if ($sql instanceof Query) {
493
            return $this->getRequestWrite($sql);
494
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
495 34
        $query = $this->prepareQuery($sql, $bindings);
496 34
        $query->setFormat('JSON');
497
498 34
        return $this->getRequestRead($query, $whereInFile, $writeToFile);
499
    }
500
501
    /**
502
     * @param Query|string $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
503
     * @param mixed[]      $bindings
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
504
     * @return CurlerRequest
505
     * @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...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
506
     */
507 63
    private function prepareWrite($sql, $bindings = [])
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::prepareWrite() does not have return type hint for its return value but it should be possible to add it based on @return annotation "CurlerRequest".
Loading history...
Coding Style introduced by
Type hint "array" missing for $bindings
Loading history...
508
    {
509 63
        if ($sql instanceof Query) {
510
            return $this->getRequestWrite($sql);
511
        }
512
513 63
        $query = $this->prepareQuery($sql, $bindings);
514
515 63
        return $this->getRequestWrite($query);
516
    }
517
518
    /**
519
     * @return bool
0 ignored issues
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
introduced by
Incorrect annotations group.
Loading history...
520
     * @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...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
521
     */
522 10
    public function executeAsync()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::executeAsync() does not have return type hint for its return value but it should be possible to add it based on @return annotation "bool".
Loading history...
523
    {
524 10
        return $this->_curler->execLoopWait();
525
    }
526
527
    /**
528
     * @param Query|string     $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
529
     * @param mixed[]          $bindings
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
530
     * @param null|WhereInFile $whereInFile
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
introduced by
Null type hint should be on last position.
Loading history...
531
     * @param null|WriteToFile $writeToFile
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
introduced by
Null type hint should be on last position.
Loading history...
532
     * @return Statement
533
     * @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...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
534
     * @throws \Exception
0 ignored issues
show
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
introduced by
Class \Exception should not be referenced via a fully qualified name, but via a use statement.
Loading history...
535
     */
536 29
    public function select($sql, array $bindings = [], $whereInFile = null, $writeToFile = null)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::select() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Statement".
Loading history...
537
    {
538 29
        $request = $this->prepareSelect($sql, $bindings, $whereInFile, $writeToFile);
539 29
        $this->_curler->execOne($request);
540
541 29
        return new Statement($request);
542
    }
543
544
    /**
545
     * @param Query|string     $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
546
     * @param mixed[]          $bindings
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
547
     * @param null|WhereInFile $whereInFile
0 ignored issues
show
introduced by
Null type hint should be on last position.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
548
     * @param null|WriteToFile $writeToFile
0 ignored issues
show
introduced by
Null type hint should be on last position.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
549
     * @return Statement
550
     * @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...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
551
     * @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...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
552
     */
553 5
    public function selectAsync($sql, array $bindings = [], $whereInFile = null, $writeToFile = null)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::selectAsync() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Statement".
Loading history...
554
    {
555 5
        $request = $this->prepareSelect($sql, $bindings, $whereInFile, $writeToFile);
556 5
        $this->_curler->addQueLoop($request);
557
558 5
        return new Statement($request);
559
    }
560
561 1
    public function setProgressFunction(callable $callback) : void
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function setProgressFunction()
Loading history...
562
    {
563 1
        $this->xClickHouseProgress = $callback;
564 1
    }
565
566
    /**
567
     * @param string  $sql
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
568
     * @param mixed[] $bindings
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
569
     * @param bool    $exception
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected "boolean" but found "bool" for parameter type
Loading history...
570
     * @return Statement
571
     * @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...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
572
     */
573 63
    public function write($sql, array $bindings = [], $exception = true)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::write() does not have parameter 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 parameter type hint for its parameter $exception but it should be possible to add it based on @param annotation "bool".
Loading history...
introduced by
Method \ClickHouseDB\Transport\Http::write() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Statement".
Loading history...
Coding Style introduced by
Type hint "string" missing for $sql
Loading history...
Coding Style introduced by
Type hint "bool" missing for $exception
Loading history...
574
    {
575 63
        $request = $this->prepareWrite($sql, $bindings);
576 63
        $this->_curler->execOne($request);
577 63
        $response = new Statement($request);
578 63
        if ($exception) {
579 63
            if ($response->isError()) {
580 3
                $response->error();
581
            }
582
        }
583
584 63
        return $response;
585
    }
586
587
    /**
588
     * @param Stream        $streamRW
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::streaming() has useless @param annotation for parameter $streamRW.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
introduced by
Incorrect annotations group.
Loading history...
589
     * @param CurlerRequest $request
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::streaming() has useless @param annotation for parameter $request.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
590
     * @return Statement
591
     * @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...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
592
     */
593 2
    private function streaming(Stream $streamRW, CurlerRequest $request)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\Http::streaming() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Statement".
Loading history...
594
    {
595 2
        $callable = $streamRW->getClosure();
596 2
        $stream   = $streamRW->getStream();
597
598
        try {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
599
600 2
            if (! is_callable($callable)) {
601
                if ($streamRW->isWrite()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
602
603
                    $callable = function ($ch, $fd, $length) use ($stream) {
0 ignored issues
show
introduced by
Closure not using "$this" should be declared static.
Loading history...
604
                        return ($line = fread($stream, $length)) ? $line : '';
0 ignored issues
show
introduced by
Useless parentheses.
Loading history...
introduced by
Function fread() should not be referenced via a fallback global name, but via a use statement.
Loading history...
605
                    };
606
                } else {
607
                    $callable = function ($ch, $fd) use ($stream) {
0 ignored issues
show
introduced by
Closure not using "$this" should be declared static.
Loading history...
608
                        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...
609
                    };
610
                }
611
            }
612
613 2
            if ($streamRW->isGzipHeader()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
614
615 1
                if ($streamRW->isWrite()) {
616 1
                    $request->header('Content-Encoding', 'gzip');
617 1
                    $request->header('Content-Type', 'application/x-www-form-urlencoded');
618
                } else {
619
                    $request->header('Accept-Encoding', 'gzip');
620
                }
621
            }
622
623 2
            $request->header('Transfer-Encoding', 'chunked');
624
625 2
            if ($streamRW->isWrite()) {
626 1
                $request->setReadFunction($callable);
627
            } else {
628 1
                $request->setWriteFunction($callable);
629
//                $request->setHeaderFunction($callableHead);
630
            }
631
632 2
            $this->_curler->execOne($request, true);
633 2
            $response = new Statement($request);
634 2
            if ($response->isError()) {
635
                $response->error();
636
            }
637
638 2
            return $response;
639
        } finally {
640 2
            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...
641 2
                fclose($stream);
642
        }
643
    }
644
645
    /**
646
     * @param Stream  $streamRead
647
     * @param string  $sql
648
     * @param mixed[] $bindings
649
     * @return Statement
650
     * @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...
651
     */
652 1
    public function streamRead(Stream $streamRead, $sql, $bindings = [])
653
    {
654 1
        $sql     = $this->prepareQuery($sql, $bindings);
655 1
        $request = $this->getRequestRead($sql);
656
657 1
        return $this->streaming($streamRead, $request);
658
    }
659
660
    /**
661
     * @param Stream  $streamWrite
662
     * @param string  $sql
663
     * @param mixed[] $bindings
664
     * @return Statement
665
     * @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...
666
     */
667 1
    public function streamWrite(Stream $streamWrite, $sql, $bindings = [])
668
    {
669 1
        $sql     = $this->prepareQuery($sql, $bindings);
670 1
        $request = $this->writeStreamData($sql);
671
672 1
        return $this->streaming($streamWrite, $request);
673
    }
674
}
675