RequestOptions::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 8
nc 1
nop 8
dl 0
loc 18
rs 10
c 1
b 0
f 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Qiniu\Http;
4
5
use Qiniu\Http\Middleware\Middleware;
6
7
final class RequestOptions
8
{
9
10
    /**
11
     * @var int|null
12
     * http 请求的超时时间,单位:秒,默认:0,不超时
13
     */
14
    public $connection_timeout;
15
16
    /**
17
     * @var int|null
18
     * http 请求的超时时间,单位:毫秒,默认:0,不超时
19
     */
20
    public $connection_timeout_ms;
21
22
    /**
23
     * @var int|null
24
     * http 请求的超时时间,单位:秒,默认:0,不超时
25
     */
26
    public $timeout;
27
28
29
    /**
30
     * @var int|null
31
     * http 请求的超时时间,单位:毫秒,默认:0,不超时
32
     */
33
    public $timeout_ms;
34
35
    /**
36
     * @var string|null
37
     * 代理URL,默认:空
38
     */
39
    public $proxy;
40
41
    /**
42
     * @var int|null
43
     * 代理鉴权方式,默认:空
44
     */
45
    public $proxy_auth;
46
47
    /**
48
     * @var string|null
49
     * 代理鉴权参数,默认:空
50
     */
51
    public $proxy_user_password;
52
53
    /**
54
     * @var array<Middleware>
55
     */
56
    public $middlewares;
57
58
    public function __construct(
59
        $connection_timeout = null,
60
        $connection_timeout_ms = null,
61
        $timeout = null,
62
        $timeout_ms = null,
63
        $middlewares = array(),
64
        $proxy = null,
65
        $proxy_auth = null,
66
        $proxy_user_password = null
67
    ) {
68
        $this->connection_timeout = $connection_timeout;
69
        $this->connection_timeout_ms = $connection_timeout_ms;
70
        $this->timeout = $timeout;
71
        $this->timeout_ms = $timeout_ms;
72
        $this->proxy = $proxy;
73
        $this->proxy_auth = $proxy_auth;
74
        $this->proxy_user_password = $proxy_user_password;
75
        $this->middlewares = $middlewares;
76
    }
77
78
    public function getCurlOpt()
79
    {
80
        $result = array();
81
        if ($this->connection_timeout != null) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $this->connection_timeout of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
82
            $result[CURLOPT_CONNECTTIMEOUT] = $this->connection_timeout;
83
        }
84
        if ($this->connection_timeout_ms != null) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $this->connection_timeout_ms of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
85
            $result[CURLOPT_CONNECTTIMEOUT_MS] = $this->connection_timeout_ms;
86
        }
87
        if ($this->timeout != null) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $this->timeout of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
88
            $result[CURLOPT_TIMEOUT] = $this->timeout;
89
        }
90
        if ($this->timeout_ms != null) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $this->timeout_ms of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
91
            $result[CURLOPT_TIMEOUT_MS] = $this->timeout_ms;
92
        }
93
        if ($this->proxy != null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->proxy of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
94
            $result[CURLOPT_PROXY] = $this->proxy;
95
        }
96
        if ($this->proxy_auth != null) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $this->proxy_auth of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
97
            $result[CURLOPT_PROXYAUTH] = $this->proxy_auth;
98
        }
99
        if ($this->proxy_user_password != null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->proxy_user_password of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
100
            $result[CURLOPT_PROXYUSERPWD] = $this->proxy_user_password;
101
        }
102
        return $result;
103
    }
104
}
105