CurlerResponse   B
last analyzed

Complexity

Total Complexity 43

Size/Duplication

Total Lines 305
Duplicated Lines 0 %

Test Coverage

Coverage 52.27%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 66
dl 0
loc 305
ccs 46
cts 88
cp 0.5227
rs 8.96
c 2
b 0
f 0
wmc 43

28 Methods

Rating   Name   Duplication   Size   Complexity  
A error() 0 3 1
A content_type() 0 3 1
A error_no() 0 3 1
A as_string() 0 3 1
A dump_json() 0 3 1
A body() 0 3 1
A starttransfer_time() 0 3 1
A connect_time() 0 3 1
A connection() 0 3 1
A pretransfer_time() 0 3 1
A url() 0 3 1
A http_code() 0 3 1
A total_time() 0 3 1
A headers() 0 7 2
A rawDataOrJson() 0 10 3
A request_size() 0 3 1
A size_upload() 0 3 1
A size_download() 0 3 1
A header_size() 0 3 1
A upload_content_length() 0 3 1
A speed_upload() 0 4 1
A speed_download() 0 4 1
A json() 0 13 3
A info() 0 3 1
B humanFileSize() 0 13 10
A dump() 0 16 2
A getDetails() 0 7 1
A __construct() 0 1 1

How to fix   Complexity   

Complex Class

Complex classes like CurlerResponse often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CurlerResponse, and based on these observations, apply Extract Interface, too.

1
<?php
0 ignored issues
show
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;
0 ignored issues
show
Coding Style introduced by
Public member variable "_headers" must not contain a leading underscore
Loading history...
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;
0 ignored issues
show
Coding Style introduced by
Public member variable "_info" must not contain a leading underscore
Loading history...
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;
0 ignored issues
show
Coding Style introduced by
Public member variable "_error" must not contain a leading underscore
Loading history...
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;
0 ignored issues
show
Coding Style introduced by
Public member variable "_errorNo" must not contain a leading underscore
Loading history...
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;
0 ignored issues
show
Coding Style introduced by
Public member variable "_useTime" must not contain a leading underscore
Loading history...
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;
0 ignored issues
show
Coding Style introduced by
Public member variable "_body" must not contain a leading underscore
Loading history...
36
37
38
    /**
39
     * Response constructor.
0 ignored issues
show
introduced by
Documentation comment contains forbidden comment "Response constructor.".
Loading history...
40
     */
41 49
    public function __construct() {}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 2 found
Loading history...
Coding Style introduced by
Expected 1 blank line after function; 2 found
Loading history...
42
43
44
    /**
45
     * @return int
46
     */
47 39
    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 native return type hint for its return value but it should be possible to add it based on @return annotation "int".
Loading history...
48
    {
49 39
        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 native 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 native 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 native 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 49
    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 49
        return $this->_info['http_code'];
114
    }
115
116
    /**
117
     * @param string $name
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
118
     * @return null|string
0 ignored issues
show
introduced by
Null type hint should be on last position in "null|string".
Loading history...
119
     */
120
    public function headers($name)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::headers() does not have native 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 native return type hint for its return value but it should be possible to add it based on @return annotation "null|string".
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 in "null|string".
Loading history...
131
     */
132
    public function connection()
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::connection() does not have native 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 48
    public function body()
141
    {
142 48
        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
     */
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 1
    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...
162
    {
163
        return [
164 1
            'body'    => $this->_body,
165 1
            'headers' => $this->_headers,
166 1
            'error'   => $this->error(),
167 1
            'info'    => $this->_info,
168
        ];
169
    }
170
171
    /**
172
     * @param bool $result
0 ignored issues
show
introduced by
Incorrect annotations group.
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 native 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 native return type hint for its return value but it should be possible to add it based on @return annotation "string".
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 introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
195
     * @param string $unit
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 native 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 native 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 native return type hint for its return value but it should be possible to add it based on @return annotation "string".
Loading history...
199
    {
200 1
        if ((!$unit && $size >= 1 << 30) || $unit == 'GB') {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
introduced by
Operator == is disallowed, use === instead.
Loading history...
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
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
        }
203 1
        if ((!$unit && $size >= 1 << 20) || $unit == 'MB') {
0 ignored issues
show
introduced by
Expected 1 lines after "if", found 0.
Loading history...
introduced by
Operator == is disallowed, use === instead.
Loading history...
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
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
        }
206 1
        if ((!$unit && $size >= 1 << 10) || $unit == 'KB') {
0 ignored issues
show
introduced by
Operator == is disallowed, use === instead.
Loading history...
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
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 native 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 native 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'];
0 ignored issues
show
Coding Style introduced by
The variable $SPEED_UPLOAD should be in camel caps format.
Loading history...
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...
introduced by
Expected 1 lines before "return", found 0.
Loading history...
introduced by
Useless parentheses.
Loading history...
Coding Style introduced by
The variable $SPEED_UPLOAD should be in camel caps format.
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 native 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'];
0 ignored issues
show
Coding Style introduced by
The variable $SPEED_UPLOAD should be in camel caps format.
Loading history...
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...
introduced by
Expected 1 lines before "return", found 0.
Loading history...
introduced by
Useless parentheses.
Loading history...
Coding Style introduced by
The variable $SPEED_UPLOAD should be in camel caps format.
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 native 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 native 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 native 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 native 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
    }
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line after function; 0 found
Loading history...
278
    /**
279
     * @param string|null $key
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
280
     * @return bool|mixed
281
     */
282 30
    public function json($key = null)
0 ignored issues
show
introduced by
Method \ClickHouseDB\Transport\CurlerResponse::json() does not have native type hint for its parameter $key but it should be possible to add it based on @param annotation "string|null".
Loading history...
283
    {
284 30
        $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 30
        if (!$key) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
287 30
            return $d;
288
        }
289
290
        if (!isset($d[$key])) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after NOT operator; 0 found
Loading history...
291
            return false;
292
        }
293
294
        return $d[$key];
295
    }
296
297
    /**
298
     * @return mixed
299
     */
300 31
    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 31
        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...
introduced by
Expected 1 lines after "if", found 0.
Loading history...
305
        {
306 30
            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...
Coding Style introduced by
Expected at least 1 space before "==="; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "==="; 0 found
Loading history...
307 30
            return $this->json();
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
308
        }
309 2
        return $this->body();
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
introduced by
Expected 0 lines after "return", found 1.
Loading history...
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