Passed
Push — master ( afce37...5f7fa6 )
by Igor
02:17
created

Client::insertBatchFiles()   B

Complexity

Conditions 11
Paths 24

Size

Total Lines 45
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 12.8905

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 45
c 0
b 0
f 0
ccs 15
cts 20
cp 0.75
rs 7.3166
cc 11
nc 24
nop 4
crap 12.8905

How to fix   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...
2
3
namespace ClickHouseDB;
4
5
use ClickHouseDB\Exception\QueryException;
6
use ClickHouseDB\Query\Degeneration\Bindings;
7
use ClickHouseDB\Query\WhereInFile;
8
use ClickHouseDB\Query\WriteToFile;
9
use ClickHouseDB\Quote\FormatLine;
10
use ClickHouseDB\Transport\Http;
11
use ClickHouseDB\Transport\Stream;
12
use function sprintf;
13
14
class Client
15
{
16
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Client::$_transport with single line content, use one-line comment instead.
Loading history...
17
     * @var Http
18
     */
19
    private $_transport = null;
20
21
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Client::$_connect_username with single line content, use one-line comment instead.
Loading history...
22
     * @var string
23
     */
24
    private $_connect_username = '';
25
26
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Client::$_connect_password with single line content, use one-line comment instead.
Loading history...
27
     * @var string
28
     */
29
    private $_connect_password = '';
30
31
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Client::$_connect_host with single line content, use one-line comment instead.
Loading history...
32
     * @var string
33
     */
34
    private $_connect_host = '';
35
36
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Client::$_connect_port with single line content, use one-line comment instead.
Loading history...
37
     * @var string
38
     */
39
    private $_connect_port = '';
40
41
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Client::$_connect_user_readonly with single line content, use one-line comment instead.
Loading history...
42
     * @var bool
43
     */
44
    private $_connect_user_readonly = false;
45
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Client::$_support_format with single line content, use one-line comment instead.
Loading history...
46
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \ClickHouseDB\Client::$_support_format does not specify type hint for its items.
Loading history...
47
     */
48
    private $_support_format = ['TabSeparated', 'TabSeparatedWithNames', 'CSV', 'CSVWithNames', 'JSONEachRow'];
49
50
    /**
51
     * Client constructor.
0 ignored issues
show
introduced by
Documentation comment contains forbidden comment "Client constructor.".
Loading history...
52
     * @param array $connect_params
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::__construct() does not specify type hint for items of its traversable parameter $connect_params.
Loading history...
53
     * @param array $settings
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::__construct() does not specify type hint for items of its traversable parameter $settings.
Loading history...
54
     */
55 59
    public function __construct($connect_params, $settings = [])
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::__construct() does not have parameter type hint for its parameter $connect_params but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \ClickHouseDB\Client::__construct() does not have parameter type hint for its parameter $settings but it should be possible to add it based on @param annotation "array".
Loading history...
56
    {
57 59
        if (!isset($connect_params['username'])) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
58
            throw  new \InvalidArgumentException('not set username');
59
        }
60
61 59
        if (!isset($connect_params['password'])) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
62
            throw  new \InvalidArgumentException('not set password');
63
        }
64
65 59
        if (!isset($connect_params['port'])) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
66
            throw  new \InvalidArgumentException('not set port');
67
        }
68
69 59
        if (!isset($connect_params['host'])) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
70
            throw  new \InvalidArgumentException('not set host');
71
        }
72
73 59
        if (isset($connect_params['settings']) && is_array($connect_params['settings'])) {
0 ignored issues
show
introduced by
Function is_array() should not be referenced via a fallback global name, but via a use statement.
Loading history...
74 2
            if (empty($settings)) {
75 2
                $settings = $connect_params['settings'];
76
            }
77
        }
78
79 59
        $this->_connect_username    = $connect_params['username'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 1 space but found 4 spaces

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

To visualize

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

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

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

will produce no issues.

Loading history...
80 59
        $this->_connect_password    = $connect_params['password'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 1 space but found 4 spaces

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

To visualize

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

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

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

will produce no issues.

Loading history...
81 59
        $this->_connect_port        = $connect_params['port'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 8 spaces

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

To visualize

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

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

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

will produce no issues.

Loading history...
82 59
        $this->_connect_host        = $connect_params['host'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 8 spaces

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

To visualize

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

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

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

will produce no issues.

Loading history...
83
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
84
85
        // init transport class
86 59
        $this->_transport = new Http(
87 59
            $this->_connect_host,
88 59
            $this->_connect_port,
89 59
            $this->_connect_username,
90 59
            $this->_connect_password
91
        );
92
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
93
94 59
        $this->_transport->addQueryDegeneration(new Bindings());
95
96
        // apply settings to transport class
97 59
        $this->settings()->database('default');
98 59
        if (sizeof($settings)) {
0 ignored issues
show
introduced by
Function sizeof() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Coding Style introduced by
The use of function sizeof() is forbidden; use count() instead
Loading history...
99 2
            $this->settings()->apply($settings);
100
        }
101
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
102
103 59
        if (isset($connect_params['readonly']))
104
        {
105
            $this->setReadOnlyUser($connect_params['readonly']);
106
        }
107
108 59
        if (isset($connect_params['https']))
109
        {
110
            $this->https($connect_params['https']);
111
        }
112
113 59
        $this->enableHttpCompression();
114
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
115
116 59
    }
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...
117
118
    /**
119
     * if the user has only read in the config file
120
     *
121
     * @param bool $flag
122
     */
123
    public function setReadOnlyUser($flag)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::setReadOnlyUser() does not have parameter type hint for its parameter $flag but it should be possible to add it based on @param annotation "bool".
Loading history...
introduced by
Method \ClickHouseDB\Client::setReadOnlyUser() does not have void return type hint.
Loading history...
124
    {
125
        $this->_connect_user_readonly = $flag;
126
        $this->settings()->setReadOnlyUser($this->_connect_user_readonly);
127
    }
128
    /**
129
     * Clear Degeneration processing request [template ]
130
     *
131
     * @return bool
132
     */
133 1
    public function cleanQueryDegeneration()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::cleanQueryDegeneration() 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...
134
    {
135 1
        return $this->_transport->cleanQueryDegeneration();
136
    }
137
138
    /**
139
     * add Degeneration processing
140
     *
141
     * @param Query\Degeneration $degeneration
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::addQueryDegeneration() has useless @param annotation for parameter $degeneration.
Loading history...
142
     * @return bool
143
     */
144
    public function addQueryDegeneration(Query\Degeneration $degeneration)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::addQueryDegeneration() does not have return type hint for its return value but it should be possible to add it based on @return annotation "bool".
Loading history...
145
    {
146
        return $this->_transport->addQueryDegeneration($degeneration);
147
    }
148
149
    /**
150
     * add Conditions in query
151
     *
152
     * @return bool
153
     */
154 1
    public function enableQueryConditions()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::enableQueryConditions() 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...
155
    {
156 1
        return $this->_transport->addQueryDegeneration(new \ClickHouseDB\Query\Degeneration\Conditions());
0 ignored issues
show
introduced by
Class \ClickHouseDB\Query\Degeneration\Conditions should not be referenced via a fully qualified name, but via a use statement.
Loading history...
157
    }
158
    /**
159
     * Set connection host
160
     *
161
     * @param string|array $host
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::setHost() does not specify type hint for items of its traversable parameter $host.
Loading history...
162
     */
163
    public function setHost($host)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::setHost() does not have void return type hint.
Loading history...
164
    {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
165
166
        if (is_array($host))
0 ignored issues
show
introduced by
Function is_array() should not be referenced via a fallback global name, but via a use statement.
Loading history...
167
        {
168
            $host = array_rand(array_flip($host));
0 ignored issues
show
introduced by
Function array_rand() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Function array_flip() should not be referenced via a fallback global name, but via a use statement.
Loading history...
169
        }
170
171
        $this->_connect_host = $host;
172
        $this->transport()->setHost($host);
173
    }
174
175
    /**
176
     * Таймаут
177
     *
178
     * @param int $timeout
179
     * @return Settings
180
     */
181 2
    public function setTimeout($timeout)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::setTimeout() does not have parameter type hint for its parameter $timeout but it should be possible to add it based on @param annotation "int".
Loading history...
introduced by
Method \ClickHouseDB\Client::setTimeout() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Settings".
Loading history...
182
    {
183 2
        return $this->settings()->max_execution_time($timeout);
184
    }
185
186
    /**
187
     * Timeout
188
     *
189
     * @return mixed
190
     */
191 1
    public function getTimeout()
192
    {
193 1
        return $this->settings()->getTimeOut();
194
    }
195
196
    /**
197
     * ConnectTimeOut in seconds ( support 1.5 = 1500ms )
198
     *
199
     * @param int $connectTimeOut
200
     */
201 2
    public function setConnectTimeOut($connectTimeOut)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::setConnectTimeOut() does not have parameter type hint for its parameter $connectTimeOut but it should be possible to add it based on @param annotation "int".
Loading history...
introduced by
Method \ClickHouseDB\Client::setConnectTimeOut() does not have void return type hint.
Loading history...
202
    {
203 2
        $this->transport()->setConnectTimeOut($connectTimeOut);
204 2
    }
205
206
    /**
207
     * get ConnectTimeOut
208
     *
209
     * @return int
210
     */
211 1
    public function getConnectTimeOut()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::getConnectTimeOut() 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...
212
    {
213 1
        return $this->transport()->getConnectTimeOut();
214
    }
215
216
217
    /**
218
     * transport
219
     *
220
     * @return Http
221
     */
222 59
    public function transport()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::transport() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Http".
Loading history...
223
    {
224 59
        if (!$this->_transport) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
225
            throw  new \InvalidArgumentException('Empty transport class');
226
        }
227 59
        return $this->_transport;
228
    }
229
230
    /**
231
     * @return string
232
     */
233
    public function getConnectHost()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::getConnectHost() 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...
234
    {
235
        return $this->_connect_host;
236
    }
237
238
    /**
239
     * @return string
240
     */
241
    public function getConnectPassword()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::getConnectPassword() 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...
242
    {
243
        return $this->_connect_password;
244
    }
245
246
    /**
247
     * @return string
248
     */
249
    public function getConnectPort()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::getConnectPort() 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...
250
    {
251
        return $this->_connect_port;
252
    }
253
254
    /**
255
     * @return string
256
     */
257
    public function getConnectUsername()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::getConnectUsername() 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...
258
    {
259
        return $this->_connect_username;
260
    }
261
262
    /**
263
     * transport
264
     *
265
     * @return Http
266
     */
267
    public function getTransport()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::getTransport() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Http".
Loading history...
268
    {
269
        return $this->_transport;
270
    }
271
272
273
    /**
274
     * Режим отладки CURL
275
     *
276
     * @return mixed
277
     */
278
    public function verbose()
279
    {
280
        return $this->transport()->verbose(true);
281
    }
282
283
    /**
284
     * @return Settings
285
     */
286 59
    public function settings()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::settings() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Settings".
Loading history...
287
    {
288 59
        return $this->transport()->settings();
289
    }
290
291
    /**
292
     * @return $this
293
     */
294 2
    public function useSession($useSessionId = false)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::useSession() does not have parameter type hint nor @param annotation for its parameter $useSessionId.
Loading history...
295
    {
296 2
        if (!$this->settings()->getSessionId())
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
297
        {
298 2
            if (!$useSessionId)
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
299
            {
300 2
                $this->settings()->makeSessionId();
301
            } else
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after ELSE keyword; newline found
Loading history...
302
            {
303
                $this->settings()->session_id($useSessionId);
304
            }
305
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
306
        }
307 2
        return $this;
308
    }
309
    /**
310
     * @return mixed
311
     */
312 2
    public function getSession()
313
    {
314 2
        return $this->settings()->getSessionId();
315
    }
316
317
    /**
318
     * Query CREATE/DROP
319
     *
320
     * @param string $sql
321
     * @param array $bindings
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::write() does not specify type hint for items of its traversable parameter $bindings.
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
322
     * @param bool $exception
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
323
     * @return Statement
324
     * @throws Exception\TransportException
325
     */
326 23
    public function write($sql, $bindings = [], $exception = true)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::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\Client::write() does not have parameter type hint for its parameter $bindings but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \ClickHouseDB\Client::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\Client::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...
327
    {
328 23
        return $this->transport()->write($sql, $bindings, $exception);
329
    }
330
331
    /**
332
     * set db name
333
     * @param string $db
334
     * @return $this
335
     */
336 59
    public function database($db)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::database() does not have parameter type hint for its parameter $db but it should be possible to add it based on @param annotation "string".
Loading history...
337
    {
338 59
        $this->settings()->database($db);
339 59
        return $this;
340
    }
341
342
    /**
343
     * Write to system.query_log
344
     *
345
     * @param bool $flag
346
     * @return $this
347
     */
348
    public function enableLogQueries($flag = true)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::enableLogQueries() does not have parameter type hint for its parameter $flag but it should be possible to add it based on @param annotation "bool".
Loading history...
349
    {
350
        $this->settings()->set('log_queries', intval($flag));
0 ignored issues
show
introduced by
Function intval() should not be referenced via a fallback global name, but via a use statement.
Loading history...
351
        return $this;
352
    }
353
354
    /**
355
     * Compress the result if the HTTP client said that it understands data compressed with gzip or deflate
356
     *
357
     * @param bool $flag
358
     * @return $this
359
     */
360 59
    public function enableHttpCompression($flag = true)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::enableHttpCompression() does not have parameter type hint for its parameter $flag but it should be possible to add it based on @param annotation "bool".
Loading history...
361
    {
362 59
        $this->settings()->enableHttpCompression($flag);
363 59
        return $this;
364
    }
365
366
    /**
367
     * Enable / Disable HTTPS
368
     *
369
     * @param bool $flag
370
     * @return $this
371
     */
372 1
    public function https($flag = true)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::https() does not have parameter type hint for its parameter $flag but it should be possible to add it based on @param annotation "bool".
Loading history...
373
    {
374 1
        $this->settings()->https($flag);
375 1
        return $this;
376
    }
377
378
    /**
379
     * Read extremes of the result columns. They can be output in JSON-formats.
380
     *
381
     * @param bool $flag
382
     * @return $this
383
     */
384 2
    public function enableExtremes($flag = true)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::enableExtremes() does not have parameter type hint for its parameter $flag but it should be possible to add it based on @param annotation "bool".
Loading history...
385
    {
386 2
        $this->settings()->set('extremes', intval($flag));
0 ignored issues
show
introduced by
Function intval() should not be referenced via a fallback global name, but via a use statement.
Loading history...
387 2
        return $this;
388
    }
389
390
    /**
391
     * SELECT
392
     *
393
     * @param string $sql
0 ignored issues
show
Coding Style introduced by
Expected 11 spaces after parameter type; 1 found
Loading history...
394
     * @param array $bindings
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::select() does not specify type hint for items of its traversable parameter $bindings.
Loading history...
Coding Style introduced by
Expected 12 spaces after parameter type; 1 found
Loading history...
395
     * @param null|WhereInFile $whereInFile
396
     * @param null|WriteToFile $writeToFile
397
     * @return Statement
398
     * @throws Exception\TransportException
399
     * @throws \Exception
400
     */
401 26
    public function select($sql, $bindings = [], $whereInFile = null, $writeToFile = null)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::select() 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\Client::select() does not have parameter type hint for its parameter $bindings but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \ClickHouseDB\Client::select() 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\Client::select() 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\Client::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...
402
    {
403 26
        return $this->transport()->select($sql, $bindings, $whereInFile, $writeToFile);
404
    }
405
406
    /**
407
     * execute run
408
     *
409
     * @return bool
410
     * @throws Exception\TransportException
411
     */
412 10
    public function executeAsync()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::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...
413
    {
414 10
        return $this->transport()->executeAsync();
415
    }
416
417
    /**
418
     * set progressFunction
419
     *
420
     * @param callable $callback
421
     */
422 1
    public function progressFunction($callback)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::progressFunction() does not have parameter type hint for its parameter $callback but it should be possible to add it based on @param annotation "callable".
Loading history...
introduced by
Method \ClickHouseDB\Client::progressFunction() does not have void return type hint.
Loading history...
423
    {
424 1
        if (!is_callable($callback)) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a 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...
425
            throw new \InvalidArgumentException('Not is_callable progressFunction');
426
        }
427
428 1
        if (!$this->settings()->is('send_progress_in_http_headers'))
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
429
        {
430 1
            $this->settings()->set('send_progress_in_http_headers', 1);
431
        }
432 1
        if (!$this->settings()->is('http_headers_progress_interval_ms'))
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
433
        {
434 1
            $this->settings()->set('http_headers_progress_interval_ms', 100);
435
        }
436
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
437
438 1
        $this->transport()->setProgressFunction($callback);
439 1
    }
440
441
    /**
442
     * prepare select
443
     *
444
     * @param string $sql
0 ignored issues
show
Coding Style introduced by
Expected 11 spaces after parameter type; 1 found
Loading history...
445
     * @param array $bindings
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::selectAsync() does not specify type hint for items of its traversable parameter $bindings.
Loading history...
Coding Style introduced by
Expected 12 spaces after parameter type; 1 found
Loading history...
446
     * @param null|WhereInFile $whereInFile
447
     * @param null|WriteToFile $writeToFile
448
     * @return Statement
449
     * @throws Exception\TransportException
450
     * @throws \Exception
451
     */
452 5
    public function selectAsync($sql, $bindings = [], $whereInFile = null, $writeToFile = null)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::selectAsync() 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\Client::selectAsync() does not have parameter type hint for its parameter $bindings but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \ClickHouseDB\Client::selectAsync() 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\Client::selectAsync() 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\Client::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...
453
    {
454 5
        return $this->transport()->selectAsync($sql, $bindings, $whereInFile, $writeToFile);
455
    }
456
457
    /**
458
     * SHOW PROCESSLIST
459
     *
460
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Client::showProcesslist() does not specify type hint for items of its traversable return value.
Loading history...
461
     * @throws Exception\TransportException
462
     * @throws \Exception
463
     */
464
    public function showProcesslist()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::showProcesslist() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
465
    {
466
        return $this->select('SHOW PROCESSLIST')->rows();
467
    }
468
469
    /**
470
     * show databases
471
     *
472
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Client::showDatabases() does not specify type hint for items of its traversable return value.
Loading history...
473
     * @throws Exception\TransportException
474
     * @throws \Exception
475
     */
476
    public function showDatabases()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::showDatabases() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
477
    {
478
        return $this->select('show databases')->rows();
479
    }
480
481
    /**
482
     * statement = SHOW CREATE TABLE
483
     *
484
     * @param string $table
485
     * @return mixed
486
     * @throws Exception\TransportException
487
     * @throws \Exception
488
     */
489
    public function showCreateTable($table)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::showCreateTable() does not have parameter type hint for its parameter $table but it should be possible to add it based on @param annotation "string".
Loading history...
490
    {
491
        return ($this->select('SHOW CREATE TABLE ' . $table)->fetchOne('statement'));
0 ignored issues
show
introduced by
Usage of language construct "return" with parentheses is disallowed.
Loading history...
492
    }
493
494
    /**
495
     * SHOW TABLES
496
     *
497
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Client::showTables() does not specify type hint for items of its traversable return value.
Loading history...
498
     * @throws Exception\TransportException
499
     * @throws \Exception
500
     */
501 1
    public function showTables()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::showTables() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
502
    {
503 1
        return $this->select('SHOW TABLES')->rowsAsTree('name');
504
    }
505
506
    /**
507
     * Get the number of simultaneous/Pending requests
508
     *
509
     * @return int
510
     */
511 12
    public function getCountPendingQueue()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::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...
512
    {
513 12
        return $this->transport()->getCountPendingQueue();
514
    }
515
516
    /**
517
     * Insert Array
518
     *
519
     * @param string $table
520
     * @param array $values
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::insert() does not specify type hint for items of its traversable parameter $values.
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
521
     * @param array $columns
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::insert() does not specify type hint for items of its traversable parameter $columns.
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
522
     * @return Statement
523
     * @throws Exception\TransportException
524
     */
525 6
    public function insert($table, $values, $columns = [])
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::insert() does not have parameter type hint for its parameter $table but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::insert() does not have parameter type hint for its parameter $values but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \ClickHouseDB\Client::insert() does not have parameter type hint for its parameter $columns but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \ClickHouseDB\Client::insert() 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...
526
    {
527 6
        if (stripos($table,'`')===false &&  stripos($table,'.')===false) {
0 ignored issues
show
introduced by
Function stripos() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Coding Style introduced by
Expected 1 space after logical operator; 2 found
Loading history...
528 3
            $table = '`' . $table . "`"; //quote table name for dot names
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal ` does not require double quotes, as per coding-style, please use single quotes.

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

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

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

<?php

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

print $doubleQuoted;

will print an indented: Single is Value

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

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

Loading history...
529
        }
530 6
        $sql = 'INSERT INTO ' . $table;
531
532 6
        if (0 !== count($columns)) {
0 ignored issues
show
introduced by
Yoda comparisons are disallowed.
Loading history...
introduced by
Function count() should not be referenced via a fallback global name, but via a use statement.
Loading history...
533 6
            $sql .= ' (`' . implode('`,`', $columns) . '`) ';
0 ignored issues
show
introduced by
Function implode() should not be referenced via a fallback global name, but via a use statement.
Loading history...
534
        }
535
536 6
        $sql .= ' VALUES ';
537
538 6
        foreach ($values as $row) {
539 6
            $sql .= ' (' . FormatLine::Insert($row) . '), ';
540
        }
541 6
        $sql = trim($sql, ', ');
0 ignored issues
show
introduced by
Function trim() should not be referenced via a fallback global name, but via a use statement.
Loading history...
542 6
        return $this->transport()->write($sql);
543
    }
544
545
    /**
546
      * Prepares the values to insert from the associative array.
547
      * There may be one or more lines inserted, but then the keys inside the array list must match (including in the sequence)
548
      *
549
      * @param array $values - array column_name => value (if we insert one row) or array list column_name => value if we insert many lines
550
      * @return array - list of arrays - 0 => fields, 1 => list of value arrays for insertion
551
      */
552 3
    public function prepareInsertAssocBulk(array $values)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::prepareInsertAssocBulk() does not have @param annotation for its traversable parameter $values.
Loading history...
introduced by
Method \ClickHouseDB\Client::prepareInsertAssocBulk() does not have return type hint nor @return annotation for its return value.
Loading history...
553
    {
554 3
        if (isset($values[0]) && is_array($values[0])) { //случай, когда много строк вставляется
0 ignored issues
show
introduced by
Function is_array() should not be referenced via a fallback global name, but via a use statement.
Loading history...
555 2
            $preparedFields = array_keys($values[0]);
0 ignored issues
show
introduced by
Function array_keys() should not be referenced via a fallback global name, but via a use statement.
Loading history...
556 2
            $preparedValues = [];
557 2
            foreach ($values as $idx => $row) {
558 2
                $_fields = array_keys($row);
0 ignored issues
show
introduced by
Function array_keys() should not be referenced via a fallback global name, but via a use statement.
Loading history...
559 2
                if ($_fields !== $preparedFields) {
560 1
                    throw new QueryException("Fields not match: " . implode(',', $_fields) . " and " . implode(',', $preparedFields) . " on element $idx");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Fields not match: does not require double quotes, as per coding-style, please use single quotes.

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

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

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

<?php

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

print $doubleQuoted;

will print an indented: Single is Value

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

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

Loading history...
introduced by
Function implode() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Coding Style Comprehensibility introduced by
The string literal and does not require double quotes, as per coding-style, please use single quotes.

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

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

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

<?php

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

print $doubleQuoted;

will print an indented: Single is Value

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

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

Loading history...
Coding Style Best Practice introduced by
Variable "%s" not allowed in double quoted string; use sprintf() or concatenation instead

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

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

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
561
                }
562 2
                $preparedValues[] = array_values($row);
0 ignored issues
show
introduced by
Function array_values() should not be referenced via a fallback global name, but via a use statement.
Loading history...
563
            }
564
        } else { //одна строка
565 1
            $preparedFields = array_keys($values);
0 ignored issues
show
introduced by
Function array_keys() should not be referenced via a fallback global name, but via a use statement.
Loading history...
566 1
            $preparedValues = [array_values($values)];
0 ignored issues
show
introduced by
Function array_values() should not be referenced via a fallback global name, but via a use statement.
Loading history...
567
        }
568 2
        return [$preparedFields, $preparedValues];
569
    }
570
571
    /**
572
     * Inserts one or more rows from an associative array.
573
     * If there is a discrepancy between the keys of the value arrays (or their order) - throws an exception.
574
     *
575
     * @param string $table - table name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
576
     * @param array $values - array column_name => value (if we insert one row) or array list column_name => value if we insert many lines
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::insertAssocBulk() does not specify type hint for items of its traversable parameter $values.
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
577
     * @return Statement
578
     * @throws QueryException
579
     * @throws Exception\TransportException
580
     */
581
    public function insertAssocBulk($table, array $values)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::insertAssocBulk() does not have parameter type hint for its parameter $table but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::insertAssocBulk() 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...
582
    {
583
        list($columns, $vals) = $this->prepareInsertAssocBulk($values);
584
        return $this->insert($table, $vals, $columns);
585
    }
586
587
    /**
588
     * insert TabSeparated files
589
     *
590
     * @param string $table_name
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
591
     * @param string|array $file_names
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::insertBatchTSVFiles() does not specify type hint for items of its traversable parameter $file_names.
Loading history...
592
     * @param array $columns_array
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::insertBatchTSVFiles() does not specify type hint for items of its traversable parameter $columns_array.
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
593
     * @return mixed
594
     * @throws Exception\TransportException
595
     */
596 1
    public function insertBatchTSVFiles($table_name, $file_names, $columns_array=[])
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::insertBatchTSVFiles() does not have parameter type hint for its parameter $table_name but it should be possible to add it based on @param annotation "string".
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$columns_array" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$columns_array"; expected 1 but found 0
Loading history...
597
    {
598 1
        return $this->insertBatchFiles($table_name, $file_names, $columns_array, 'TabSeparated');
599
    }
600
601
    /**
602
     * insert Batch Files
603
     *
604
     * @param string $table_name
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
605
     * @param string|array $file_names
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::insertBatchFiles() does not specify type hint for items of its traversable parameter $file_names.
Loading history...
606
     * @param array $columns_array
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::insertBatchFiles() does not specify type hint for items of its traversable parameter $columns_array.
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
607
     * @param string $format ['TabSeparated','TabSeparatedWithNames','CSV','CSVWithNames']
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter name; 1 found
Loading history...
608
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Client::insertBatchFiles() does not specify type hint for items of its traversable return value.
Loading history...
609
     * @throws Exception\TransportException
610
     */
611 8
    public function insertBatchFiles($table_name, $file_names, $columns_array=[], $format = "CSV")
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::insertBatchFiles() does not have parameter type hint for its parameter $table_name but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::insertBatchFiles() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$columns_array" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$columns_array"; expected 1 but found 0
Loading history...
Coding Style Comprehensibility introduced by
The string literal CSV does not require double quotes, as per coding-style, please use single quotes.

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

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

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

<?php

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

print $doubleQuoted;

will print an indented: Single is Value

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

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

Loading history...
612
    {
613 8
        if (is_string($file_names))
0 ignored issues
show
introduced by
Function is_string() should not be referenced via a fallback global name, but via a use statement.
Loading history...
614
        {
615
            $file_names = [$file_names];
616
        }
617 8
        if ($this->getCountPendingQueue() > 0) {
618
            throw new QueryException('Queue must be empty, before insertBatch, need executeAsync');
619
        }
620
621 8
        if (!in_array($format, $this->_support_format))
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
introduced by
Function in_array() should not be referenced via a fallback global name, but via a use statement.
Loading history...
622
        {
623
            throw new QueryException('Format not support in insertBatchFiles');
624
        }
625
626 8
        $result = [];
627
628 8
        foreach ($file_names as $fileName) {
629 8
            if (!is_file($fileName) || !is_readable($fileName)) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
introduced by
Function is_file() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Function is_readable() should not be referenced via a fallback global name, but via a use statement.
Loading history...
630
                throw  new QueryException('Cant read file: ' . $fileName . ' ' . (is_file($fileName) ? '' : ' is not file'));
0 ignored issues
show
introduced by
Function is_file() should not be referenced via a fallback global name, but via a use statement.
Loading history...
631
            }
632
633 8
            if (empty($columns_array))
634
            {
635
                $sql = 'INSERT INTO ' . $table_name . ' FORMAT ' . $format;
636
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
637
            } else
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after ELSE keyword; newline found
Loading history...
638
            {
639 8
                $sql = 'INSERT INTO ' . $table_name . ' ( ' . implode(',', $columns_array) . ' ) FORMAT ' . $format;
0 ignored issues
show
introduced by
Function implode() should not be referenced via a fallback global name, but via a use statement.
Loading history...
640
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
641
            }
642 8
            $result[$fileName] = $this->transport()->writeAsyncCSV($sql, $fileName);
643
        }
644
645
        // exec
646 8
        $this->executeAsync();
647
648
        // fetch resutl
649 8
        foreach ($file_names as $fileName) {
650 8
            if ($result[$fileName]->isError()) {
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
651 8
                $result[$fileName]->error();
652
            }
653
        }
654
655 6
        return $result;
656
    }
657
658
    /**
659
     * insert Batch Stream
660
     *
661
     * @param string $table_name
662
     * @param array $columns_array
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::insertBatchStream() does not specify type hint for items of its traversable parameter $columns_array.
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
663
     * @param string $format ['TabSeparated','TabSeparatedWithNames','CSV','CSVWithNames']
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter name; 1 found
Loading history...
664
     * @return Transport\CurlerRequest
665
     */
666 2
    public function insertBatchStream($table_name, $columns_array=[], $format = "CSV")
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::insertBatchStream() does not have parameter type hint for its parameter $table_name but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::insertBatchStream() does not have parameter type hint for its parameter $columns_array but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \ClickHouseDB\Client::insertBatchStream() does not have parameter type hint for its parameter $format but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::insertBatchStream() does not have return type hint for its return value but it should be possible to add it based on @return annotation "Transport\CurlerRequest".
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$columns_array" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$columns_array"; expected 1 but found 0
Loading history...
Coding Style Comprehensibility introduced by
The string literal CSV does not require double quotes, as per coding-style, please use single quotes.

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

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

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

<?php

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

print $doubleQuoted;

will print an indented: Single is Value

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

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

Loading history...
667
    {
668 2
        if ($this->getCountPendingQueue() > 0) {
669
            throw new QueryException('Queue must be empty, before insertBatch, need executeAsync');
670
        }
671
672 2
        if (!in_array($format, $this->_support_format))
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
introduced by
Function in_array() should not be referenced via a fallback global name, but via a use statement.
Loading history...
673
        {
674
            throw new QueryException('Format not support in insertBatchFiles');
675
        }
676
677 2
        if (empty($columns_array))
678
        {
679
            $sql = 'INSERT INTO ' . $table_name . ' FORMAT ' . $format;
680
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
681
        } else
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after ELSE keyword; newline found
Loading history...
682
        {
683 2
            $sql = 'INSERT INTO ' . $table_name . ' ( ' . implode(',', $columns_array) . ' ) FORMAT ' . $format;
0 ignored issues
show
introduced by
Function implode() should not be referenced via a fallback global name, but via a use statement.
Loading history...
684
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
685
        }
686
687 2
        return $this->transport()->writeStreamData($sql);
688
    }
689
690
691
    /**
692
     * stream Write
693
     *
694
     * @param Stream $stream
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::streamWrite() has useless @param annotation for parameter $stream.
Loading history...
695
     * @param string $sql
696
     * @param array $bind
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::streamWrite() does not specify type hint for items of its traversable parameter $bind.
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
697
     * @return Statement
698
     * @throws Exception\TransportException
699
     */
700 1
    public function streamWrite(Stream $stream,$sql,$bind=[])
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::streamWrite() 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\Client::streamWrite() does not have parameter type hint for its parameter $bind but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \ClickHouseDB\Client::streamWrite() 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
Expected 1 space between comma and argument "$sql"; 0 found
Loading history...
Coding Style introduced by
Expected 1 space between comma and argument "$bind"; 0 found
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$bind" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$bind"; expected 1 but found 0
Loading history...
701
    {
702 1
        if ($this->getCountPendingQueue() > 0) {
703
            throw new QueryException('Queue must be empty, before streamWrite');
704
        }
705 1
        return $this->transport()->streamWrite($stream,$sql,$bind);
706
    }
707
708
709
    /**
710
     * stream Read
711
     *
712
     * @param Stream $streamRead
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::streamRead() has useless @param annotation for parameter $streamRead.
Loading history...
713
     * @param string $sql
714
     * @param array $bind
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Client::streamRead() does not specify type hint for items of its traversable parameter $bind.
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
715
     * @return Statement
716
     * @throws Exception\TransportException
717
     */
718 1
    public function streamRead(Stream $streamRead,$sql,$bind=[])
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::streamRead() 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\Client::streamRead() does not have parameter type hint for its parameter $bind but it should be possible to add it based on @param annotation "array".
Loading history...
introduced by
Method \ClickHouseDB\Client::streamRead() 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
Expected 1 space between comma and argument "$sql"; 0 found
Loading history...
Coding Style introduced by
Expected 1 space between comma and argument "$bind"; 0 found
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$bind" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$bind"; expected 1 but found 0
Loading history...
719
    {
720 1
        if ($this->getCountPendingQueue() > 0) {
721
            throw new QueryException('Queue must be empty, before streamWrite');
722
        }
723 1
        return $this->transport()->streamRead($streamRead,$sql,$bind);
724
    }
725
726
    /**
727
     * Size of database
728
     *
729
     * @return mixed|null
730
     * @throws Exception\TransportException
731
     * @throws \Exception
732
     */
733
    public function databaseSize()
734
    {
735
        $b = $this->settings()->getDatabase();
736
737
        return $this->select('
738
            SELECT database,formatReadableSize(sum(bytes)) as size
739
            FROM system.parts
740
            WHERE active AND database=:database
741
            GROUP BY database
742
        ', ['database' => $b])->fetchOne();
743
    }
744
745
    /**
746
     * Size of tables
747
     *
748
     * @param string $tableName
749
     * @return mixed
750
     * @throws Exception\TransportException
751
     * @throws \Exception
752
     */
753 1
    public function tableSize($tableName)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::tableSize() does not have parameter type hint for its parameter $tableName but it should be possible to add it based on @param annotation "string".
Loading history...
754
    {
755 1
        $tables = $this->tablesSize();
756
757 1
        if (isset($tables[$tableName])) {
758 1
            return $tables[$tableName];
759
        }
760
761
        return null;
762
    }
763
764
    /**
765
     * Ping server
766
     *
767
     * @return bool
768
     * @throws Exception\TransportException
769
     */
770 37
    public function ping()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::ping() 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...
771
    {
772 37
        return $this->transport()->ping();
773
    }
774
775
    /**
776
     * Tables sizes
777
     *
778
     * @param bool $flatList
779
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Client::tablesSize() does not specify type hint for items of its traversable return value.
Loading history...
780
     * @throws Exception\TransportException
781
     * @throws \Exception
782
     */
783 1
    public function tablesSize($flatList = false)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::tablesSize() does not have parameter type hint for its parameter $flatList but it should be possible to add it based on @param annotation "bool".
Loading history...
introduced by
Method \ClickHouseDB\Client::tablesSize() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
784
    {
785 1
        $z = $this->select('
786
        SELECT name as table,database,
787
            max(sizebytes) as sizebytes,
788
            max(size) as size,
789
            min(min_date) as min_date,
790
            max(max_date) as max_date
791
            FROM system.tables
792
            ANY LEFT JOIN 
793
            (
794
            SELECT table,database,
795
                        formatReadableSize(sum(bytes)) as size,
796
                        sum(bytes) as sizebytes,
797
                        min(min_date) as min_date,
798
                        max(max_date) as max_date
799
                        FROM system.parts 
800
                        WHERE active AND database=:database
801
                        GROUP BY table,database
802
            ) USING ( table,database )
803
            WHERE database=:database
804
            GROUP BY table,database
805 1
        ', ['database'=>$this->settings()->getDatabase()]);
0 ignored issues
show
Coding Style introduced by
Expected 1 space between "'database'" and double arrow; 0 found
Loading history...
Coding Style introduced by
Expected 1 space between double arrow and "$this"; 0 found
Loading history...
806
807 1
        if ($flatList) {
808
            return $z->rows();
809
        }
810
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
811
812 1
        return $z->rowsAsTree('table');
813
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
814
815
    }
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...
816
817
818
    /**
819
     * isExists
820
     *
821
     * @param string $database
822
     * @param string $table
823
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Client::isExists() does not specify type hint for items of its traversable return value.
Loading history...
824
     * @throws Exception\TransportException
825
     * @throws \Exception
826
     */
827
    public function isExists($database, $table)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::isExists() does not have parameter type hint for its parameter $database but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::isExists() does not have parameter type hint for its parameter $table but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::isExists() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
828
    {
829
        return $this->select('
830
            SELECT *
831
            FROM system.tables 
832
            WHERE name=\''.$table . '\' AND database=\'' . $database . '\''
0 ignored issues
show
Coding Style introduced by
Concat operator must be surrounded by a single space
Loading history...
833
        )->rowsAsTree('name');
834
    }
835
836
837
    /**
838
     * List of partitions
839
     *
840
     * @return mixed[][]
841
     */
842
    public function partitions(string $table, int $limit = null, bool $active = null)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::partitions() does not have return type hint for its return value but it should be possible to add it based on @return annotation "mixed[][]".
Loading history...
introduced by
Parameter $limit has null default value, but is not marked as nullable.
Loading history...
introduced by
Parameter $active has null default value, but is not marked as nullable.
Loading history...
843
    {
844
        $database          = $this->settings()->getDatabase();
845
        $whereActiveClause = $active === null ? '' : sprintf(' AND active = %s', (int) $active);
846
        $limitClause       = $limit !== null ? ' LIMIT ' . $limit : '';
847
848
        return $this->select(<<<CLICKHOUSE
849
SELECT *
850
FROM system.parts 
851
WHERE like(table,'%$table%') AND database='$database'$whereActiveClause
852
ORDER BY max_date $limitClause
853
CLICKHOUSE
854
        )->rowsAsTree('name');
855
    }
856
857
    /**
858
     * dropPartition
859
     * @deprecated
860
     * @param string $dataBaseTableName database_name.table_name
861
     * @param string $partition_id
862
     * @return Statement
863
     * @throws Exception\TransportException
864
     */
865
    public function dropPartition($dataBaseTableName, $partition_id)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::dropPartition() does not have parameter type hint for its parameter $dataBaseTableName but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::dropPartition() does not have parameter type hint for its parameter $partition_id but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::dropPartition() 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...
866
    {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
867
868
        $partition_id = trim($partition_id, '\'');
0 ignored issues
show
introduced by
Function trim() should not be referenced via a fallback global name, but via a use statement.
Loading history...
869
        $this->settings()->set('replication_alter_partitions_sync', 2);
870
        $state = $this->write('ALTER TABLE {dataBaseTableName} DROP PARTITION :partion_id', [
871
            'dataBaseTableName'  => $dataBaseTableName,
872
            'partion_id' => $partition_id
0 ignored issues
show
introduced by
Multiline arrays must have a trailing comma after the last element.
Loading history...
873
        ]);
874
        return $state;
875
    }
876
877
    /**
878
     * Truncate ( drop all partitions )
879
     * @deprecated
880
     * @param string $tableName
881
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Client::truncateTable() does not specify type hint for items of its traversable return value.
Loading history...
882
     * @throws Exception\TransportException
883
     * @throws \Exception
884
     */
885
    public function truncateTable($tableName)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::truncateTable() does not have parameter type hint for its parameter $tableName but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::truncateTable() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
886
    {
887
        $partions = $this->partitions($tableName);
888
        $out = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

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

To visualize

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

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

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

will produce no issues.

Loading history...
889
        foreach ($partions as $part_key=>$part)
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected 1 space after "=>"; 0 found
Loading history...
890
        {
891
            $part_id = $part['partition'];
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...
892
            $out[$part_id] = $this->dropPartition($tableName, $part_id);
0 ignored issues
show
Deprecated Code introduced by
The function ClickHouseDB\Client::dropPartition() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

892
            $out[$part_id] = /** @scrutinizer ignore-deprecated */ $this->dropPartition($tableName, $part_id);
Loading history...
893
        }
894
        return $out;
895
    }
896
897
    /**
898
     * Returns the server's uptime in seconds.
899
     *
900
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Client::getServerUptime() does not specify type hint for items of its traversable return value.
Loading history...
901
     * @throws Exception\TransportException
902
     */
903 1
    public function getServerUptime()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::getServerUptime() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
904
    {
905 1
        return $this->select('SELECT uptime() as uptime')->fetchOne('uptime');
906
    }
907
908
    /**
909
     * Returns string with the server version.
910
     *
911
     * @throws Exception\TransportException
912
     */
913 1
    public function getServerVersion() : string
914
    {
915 1
        return (string) $this->select('SELECT version() as version')->fetchOne('version');
916
    }
917
918
919
    /**
920
     * Read system.settings table
921
     *
922
     * @param string $like
923
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Client::getServerSystemSettings() does not specify type hint for items of its traversable return value.
Loading history...
924
     * @throws Exception\TransportException
925
     */
926 1
    public function getServerSystemSettings($like='')
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::getServerSystemSettings() does not have parameter type hint for its parameter $like but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::getServerSystemSettings() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$like" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$like"; expected 1 but found 0
Loading history...
927
    {
928 1
        $l=[];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 0 spaces

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

To visualize

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

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

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

will produce no issues.

Loading history...
929 1
        $list=$this->select('SELECT * FROM system.settings'.($like ? ' WHERE name LIKE :like':'' ),['like'=>'%'.$like.'%'])->rows();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 1 space but found 0 spaces

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

To visualize

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

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

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

will produce no issues.

Loading history...
Coding Style introduced by
Concat operator must be surrounded by a single space
Loading history...
Coding Style introduced by
Expected 1 space between "'like'" and double arrow; 0 found
Loading history...
Coding Style introduced by
Expected 1 space between double arrow and "'%'"; 0 found
Loading history...
930 1
        foreach ($list as $row) {
931 1
            if (isset($row['name'])) {$n=$row['name']; unset($row['name']) ; $l[$n]=$row;}
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces
Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
932
        }
933 1
        return $l;
934
    }
935
936
937
938
    /**
939
     * dropOldPartitions by day_ago
940
     * @deprecated
941
     *
942
     * @param string $table_name
943
     * @param int $days_ago
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
944
     * @param int $count_partitons_per_one
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
945
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Client::dropOldPartitions() does not specify type hint for items of its traversable return value.
Loading history...
946
     * @throws Exception\TransportException
947
     * @throws \Exception
948
     */
949
    public function dropOldPartitions($table_name, $days_ago, $count_partitons_per_one = 100)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Client::dropOldPartitions() does not have parameter type hint for its parameter $table_name but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Client::dropOldPartitions() does not have parameter type hint for its parameter $days_ago but it should be possible to add it based on @param annotation "int".
Loading history...
introduced by
Method \ClickHouseDB\Client::dropOldPartitions() does not have parameter type hint for its parameter $count_partitons_per_one but it should be possible to add it based on @param annotation "int".
Loading history...
introduced by
Method \ClickHouseDB\Client::dropOldPartitions() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
950
    {
951
        $days_ago = strtotime(date('Y-m-d 00:00:00', strtotime('-' . $days_ago . ' day')));
0 ignored issues
show
introduced by
Function strtotime() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Function date() should not be referenced via a fallback global name, but via a use statement.
Loading history...
952
953
        $drop = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

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

To visualize

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

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

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

will produce no issues.

Loading history...
954
        $list_patitions = $this->partitions($table_name, $count_partitons_per_one);
955
956
        foreach ($list_patitions as $partion_id => $partition) {
957
            if (stripos($partition['engine'], 'mergetree') === false) {
0 ignored issues
show
introduced by
Function stripos() should not be referenced via a fallback global name, but via a use statement.
Loading history...
958
                continue;
959
            }
960
961
            // $min_date = strtotime($partition['min_date']);
962
            $max_date = strtotime($partition['max_date']);
0 ignored issues
show
introduced by
Function strtotime() should not be referenced via a fallback global name, but via a use statement.
Loading history...
963
964
            if ($max_date < $days_ago) {
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
965
                $drop[] = $partition['partition'];
966
            }
967
        }
968
969
        $result = [];
970
        foreach ($drop as $partition_id) {
971
            $result[$partition_id] = $this->dropPartition($table_name, $partition_id);
0 ignored issues
show
Deprecated Code introduced by
The function ClickHouseDB\Client::dropPartition() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

971
            $result[$partition_id] = /** @scrutinizer ignore-deprecated */ $this->dropPartition($table_name, $partition_id);
Loading history...
972
        }
973
974
        return $result;
975
    }
976
}
977