Conditions | 16 |
Paths | 2592 |
Total Lines | 75 |
Lines | 6 |
Ratio | 8 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
66 | public function buildOptions(Request $request, array $options) |
||
67 | { |
||
68 | $headers = []; |
||
69 | View Code Duplication | foreach ($request->getHeaders() as $key => $values) { |
|
70 | $headers[] = $key . ': ' . implode(', ', $values); |
||
71 | } |
||
72 | |||
73 | $out = [ |
||
74 | CURLOPT_URL => (string)$request->getUri(), |
||
75 | CURLOPT_HTTP_VERSION => $this->getProtocolVersion($request), |
||
76 | CURLOPT_RETURNTRANSFER => true, |
||
77 | CURLOPT_HEADER => true, |
||
78 | CURLOPT_HTTPHEADER => $headers, |
||
79 | ]; |
||
80 | switch ($request->getMethod()) { |
||
81 | case Request::METHOD_GET: |
||
82 | $out[CURLOPT_HTTPGET] = true; |
||
83 | break; |
||
84 | |||
85 | case Request::METHOD_POST: |
||
86 | $out[CURLOPT_POST] = true; |
||
87 | break; |
||
88 | |||
89 | default: |
||
90 | $out[CURLOPT_POST] = true; |
||
91 | $out[CURLOPT_CUSTOMREQUEST] = $request->getMethod(); |
||
92 | break; |
||
93 | } |
||
94 | |||
95 | $body = $request->getBody(); |
||
96 | if ($body) { |
||
97 | $body->rewind(); |
||
98 | $out[CURLOPT_POSTFIELDS] = $body->getContents(); |
||
99 | // GET requests with bodies require custom request to be used. |
||
100 | if (isset($out[CURLOPT_HTTPGET])) { |
||
101 | $out[CURLOPT_CUSTOMREQUEST] = 'get'; |
||
102 | } |
||
103 | } |
||
104 | |||
105 | View Code Duplication | if (empty($options['ssl_cafile'])) { |
|
106 | $options['ssl_cafile'] = CORE_PATH . 'config' . DIRECTORY_SEPARATOR . 'cacert.pem'; |
||
107 | } |
||
108 | if (!empty($options['ssl_verify_host'])) { |
||
109 | // Value of 1 or true is deprecated. Only 2 or 0 should be used now. |
||
110 | $options['ssl_verify_host'] = 2; |
||
111 | } |
||
112 | $optionMap = [ |
||
113 | 'timeout' => CURLOPT_TIMEOUT, |
||
114 | 'ssl_verify_peer' => CURLOPT_SSL_VERIFYPEER, |
||
115 | 'ssl_verify_host' => CURLOPT_SSL_VERIFYHOST, |
||
116 | 'ssl_cafile' => CURLOPT_CAINFO, |
||
117 | 'ssl_local_cert' => CURLOPT_SSLCERT, |
||
118 | 'ssl_passphrase' => CURLOPT_SSLCERTPASSWD, |
||
119 | ]; |
||
120 | foreach ($optionMap as $option => $curlOpt) { |
||
121 | if (isset($options[$option])) { |
||
122 | $out[$curlOpt] = $options[$option]; |
||
123 | } |
||
124 | } |
||
125 | if (isset($options['proxy']['proxy'])) { |
||
126 | $out[CURLOPT_PROXY] = $options['proxy']['proxy']; |
||
127 | } |
||
128 | if (isset($options['proxy']['username'])) { |
||
129 | $password = !empty($options['proxy']['password']) ? $options['proxy']['password'] : ''; |
||
130 | $out[CURLOPT_PROXYUSERPWD] = $options['proxy']['username'] . ':' . $password; |
||
131 | } |
||
132 | if (isset($options['curl']) && is_array($options['curl'])) { |
||
133 | // Can't use array_merge() because keys will be re-ordered. |
||
134 | foreach ($options['curl'] as $key => $value) { |
||
135 | $out[$key] = $value; |
||
136 | } |
||
137 | } |
||
138 | |||
139 | return $out; |
||
140 | } |
||
141 | |||
197 |