Failed Conditions
Push — master ( 806629...ea64d8 )
by Igor
13s queued 11s
created

CurlerResponse::connection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 0
cts 2
cp 0
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
0 ignored issues
show
introduced by
An error occurred during processing; checking has been aborted. The error message was: "if" without curly braces is not supported.
Loading history...
introduced by
Missing declare(strict_types=1).
Loading history...
2
3
namespace ClickHouseDB\Transport;
4
5
class CurlerResponse
6
{
7
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\CurlerResponse::$_headers with single line content, use one-line comment instead.
Loading history...
8
     * @var mixed
9
     */
10
    public $_headers;
11
12
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\CurlerResponse::$_info with single line content, use one-line comment instead.
Loading history...
13
     * @var mixed
14
     */
15
    public $_info;
16
17
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\CurlerResponse::$_error with single line content, use one-line comment instead.
Loading history...
18
     * @var mixed
19
     */
20
    public $_error;
21
22
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\CurlerResponse::$_errorNo with single line content, use one-line comment instead.
Loading history...
23
     * @var int
24
     */
25
    public $_errorNo = 0;
26
27
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\CurlerResponse::$_useTime with single line content, use one-line comment instead.
Loading history...
28
     * @var float
29
     */
30
    public $_useTime;
31
32
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \ClickHouseDB\Transport\CurlerResponse::$_body with single line content, use one-line comment instead.
Loading history...
33
     * @var string
34
     */
35
    public $_body;
36
37
38
    /**
39
     * Response constructor.
0 ignored issues
show
introduced by
Documentation comment contains forbidden comment "Response constructor.".
Loading history...
40
     */
41
    public function __construct() {}
42
43
44
    /**
45
     * @return int
0 ignored issues
show
Coding Style introduced by
Expected "integer" but found "int" for function return type
Loading history...
46
     */
47 38
    public function error_no()
0 ignored issues
show
Coding Style introduced by
Method name "CurlerResponse::error_no" is not in camel caps format
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::error_no() 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...
48
    {
49 38
        return $this->_errorNo;
50
    }
51
52
    /**
53
     * @return mixed
54
     */
55 6
    public function error()
56
    {
57 6
        return $this->_error;
58
    }
59
60
    /**
61
     * @return mixed
62
     */
63
    public function url()
64
    {
65
        return $this->_info['url'];
66
    }
67
68
    /**
69
     * @return mixed
70
     */
71 2
    public function total_time()
0 ignored issues
show
Coding Style introduced by
Method name "CurlerResponse::total_time" is not in camel caps format
Loading history...
72
    {
73 2
        return round($this->_info['total_time'], 3);
0 ignored issues
show
introduced by
Function round() should not be referenced via a fallback global name, but via a use statement.
Loading history...
74
    }
75
76
    /**
77
     * @return string
78
     */
79 1
    public function starttransfer_time()
0 ignored issues
show
Coding Style introduced by
Method name "CurlerResponse::starttransfer_time" is not in camel caps format
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::starttransfer_time() 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...
80
    {
81 1
        return round($this->_info['starttransfer_time'], 3);
0 ignored issues
show
introduced by
Function round() should not be referenced via a fallback global name, but via a use statement.
Loading history...
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function connect_time()
0 ignored issues
show
Coding Style introduced by
Method name "CurlerResponse::connect_time" is not in camel caps format
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::connect_time() 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...
88
    {
89
        return round($this->_info['connect_time'], 3);
0 ignored issues
show
introduced by
Function round() should not be referenced via a fallback global name, but via a use statement.
Loading history...
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function pretransfer_time()
0 ignored issues
show
Coding Style introduced by
Method name "CurlerResponse::pretransfer_time" is not in camel caps format
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::pretransfer_time() 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...
96
    {
97
        return round($this->_info['pretransfer_time'], 3);
0 ignored issues
show
introduced by
Function round() should not be referenced via a fallback global name, but via a use statement.
Loading history...
98
    }
99
100
    /**
101
     * @return mixed
102
     */
103
    public function content_type()
0 ignored issues
show
Coding Style introduced by
Method name "CurlerResponse::content_type" is not in camel caps format
Loading history...
104
    {
105
        return $this->_info['content_type'];
106
    }
107
108
    /**
109
     * @return mixed
110
     */
111 48
    public function http_code()
0 ignored issues
show
Coding Style introduced by
Method name "CurlerResponse::http_code" is not in camel caps format
Loading history...
112
    {
113 48
        return $this->_info['http_code'];
114
    }
115
116
    /**
117
     * @param string $name
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
118
     * @return null|string
0 ignored issues
show
introduced by
Null type hint should be on last position.
Loading history...
119
     */
120
    public function headers($name)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::headers() does not have parameter type hint for its parameter $name but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::headers() does not have return type hint for its return value but it should be possible to add it based on @return annotation "null|string".
Loading history...
Coding Style introduced by
Type hint "string" missing for $name
Loading history...
121
    {
122
        if (isset($this->_headers[$name])) {
123
            return $this->_headers[$name];
124
        }
125
126
        return null;
127
    }
128
129
    /**
130
     * @return null|string
0 ignored issues
show
introduced by
Null type hint should be on last position.
Loading history...
131
     */
132
    public function connection()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::connection() does not have return type hint for its return value but it should be possible to add it based on @return annotation "null|string".
Loading history...
133
    {
134
        return $this->headers('Connection');
135
    }
136
137
    /**
138
     * @return mixed
139
     */
140 47
    public function body()
141
    {
142 47
        return $this->_body;
143
    }
144
145
    /**
146
     * @return mixed
147
     */
148
    public function as_string()
0 ignored issues
show
Coding Style introduced by
Method name "CurlerResponse::as_string" is not in camel caps format
Loading history...
149
    {
150
        return $this->body();
151
    }
152
153
    /**
0 ignored issues
show
introduced by
Empty comment
Loading history...
154
     *
155
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
156
    public function dump_json()
0 ignored issues
show
Coding Style introduced by
Method name "CurlerResponse::dump_json" is not in camel caps format
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::dump_json() does not have void return type hint.
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::dump_json() does not need documentation comment.
Loading history...
157
    {
158
        print_r($this->json());
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...
159
    }
160
161
    public function getDetails(): array
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::getDetails() does not have @return annotation for its traversable return value.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function getDetails()
Loading history...
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
162
    {
163
        return [
164
            'body'    => $this->_body,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 16 spaces, but found 12.
Loading history...
165
            'headers' => $this->_headers,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 16 spaces, but found 12.
Loading history...
166
            'error'   => $this->error(),
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 16 spaces, but found 12.
Loading history...
167
            'info'    => $this->_info,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 16 spaces, but found 12.
Loading history...
168
        ];
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 15 space(s), but found 8.
Loading history...
169
    }
170
171
    /**
172
     * @param bool $result
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...
173
     * @return string
174
     */
175
    public function dump($result = false)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::dump() does not have parameter type hint for its parameter $result but it should be possible to add it based on @param annotation "bool".
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::dump() 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...
Coding Style introduced by
Type hint "bool" missing for $result
Loading history...
176
    {
177
        $msg = "\n--------------------------- Response -------------------------------------\nBODY:\n";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 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...
178
        $msg .= print_r($this->_body, true);
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...
179
        $msg .= "\nHEAD:\n";
180
        $msg .= print_r($this->_headers, true);
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...
181
        $msg .= "\nERROR:\n" . $this->error();
182
        $msg .= "\nINFO:\n";
183
        $msg .= json_encode($this->_info);
0 ignored issues
show
introduced by
Function json_encode() should not be referenced via a fallback global name, but via a use statement.
Loading history...
184
        $msg .= "\n----------------------------------------------------------------------\n";
185
186
        if ($result) {
187
            return $msg;
188
        }
189
190
        echo $msg;
191
    }
192
193
    /**
194
     * @param int $size
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 "integer" but found "int" for parameter type
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
195
     * @param string $unit
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
196
     * @return string
197
     */
198 1
    private function humanFileSize($size, $unit = '')
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::humanFileSize() does not have parameter type hint for its parameter $size but it should be possible to add it based on @param annotation "int".
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::humanFileSize() does not have parameter type hint for its parameter $unit but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::humanFileSize() 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...
Coding Style introduced by
Type hint "int" missing for $size
Loading history...
Coding Style introduced by
Type hint "string" missing for $unit
Loading history...
199
    {
200 1
        if ((!$unit && $size >= 1 << 30) || $unit == 'GB') {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
introduced by
Operator == is disallowed, use === instead.
Loading history...
201
            return number_format($size / (1 << 30), 2) . ' GB';
0 ignored issues
show
introduced by
Function number_format() should not be referenced via a fallback global name, but via a use statement.
Loading history...
202
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
203 1
        if ((!$unit && $size >= 1 << 20) || $unit == 'MB') {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
introduced by
Operator == is disallowed, use === instead.
Loading history...
204
            return number_format($size / (1 << 20), 2) . ' MB';
0 ignored issues
show
introduced by
Function number_format() should not be referenced via a fallback global name, but via a use statement.
Loading history...
205
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
206 1
        if ((!$unit && $size >= 1 << 10) || $unit == 'KB') {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
introduced by
Operator == is disallowed, use === instead.
Loading history...
207
            return number_format($size / (1 << 10), 2) . ' KB';
0 ignored issues
show
introduced by
Function number_format() should not be referenced via a fallback global name, but via a use statement.
Loading history...
208
        }
209
210 1
        return number_format($size) . ' bytes';
0 ignored issues
show
introduced by
Function number_format() should not be referenced via a fallback global name, but via a use statement.
Loading history...
211
    }
212
213
    /**
214
     * @return string
215
     */
216 1
    public function upload_content_length()
0 ignored issues
show
Coding Style introduced by
Method name "CurlerResponse::upload_content_length" is not in camel caps format
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::upload_content_length() 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...
217
    {
218 1
        return $this->humanFileSize($this->_info['upload_content_length']);
219
    }
220
221
    /**
222
     * @return string
223
     */
224 1
    public function speed_upload()
0 ignored issues
show
Coding Style introduced by
Method name "CurlerResponse::speed_upload" is not in camel caps format
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::speed_upload() 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...
225
    {
226 1
        $SPEED_UPLOAD = $this->_info['speed_upload'];
227 1
        return round(($SPEED_UPLOAD * 8) / (1000 * 1000), 2) . ' Mbps';
0 ignored issues
show
introduced by
Function round() should not be referenced via a fallback global name, but via a use statement.
Loading history...
228
    }
229
230
    /**
231
     * @return string
232
     */
233 1
    public function speed_download()
0 ignored issues
show
Coding Style introduced by
Method name "CurlerResponse::speed_download" is not in camel caps format
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::speed_download() 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 1
        $SPEED_UPLOAD = $this->_info['speed_download'];
236 1
        return round(($SPEED_UPLOAD * 8) / (1000 * 1000), 2) . ' Mbps';
0 ignored issues
show
introduced by
Function round() should not be referenced via a fallback global name, but via a use statement.
Loading history...
237
    }
238
239
    /**
240
     * @return string
241
     */
242 1
    public function size_upload()
0 ignored issues
show
Coding Style introduced by
Method name "CurlerResponse::size_upload" is not in camel caps format
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::size_upload() 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...
243
    {
244 1
        return $this->humanFileSize($this->_info['size_upload']);
245
    }
246
247
    /**
248
     * @return string
249
     */
250
    public function request_size()
0 ignored issues
show
Coding Style introduced by
Method name "CurlerResponse::request_size" is not in camel caps format
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::request_size() 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...
251
    {
252
        return $this->humanFileSize($this->_info['request_size']);
253
    }
254
255
    /**
256
     * @return string
257
     */
258
    public function header_size()
0 ignored issues
show
Coding Style introduced by
Method name "CurlerResponse::header_size" is not in camel caps format
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::header_size() 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...
259
    {
260
        return $this->humanFileSize($this->_info['header_size']);
261
    }
262
263
    /**
264
     * @return string
265
     */
266 1
    public function size_download()
0 ignored issues
show
Coding Style introduced by
Method name "CurlerResponse::size_download" is not in camel caps format
Loading history...
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::size_download() 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...
267
    {
268 1
        return $this->humanFileSize($this->_info['size_download']);
269
    }
270
271
    /**
272
     * @return mixed
273
     */
274 1
    public function info()
275
    {
276 1
        return $this->_info;
277
    }
278
    /**
279
     * @param string|null $key
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
280
     * @return bool|mixed
0 ignored issues
show
Coding Style introduced by
Expected "boolean|mixed" but found "bool|mixed" for function return type
Loading history...
281
     */
282 29
    public function json($key = null)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::json() does not have parameter type hint for its parameter $key but it should be possible to add it based on @param annotation "string|null".
Loading history...
283
    {
284 29
        $d = json_decode($this->body(), true);
0 ignored issues
show
introduced by
Function json_decode() should not be referenced via a fallback global name, but via a use statement.
Loading history...
285
286 29
        if (!$key) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
287 29
            return $d;
288
        }
289
290
        if (!isset($d[$key])) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
291
            return false;
292
        }
293
294
        return $d[$key];
295
    }
296
297
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$format" missing
Loading history...
298
     * @return mixed
299
     */
300 30
    public function rawDataOrJson($format)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::rawDataOrJson() does not have parameter type hint nor @param annotation for its parameter $format.
Loading history...
301
    {
302
        // JSONCompact // JSONEachRow
303
304 30
        if (stripos($format, 'json') !== 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...
305
        {
306 29
            if (stripos($format,'JSONEachRow')===false)
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...
introduced by
Function stripos() should not be referenced via a fallback global name, but via a use statement.
Loading history...
307 29
            return $this->json();
308
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
309 2
        return $this->body();
310
311
    }
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...
312
}
313