Passed
Pull Request — master (#94)
by Šimon
04:26
created

Statement::fetchOne()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 1
dl 0
loc 17
ccs 8
cts 9
cp 0.8889
crap 4.0218
rs 9.9666
c 0
b 0
f 0
1
<?php
0 ignored issues
show
introduced by
Missing declare(strict_types = 1).
Loading history...
2
3
namespace ClickHouseDB;
4
5
use ClickHouseDB\Exception\DatabaseException;
6
use ClickHouseDB\Exception\QueryException;
7
use ClickHouseDB\Query\Query;
8
use ClickHouseDB\Transport\CurlerRequest;
9
use ClickHouseDB\Transport\CurlerResponse;
10
11
class Statement
12
{
13
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Statement::$_rawData with single line content, use one-line comment instead.
Loading history...
14
     * @var string|mixed
15
     */
16
    private $_rawData;
17
18
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Statement::$_http_code with single line content, use one-line comment instead.
Loading history...
19
     * @var int
20
     */
21
    private $_http_code = -1;
0 ignored issues
show
introduced by
Class Statement contains unused property $_http_code.
Loading history...
22
23
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Statement::$_request with single line content, use one-line comment instead.
Loading history...
24
     * @var CurlerRequest
25
     */
26
    private $_request = null;
27
28
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Statement::$_init with single line content, use one-line comment instead.
Loading history...
29
     * @var bool
30
     */
31
    private $_init = false;
32
33
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Statement::$query with single line content, use one-line comment instead.
Loading history...
34
     * @var Query
35
     */
36
    private $query;
0 ignored issues
show
introduced by
Class Statement contains write-only property $query.
Loading history...
37
38
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Statement::$format with single line content, use one-line comment instead.
Loading history...
39
     * @var mixed
40
     */
41
    private $format;
42
43
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Statement::$sql with single line content, use one-line comment instead.
Loading history...
44
     * @var string
45
     */
46
    private $sql = '';
47
48
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Statement::$meta with single line content, use one-line comment instead.
Loading history...
49
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \ClickHouseDB\Statement::$meta does not specify type hint for its items.
Loading history...
50
     */
51
    private $meta;
52
53
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Statement::$totals with single line content, use one-line comment instead.
Loading history...
54
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \ClickHouseDB\Statement::$totals does not specify type hint for its items.
Loading history...
55
     */
56
    private $totals;
57
58
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Statement::$extremes with single line content, use one-line comment instead.
Loading history...
59
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \ClickHouseDB\Statement::$extremes does not specify type hint for its items.
Loading history...
60
     */
61
    private $extremes;
62
63
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Statement::$rows with single line content, use one-line comment instead.
Loading history...
64
     * @var int
65
     */
66
    private $rows;
67
68
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Statement::$rows_before_limit_at_least with single line content, use one-line comment instead.
Loading history...
69
     * @var bool|integer
0 ignored issues
show
introduced by
Expected "int" but found "integer" in @var annotation.
Loading history...
70
     */
71
    private $rows_before_limit_at_least = false;
72
73
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Statement::$array_data with single line content, use one-line comment instead.
Loading history...
74
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \ClickHouseDB\Statement::$array_data does not specify type hint for its items.
Loading history...
75
     */
76
    private $array_data = [];
77
78
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Statement::$statistics with single line content, use one-line comment instead.
Loading history...
79
     * @var array|null
0 ignored issues
show
introduced by
@var annotation of property \ClickHouseDB\Statement::$statistics does not specify type hint for its items.
Loading history...
80
     */
81
    private $statistics = null;
82
83
84 40
    public function __construct(CurlerRequest $request)
85
    {
86 40
        $this->_request = $request;
87 40
        $this->format = $this->_request->getRequestExtendedInfo('format');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

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

To visualize

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

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

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

will produce no issues.

Loading history...
88 40
        $this->query = $this->_request->getRequestExtendedInfo('query');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

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

To visualize

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

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

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

will produce no issues.

Loading history...
89 40
        $this->sql = $this->_request->getRequestExtendedInfo('sql');
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...
90 40
    }
91
92
    /**
93
     * @return CurlerRequest
94
     */
95
    public function getRequest()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Statement::getRequest() 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...
96
    {
97
        return $this->_request;
98
    }
99
100
    /**
101
     * @return CurlerResponse
102
     * @throws Exception\TransportException
103
     */
104 36
    private function response()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Statement::response() does not have return type hint for its return value but it should be possible to add it based on @return annotation "CurlerResponse".
Loading history...
105
    {
106 36
        return $this->_request->response();
107
    }
108
109
    /**
110
     * @return mixed
111
     * @throws Exception\TransportException
112
     */
113 1
    public function responseInfo()
114
    {
115 1
        return $this->response()->info();
116
    }
117
118
    /**
119
     * @return mixed|string
120
     */
121 9
    public function sql()
122
    {
123 9
        return $this->sql;
124
    }
125
126
    /**
127
     * @param string $body
128
     * @return array|bool
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Statement::parseErrorClickHouse() does not specify type hint for items of its traversable return value.
Loading history...
129
     */
130 5
    private function parseErrorClickHouse($body)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Statement::parseErrorClickHouse() does not have parameter type hint for its parameter $body but it should be possible to add it based on @param annotation "string".
Loading history...
131
    {
132 5
        $body = trim($body);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

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

To visualize

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

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

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

will produce no issues.

Loading history...
introduced by
Function trim() should not be referenced via a fallback global name, but via a use statement.
Loading history...
133 5
        $mathes = [];
134
135
        // Code: 115, e.displayText() = DB::Exception: Unknown setting readonly[0], e.what() = DB::Exception
136
        // Code: 192, e.displayText() = DB::Exception: Unknown user x, e.what() = DB::Exception
137
        // Code: 60, e.displayText() = DB::Exception: Table default.ZZZZZ doesn't exist., e.what() = DB::Exception
138
139 5
        if (preg_match("%Code: (\d+),\se\.displayText\(\) \=\s*DB\:\:Exception\s*:\s*(.*)\,\s*e\.what.*%ius", $body, $mathes)) {
0 ignored issues
show
introduced by
Function preg_match() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Coding Style Comprehensibility introduced by
The string literal %Code: (\d+),\se\.displa...*(.*)\,\s*e\.what.*%ius 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...
140 5
            return ['code' => $mathes[1], 'message' => $mathes[2]];
141
        }
142
143
        return false;
144
    }
145
146
    /**
147
     * @return bool
148
     * @throws Exception\TransportException
149
     */
150 8
    public function error()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Statement::error() 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...
151
    {
152 8
        if (!$this->isError()) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
153 2
            return false;
154
        }
155
156 6
        $body = $this->response()->body();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

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

To visualize

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

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

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

will produce no issues.

Loading history...
157 6
        $error_no = $this->response()->error_no();
158 6
        $error = $this->response()->error();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

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

To visualize

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

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

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

will produce no issues.

Loading history...
159
160 6
        if (!$error_no && !$error) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
161 5
            $parse = $this->parseErrorClickHouse($body);
162
163 5
            if ($parse) {
0 ignored issues
show
introduced by
$parse is a non-empty array, thus is always true.
Loading history...
Bug Best Practice introduced by
The expression $parse of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
164 5
                throw new DatabaseException($parse['message'] . "\nIN:" . $this->sql(), $parse['code']);
165
            } else {
166
                $code = $this->response()->http_code();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

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

To visualize

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

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

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

will produce no issues.

Loading history...
167
                $message = "HttpCode:" . $this->response()->http_code() . " ; " . $this->response()->error() . " ;" . $body;
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal HttpCode: 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 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...
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...
168
            }
169
        } else {
170 1
            $code = $error_no;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

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

To visualize

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

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

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

will produce no issues.

Loading history...
171 1
            $message = $this->response()->error();
172
        }
173
174 1
        throw new QueryException($message, $code);
175
    }
176
177
    /**
178
     * @return bool
179
     * @throws Exception\TransportException
180
     */
181 36
    public function isError()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Statement::isError() 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...
182
    {
183 36
        return ($this->response()->http_code() !== 200 || $this->response()->error_no());
0 ignored issues
show
introduced by
Usage of language construct "return" with parentheses is disallowed.
Loading history...
184
    }
185
186
    /**
187
     * @return bool
188
     * @throws Exception\TransportException
189
     */
190 29
    private function check()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Statement::check() 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...
191
    {
192 29
        if (!$this->_request->isResponseExists()) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
193
            throw new QueryException('Not have response');
194
        }
195
196 29
        if ($this->isError()) {
197 1
            $this->error();
198
        }
199
200 28
        return true;
201
    }
202
203
    /**
204
     * @return bool
205
     * @throws Exception\TransportException
206
     */
207 27
    private function init()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Statement::init() 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...
208
    {
209 27
        if ($this->_init) {
210
            return false;
211
        }
212
213 27
        $this->check();
214
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
215
216 26
        $this->_rawData = $this->response()->rawDataOrJson($this->format);
217
218 26
        if (!$this->_rawData) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
219
            $this->_init = true;
220
            return false;
221
        }
222 26
        $data=[];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces
Loading history...
223 26
        foreach (['meta', 'data', 'totals', 'extremes', 'rows', 'rows_before_limit_at_least', 'statistics'] as $key) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
224
225 26
            if (isset($this->_rawData[$key])) {
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
226 26
                if ($key=='data')
0 ignored issues
show
introduced by
Operator == is disallowed, use === instead.
Loading history...
227
                {
228 26
                    $data=$this->_rawData[$key];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces
Loading history...
229
                }
230
                else{
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after ELSE keyword; 0 found
Loading history...
231 26
                    $this->{$key} = $this->_rawData[$key];
232
                }
233
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
234
            }
235
        }
236
237 26
        if (empty($this->meta)) {
238
            throw  new QueryException('Can`t find meta');
239
        }
240
241 26
        $isJSONCompact=(stripos($this->format,'JSONCompact')!==false?true:false);
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...
introduced by
Function stripos() should not be referenced via a fallback global name, but via a use statement.
Loading history...
242 26
        $this->array_data = [];
243 26
        foreach ($data as $rows) {
244 26
            $r = [];
245
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
246
247 26
            if ($isJSONCompact)
248
            {
249 1
                $r[]=$rows;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces
Loading history...
250
            }
251
            else {
252 25
                foreach ($this->meta as $meta) {
253 25
                    $r[$meta['name']] = $rows[$meta['name']];
254
                }
255
            }
256
257 26
            $this->array_data[] = $r;
258
        }
259
260 26
        return true;
261
    }
262
263
    /**
264
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Statement::extremes() does not specify type hint for items of its traversable return value.
Loading history...
265
     * @throws \Exception
266
     */
267
    public function extremes()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Statement::extremes() 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...
268
    {
269
        $this->init();
270
        return $this->extremes;
271
    }
272
273
    /**
274
     * @return mixed
275
     * @throws Exception\TransportException
276
     */
277 1
    public function totalTimeRequest()
278
    {
279 1
        $this->check();
280 1
        return $this->response()->total_time();
281
282
    }
0 ignored issues
show
Coding Style introduced by
Function closing brace must go on the next line following the body; found 1 blank lines before brace
Loading history...
283
284
    /**
285
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Statement::extremesMin() does not specify type hint for items of its traversable return value.
Loading history...
286
     * @throws \Exception
287
     */
288 1
    public function extremesMin()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Statement::extremesMin() 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...
289
    {
290 1
        $this->init();
291
292 1
        if (empty($this->extremes['min'])) {
293
            return [];
294
        }
295
296 1
        return $this->extremes['min'];
297
    }
298
299
    /**
300
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Statement::extremesMax() does not specify type hint for items of its traversable return value.
Loading history...
301
     * @throws \Exception
302
     */
303
    public function extremesMax()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Statement::extremesMax() 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...
304
    {
305
        $this->init();
306
307
        if (empty($this->extremes['max'])) {
308
            return [];
309
        }
310
311
        return $this->extremes['max'];
312
    }
313
314
    /**
315
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Statement::totals() does not specify type hint for items of its traversable return value.
Loading history...
316
     * @throws Exception\TransportException
317
     */
318 1
    public function totals()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Statement::totals() 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...
319
    {
320 1
        $this->init();
321 1
        return $this->totals;
322
    }
323
324
    /**
0 ignored issues
show
introduced by
Empty comment
Loading history...
325
     *
326
     */
327
    public function dumpRaw()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Statement::dumpRaw() does not have void return type hint.
Loading history...
introduced by
Method \ClickHouseDB\Statement::dumpRaw() does not need documentation comment.
Loading history...
328
    {
329
        print_r($this->_rawData);
0 ignored issues
show
introduced by
Function print_r() should not be referenced via a fallback global name, but via a use statement.
Loading history...
330
    }
331
332
    /**
0 ignored issues
show
introduced by
Empty comment
Loading history...
333
     *
334
     */
335
    public function dump()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Statement::dump() does not have void return type hint.
Loading history...
introduced by
Method \ClickHouseDB\Statement::dump() does not need documentation comment.
Loading history...
336
    {
337
        $this->_request->dump();
338
        $this->response()->dump();
339
    }
340
341
    /**
342
     * @return bool|int
343
     * @throws Exception\TransportException
344
     */
345 2
    public function countAll()
346
    {
347 2
        $this->init();
348 2
        return $this->rows_before_limit_at_least;
349
    }
350
351
    /**
352
     * @param bool $key
353
     * @return array|mixed|null
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Statement::statistics() does not specify type hint for items of its traversable return value.
Loading history...
354
     * @throws Exception\TransportException
355
     */
356
    public function statistics($key = false)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Statement::statistics() does not have parameter type hint for its parameter $key but it should be possible to add it based on @param annotation "bool".
Loading history...
357
    {
358
        $this->init();
359
        if ($key)
360
        {
361
            if (!is_array($this->statistics)) {
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_array() should not be referenced via a fallback global name, but via a use statement.
Loading history...
362
                return null;
363
            }
364
            if (!isset($this->statistics[$key])) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
365
                return null;
366
            }
367
            return $this->statistics[$key];
368
        }
369
        return $this->statistics;
370
    }
371
372
    /**
373
     * @return int
374
     * @throws Exception\TransportException
375
     */
376 8
    public function count()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Statement::count() 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...
377
    {
378 8
        $this->init();
379 8
        return $this->rows;
380
    }
381
382
    /**
383
     * @return mixed|string
384
     * @throws Exception\TransportException
385
     */
386 3
    public function rawData()
387
    {
388 3
        if ($this->_init) {
389
            return $this->_rawData;
390
        }
391
392 3
        $this->check();
393
394 3
        return $this->response()->rawDataOrJson($this->format);
395
    }
396
397
    /**
398
     * @param string $key
399
     * @return mixed|null
400
     * @throws Exception\TransportException
401
     */
402 15
    public function fetchOne($key = '')
0 ignored issues
show
introduced by
Method \ClickHouseDB\Statement::fetchOne() does not have parameter type hint for its parameter $key but it should be possible to add it based on @param annotation "string".
Loading history...
403
    {
404 15
        $this->init();
405
406 15
        if (isset($this->array_data[0])) {
407 15
            if ($key) {
408 15
                if (isset($this->array_data[0][$key])) {
409 14
                    return $this->array_data[0][$key];
410
                } else {
0 ignored issues
show
introduced by
Remove useless else to reduce code nesting.
Loading history...
411 1
                    return null;
412
                }
413
            }
414
415 2
            return $this->array_data[0];
416
        }
417
418
        return null;
419
    }
420
421
    /**
422
     * @param string|null $path
423
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Statement::rowsAsTree() does not specify type hint for items of its traversable return value.
Loading history...
424
     * @throws Exception\TransportException
425
     */
426 4
    public function rowsAsTree($path)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Statement::rowsAsTree() does not have parameter type hint for its parameter $path but it should be possible to add it based on @param annotation "string|null".
Loading history...
introduced by
Method \ClickHouseDB\Statement::rowsAsTree() 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...
427
    {
428 4
        $this->init();
429
430 4
        $out = [];
431 4
        foreach ($this->array_data as $row) {
432 4
            $d = $this->array_to_tree($row, $path);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

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

To visualize

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

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

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

will produce no issues.

Loading history...
433 4
            $out = array_replace_recursive($d, $out);
0 ignored issues
show
introduced by
Function array_replace_recursive() should not be referenced via a fallback global name, but via a use statement.
Loading history...
434
        }
435
436 4
        return $out;
437
    }
438
439
    /**
440
     * Return size_upload,upload_content,speed_upload,time_request
441
     *
442
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Statement::info_upload() does not specify type hint for items of its traversable return value.
Loading history...
443
     * @throws Exception\TransportException
444
     */
445
    public function info_upload()
0 ignored issues
show
Coding Style introduced by
Method name "Statement::info_upload" is not in camel caps format
Loading history...
introduced by
Method \ClickHouseDB\Statement::info_upload() 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...
446
    {
447
        $this->check();
448
        return [
449
            'size_upload'    => $this->response()->size_upload(),
450
            'upload_content' => $this->response()->upload_content_length(),
451
            'speed_upload'   => $this->response()->speed_upload(),
452
            'time_request'   => $this->response()->total_time()
0 ignored issues
show
Coding Style introduced by
Each line in an array declaration must end in a comma
Loading history...
introduced by
MultiLine arrays must have a trailing comma after the last element.
Loading history...
453
        ];
454
    }
455
456
    /**
457
     * Return size_upload,upload_content,speed_upload,time_request,starttransfer_time,size_download,speed_download
458
     *
459
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Statement::info() does not specify type hint for items of its traversable return value.
Loading history...
460
     * @throws Exception\TransportException
461
     */
462 1
    public function info()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Statement::info() 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...
463
    {
464 1
        $this->check();
465
        return [
466 1
            'starttransfer_time'    => $this->response()->starttransfer_time(),
467 1
            'size_download'    => $this->response()->size_download(),
468 1
            'speed_download'    => $this->response()->speed_download(),
469 1
            'size_upload'    => $this->response()->size_upload(),
470 1
            'upload_content' => $this->response()->upload_content_length(),
471 1
            'speed_upload'   => $this->response()->speed_upload(),
472 1
            'time_request'   => $this->response()->total_time()
0 ignored issues
show
introduced by
MultiLine arrays must have a trailing comma after the last element.
Loading history...
473
        ];
474
    }
475
476
    /**
477
     * get format in sql
478
     * @return mixed
479
     */
480 1
    public function getFormat()
481
    {
482 1
        return $this->format;
483
    }
484
485
    /**
486
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Statement::rows() does not specify type hint for items of its traversable return value.
Loading history...
487
     * @throws Exception\TransportException
488
     */
489 9
    public function rows()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Statement::rows() 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...
490
    {
491 9
        $this->init();
492 8
        return $this->array_data;
493
    }
494
495
    /**
496
     * @param array|string $arr
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Statement::array_to_tree() does not specify type hint for items of its traversable parameter $arr.
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
497
     * @param null|string|array $path
0 ignored issues
show
introduced by
@param annotation of method \ClickHouseDB\Statement::array_to_tree() does not specify type hint for items of its traversable parameter $path.
Loading history...
498
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \ClickHouseDB\Statement::array_to_tree() does not specify type hint for items of its traversable return value.
Loading history...
499
     */
500 4
    private function array_to_tree($arr, $path = null)
0 ignored issues
show
Coding Style introduced by
Method name "Statement::array_to_tree" is not in camel caps format
Loading history...
introduced by
Method \ClickHouseDB\Statement::array_to_tree() 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...
501
    {
502 4
        if (is_array($path)) {
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...
503
            $keys = $path;
504
        } else {
505 4
            $args = func_get_args();
0 ignored issues
show
introduced by
Function func_get_args() should not be referenced via a fallback global name, but via a use statement.
Loading history...
506 4
            array_shift($args);
0 ignored issues
show
introduced by
Function array_shift() should not be referenced via a fallback global name, but via a use statement.
Loading history...
507
508 4
            if (sizeof($args) < 2) {
0 ignored issues
show
introduced by
Function sizeof() should not be referenced via a fallback global name, but via a use statement.
Loading history...
Coding Style introduced by
The use of function sizeof() is forbidden; use count() instead
Loading history...
509 4
                $separator = '.';
510 4
                $keys = explode($separator, $path);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

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

To visualize

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

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

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

will produce no issues.

Loading history...
introduced by
Function explode() should not be referenced via a fallback global name, but via a use statement.
Loading history...
511
            } else {
512
                $keys = $args;
513
            }
514
        }
515
516
        //
0 ignored issues
show
introduced by
Empty comment
Loading history...
517 4
        $tree = $arr;
518 4
        while (count($keys)) {
0 ignored issues
show
introduced by
Function count() should not be referenced via a fallback global name, but via a use statement.
Loading history...
519 4
            $key = array_pop($keys);
0 ignored issues
show
introduced by
Function array_pop() should not be referenced via a fallback global name, but via a use statement.
Loading history...
520
521 4
            if (isset($arr[$key])) {
522 4
                $val = $arr[$key];
523
            } else {
524
                $val = $key;
525
            }
526
527 4
            $tree = array($val => $tree);
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
528
        }
529 4
        if (!is_array($tree)) {
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_array() should not be referenced via a fallback global name, but via a use statement.
Loading history...
530
            return [];
531
        }
532 4
        return $tree;
533
    }
534
}
535