Completed
Push — master ( 2e6852...bbcc62 )
by Igor
17s queued 11s
created

Statement::init()   B

Complexity

Conditions 11
Paths 30

Size

Total Lines 56
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 11.4402

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 27
c 1
b 0
f 0
nc 30
nop 0
dl 0
loc 56
ccs 22
cts 26
cp 0.8462
crap 11.4402
rs 7.3166

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
introduced by
Missing declare(strict_types=1).
Loading history...
introduced by
An error occurred during processing; checking has been aborted. The error message was: "if" without curly braces is not supported.
Loading history...
2
3
namespace ClickHouseDB;
4
5
use ClickHouseDB\Exception\ClickHouseUnavailableException;
6
use ClickHouseDB\Exception\DatabaseException;
7
use ClickHouseDB\Exception\QueryException;
8
use ClickHouseDB\Query\Query;
9
use ClickHouseDB\Transport\CurlerRequest;
10
use ClickHouseDB\Transport\CurlerResponse;
11
12
class Statement
13
{
14
    /**
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...
15
     * @var string|mixed
16
     */
17
    private $_rawData;
18
19
    /**
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...
20
     * @var int
21
     */
22
    private $_http_code = -1;
0 ignored issues
show
introduced by
Class Statement contains unused property $_http_code.
Loading history...
23
24
    /**
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...
25
     * @var CurlerRequest
26
     */
27
    private $_request = null;
28
29
    /**
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...
30
     * @var bool
31
     */
32
    private $_init = false;
33
34
    /**
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...
35
     * @var Query
36
     */
37
    private $query;
0 ignored issues
show
introduced by
Class Statement contains write-only property $query.
Loading history...
38
39
    /**
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...
40
     * @var mixed
41
     */
42
    private $format;
43
44
    /**
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...
45
     * @var string
46
     */
47
    private $sql = '';
48
49
    /**
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...
50
     * @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...
51
     */
52
    private $meta;
53
54
    /**
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...
55
     * @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...
56
     */
57
    private $totals;
58
59
    /**
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...
60
     * @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...
61
     */
62
    private $extremes;
63
64
    /**
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...
65
     * @var int
66
     */
67
    private $rows;
68
69
    /**
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...
70
     * @var bool|integer
0 ignored issues
show
introduced by
Expected "int" but found "integer" in @var annotation.
Loading history...
71
     */
72
    private $rows_before_limit_at_least = false;
73
74
    /**
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...
75
     * @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...
76
     */
77
    private $array_data = [];
78
79
    /**
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...
80
     * @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...
81
     */
82
    private $statistics = null;
83
84
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Statement::$iterator with single line content, use one-line comment instead.
Loading history...
85
     * @var int
86
     */
87
    public $iterator=0;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces
Loading history...
88
89
90 45
    public function __construct(CurlerRequest $request)
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function __construct()
Loading history...
91
    {
92 45
        $this->_request = $request;
93 45
        $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...
94 45
        $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...
95 45
        $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...
96 45
    }
97
98
    /**
99
     * @return CurlerRequest
100
     */
101
    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...
102
    {
103
        return $this->_request;
104
    }
105
106
    /**
107
     * @return CurlerResponse
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
108
     * @throws Exception\TransportException
0 ignored issues
show
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
109
     */
110 39
    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...
111
    {
112 39
        return $this->_request->response();
113
    }
114
115
    /**
116
     * @return mixed
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
117
     * @throws Exception\TransportException
0 ignored issues
show
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
118
     */
119 1
    public function responseInfo()
120
    {
121 1
        return $this->response()->info();
122
    }
123
124
    /**
125
     * @return mixed|string
126
     */
127 11
    public function sql()
128
    {
129 11
        return $this->sql;
130
    }
131
132
    /**
133
     * @param string $body
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
134
     * @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...
Coding Style introduced by
Expected "array|boolean" but found "array|bool" for function return type
Loading history...
135
     */
136 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...
Coding Style introduced by
Type hint "string" missing for $body
Loading history...
137
    {
138 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...
139 5
        $mathes = [];
140
141
        // Code: 115, e.displayText() = DB::Exception: Unknown setting readonly[0], e.what() = DB::Exception
142
        // Code: 192, e.displayText() = DB::Exception: Unknown user x, e.what() = DB::Exception
143
        // Code: 60, e.displayText() = DB::Exception: Table default.ZZZZZ doesn't exist., e.what() = DB::Exception
144
145 5
        if (preg_match("%Code: (\d+),\se\.displayText\(\) \=\s*DB\:\:Exception\s*:\s*(.*)(?:\,\s*e\.what|\(version).*%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...\.what|\(version).*%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...
146 5
            return ['code' => $mathes[1], 'message' => $mathes[2]];
0 ignored issues
show
Coding Style introduced by
Arrays with multiple values should not be declared on a single line.
Loading history...
147
        }
148
149
        return false;
150
    }
151
152
    /**
153
     * @return bool
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
154
     * @throws Exception\TransportException
0 ignored issues
show
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
155
     */
156 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...
157
    {
158 8
        if (!$this->isError()) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
159 2
            return false;
160
        }
161
162 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...
163 6
        $error_no = $this->response()->error_no();
164 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...
165
166 6
        if (!$error_no && !$error) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
167 5
            $parse = $this->parseErrorClickHouse($body);
168
169 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...
170 5
                throw new DatabaseException($parse['message'] . "\nIN:" . $this->sql(), $parse['code']);
171
            } else {
172
                $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...
173
                $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...
174
            }
175
        } else {
176 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...
177 1
            $message = $this->response()->error();
178
        }
179
180 1
        if ($code === CURLE_COULDNT_CONNECT) {
0 ignored issues
show
introduced by
Constant CURLE_COULDNT_CONNECT should not be referenced via a fallback global name, but via a use statement.
Loading history...
181
            throw new ClickHouseUnavailableException($message, $code);
182
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
183 1
        throw new QueryException($message, $code);
184
    }
185
186
    /**
187
     * @return bool
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
188
     * @throws Exception\TransportException
0 ignored issues
show
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
189
     */
190 39
    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...
191
    {
192 39
        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...
193
    }
194
195 32
    private function check() : bool
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function check()
Loading history...
196
    {
197 32
        if (!$this->_request->isResponseExists()) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
198
            throw QueryException::noResponse();
199
        }
200
201 32
        if ($this->isError()) {
202 1
            $this->error();
203
        }
204
205 31
        return true;
206
    }
207
208
    /**
209
     * @return bool
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
210
     * @throws Exception\TransportException
0 ignored issues
show
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
211
     */
212 30
    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...
213
    {
214 30
        if ($this->_init) {
215
            return false;
216
        }
217
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
218
219 30
        $this->check();
220
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
221
222 29
        $this->_rawData = $this->response()->rawDataOrJson($this->format);
223
224 29
        if (!$this->_rawData) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
225
            $this->_init = true;
226
            return false;
227
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
228 29
        $data=[];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces
Loading history...
229 29
        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...
230
231 29
            if (isset($this->_rawData[$key])) {
0 ignored issues
show
introduced by
Use early exit to reduce code nesting.
Loading history...
232 29
                if ($key=='data')
0 ignored issues
show
introduced by
Operator == is disallowed, use === instead.
Loading history...
233
                {
234 29
                    $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...
235
                }
236
                else{
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after ELSE keyword; 0 found
Loading history...
237 29
                    $this->{$key} = $this->_rawData[$key];
238
                }
0 ignored issues
show
Coding Style introduced by
Blank line found after control structure
Loading history...
239
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
240
            }
241
        }
242
243 29
        if (empty($this->meta)) {
244
            throw  new QueryException('Can`t find meta');
245
        }
246
247 29
        $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...
248 29
        $this->array_data = [];
249 29
        foreach ($data as $rows) {
250 29
            $r = [];
251
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
252
253 29
            if ($isJSONCompact)
254
            {
255 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...
256
            }
257
            else {
258 28
                foreach ($this->meta as $meta) {
259 28
                    $r[$meta['name']] = $rows[$meta['name']];
260
                }
261
            }
262
263 29
            $this->array_data[] = $r;
264
        }
265
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
266
267 29
        return true;
268
    }
269
270
    /**
271
     * @return array
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
introduced by
@return annotation of method \ClickHouseDB\Statement::extremes() does not specify type hint for items of its traversable return value.
Loading history...
272
     * @throws \Exception
0 ignored issues
show
introduced by
Class \Exception should not be referenced via a fully qualified name, but via a use statement.
Loading history...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
273
     */
274
    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...
275
    {
276
        $this->init();
277
        return $this->extremes;
278
    }
279
280
    /**
281
     * @return mixed
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
282
     * @throws Exception\TransportException
0 ignored issues
show
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
283
     */
284 1
    public function totalTimeRequest()
285
    {
286 1
        $this->check();
287 1
        return $this->response()->total_time();
288
289
    }
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...
290
291
    /**
292
     * @return array
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
introduced by
@return annotation of method \ClickHouseDB\Statement::extremesMin() does not specify type hint for items of its traversable return value.
Loading history...
293
     * @throws \Exception
0 ignored issues
show
introduced by
Class \Exception should not be referenced via a fully qualified name, but via a use statement.
Loading history...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
294
     */
295 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...
296
    {
297 1
        $this->init();
298
299 1
        if (empty($this->extremes['min'])) {
300
            return [];
301
        }
302
303 1
        return $this->extremes['min'];
304
    }
305
306
    /**
307
     * @return array
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
introduced by
@return annotation of method \ClickHouseDB\Statement::extremesMax() does not specify type hint for items of its traversable return value.
Loading history...
308
     * @throws \Exception
0 ignored issues
show
introduced by
Class \Exception should not be referenced via a fully qualified name, but via a use statement.
Loading history...
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
309
     */
310
    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...
311
    {
312
        $this->init();
313
314
        if (empty($this->extremes['max'])) {
315
            return [];
316
        }
317
318
        return $this->extremes['max'];
319
    }
320
321
    /**
322
     * @return array
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
introduced by
@return annotation of method \ClickHouseDB\Statement::totals() does not specify type hint for items of its traversable return value.
Loading history...
323
     * @throws Exception\TransportException
0 ignored issues
show
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
324
     */
325 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...
326
    {
327 1
        $this->init();
328 1
        return $this->totals;
329
    }
330
331
    /**
0 ignored issues
show
introduced by
Empty comment
Loading history...
332
     *
333
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
334
    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...
335
    {
336
        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...
337
    }
338
339
    /**
0 ignored issues
show
introduced by
Empty comment
Loading history...
340
     *
341
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
342
    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...
343
    {
344
        $this->_request->dump();
345
        $this->response()->dump();
346
    }
347
348
    /**
349
     * @return bool|int
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style introduced by
Expected "boolean|integer" but found "bool|int" for function return type
Loading history...
350
     * @throws Exception\TransportException
0 ignored issues
show
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
351
     */
352 2
    public function countAll()
353
    {
354 2
        $this->init();
355 2
        return $this->rows_before_limit_at_least;
356
    }
357
358
    /**
359
     * @param bool $key
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected "boolean" but found "bool" for parameter type
Loading history...
360
     * @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...
361
     * @throws Exception\TransportException
0 ignored issues
show
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
362
     */
363
    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...
Coding Style introduced by
Type hint "bool" missing for $key
Loading history...
364
    {
365
        $this->init();
366
367
        if (!is_array($this->statistics)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after 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...
368
            return null;
369
        }
370
371
        if (!$key) return $this->statistics;
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
372
373
        if (!isset($this->statistics[$key])) {
374
            return null;
375
        }
376
377
        return $this->statistics[$key];
378
379
    }
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...
380
381
    /**
382
     * @return int
383
     * @throws Exception\TransportException
384
     */
385 8
    public function count()
386
    {
387 8
        $this->init();
388 8
        return $this->rows;
389
    }
390
391
    /**
392
     * @return mixed|string
393
     * @throws Exception\TransportException
394
     */
395 3
    public function rawData()
396
    {
397 3
        if ($this->_init) {
398
            return $this->_rawData;
399
        }
400
401 3
        $this->check();
402
403 3
        return $this->response()->rawDataOrJson($this->format);
404
    }
405
406
    /**
407
     *
408
     */
409 2
    public function resetIterator()
410
    {
411 2
        $this->iterator=0;
412 2
    }
413
414 2
    public function fetchRow($key = null)
415
    {
416 2
        $this->init();
417
418 2
        $position=$this->iterator;
419
420 2
        if (!isset($this->array_data[$position])) {
421
            return null;
422
        }
423
424 2
        $this->iterator++;
425
426 2
        if (!$key) {
427
            return $this->array_data[$position];
428
        }
429 2
        if (!isset($this->array_data[$position][$key])) {
430 1
            return null;
431
        }
432
433 2
        return $this->array_data[$position][$key];
434
435
    }
436
    /**
437
     * @param string $key
438
     * @return mixed|null
439
     * @throws Exception\TransportException
440
     */
441 18
    public function fetchOne($key = null)
442
    {
443 18
        $this->init();
444 18
        if (!isset($this->array_data[0])) {
445
            return null;
446
        }
447
448 18
        if (!$key) {
449 3
            return $this->array_data[0];
450
        }
451
452 18
        if (!isset($this->array_data[0][$key])) {
453 2
            return null;
454
        }
455
456 17
        return $this->array_data[0][$key];
457
    }
458
459
    /**
460
     * @param string|null $path
461
     * @return array
462
     * @throws Exception\TransportException
463
     */
464 4
    public function rowsAsTree($path)
465
    {
466 4
        $this->init();
467
468 4
        $out = [];
469 4
        foreach ($this->array_data as $row) {
470 4
            $d = $this->array_to_tree($row, $path);
471 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...
472
        }
473
474 4
        return $out;
475
    }
476
477
    /**
478
     * Return size_upload,upload_content,speed_upload,time_request
479
     *
480
     * @return array
481
     * @throws Exception\TransportException
482
     */
483
    public function info_upload()
484
    {
485
        $this->check();
486
        return [
487
            'size_upload'    => $this->response()->size_upload(),
488
            'upload_content' => $this->response()->upload_content_length(),
489
            'speed_upload'   => $this->response()->speed_upload(),
490
            'time_request'   => $this->response()->total_time()
491
        ];
492
    }
493
494
    /**
495
     * Return size_upload,upload_content,speed_upload,time_request,starttransfer_time,size_download,speed_download
496
     *
497
     * @return array
498
     * @throws Exception\TransportException
499
     */
500 1
    public function info()
501
    {
502 1
        $this->check();
503
        return [
504 1
            'starttransfer_time'    => $this->response()->starttransfer_time(),
505 1
            'size_download'    => $this->response()->size_download(),
506 1
            'speed_download'    => $this->response()->speed_download(),
507 1
            'size_upload'    => $this->response()->size_upload(),
508 1
            'upload_content' => $this->response()->upload_content_length(),
509 1
            'speed_upload'   => $this->response()->speed_upload(),
510 1
            'time_request'   => $this->response()->total_time()
511
        ];
512
    }
513
514
    /**
515
     * get format in sql
516
     * @return mixed
517
     */
518 1
    public function getFormat()
519
    {
520 1
        return $this->format;
521
    }
522
523
    /**
524
     * @return array
525
     * @throws Exception\TransportException
526
     */
527 9
    public function rows()
528
    {
529 9
        $this->init();
530 8
        return $this->array_data;
531
    }
532
533
    /**
534
     * @param array|string $arr
535
     * @param null|string|array $path
536
     * @return array
537
     */
538 4
    private function array_to_tree($arr, $path = null)
539
    {
540 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...
541
            $keys = $path;
542
        } else {
543 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...
544 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...
545
546 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...
547 4
                $separator = '.';
548 4
                $keys = explode($separator, $path);
0 ignored issues
show
introduced by
Function explode() should not be referenced via a fallback global name, but via a use statement.
Loading history...
549
            } else {
550
                $keys = $args;
551
            }
552
        }
553
554
        //
555 4
        $tree = $arr;
556 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...
557 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...
558
559 4
            if (isset($arr[$key])) {
560 4
                $val = $arr[$key];
561
            } else {
562
                $val = $key;
563
            }
564
565 4
            $tree = array($val => $tree);
566
        }
567 4
        if (!is_array($tree)) {
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...
568
            return [];
569
        }
570 4
        return $tree;
571
    }
572
}
573