Total Complexity | 42 |
Total Lines | 312 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CURL 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 CURL, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class CURL extends AbstractClient |
||
20 | { |
||
21 | /** |
||
22 | * __construct. |
||
23 | * |
||
24 | * @param (string) $url |
||
25 | * (string) $method |
||
26 | * (array) $options |
||
27 | * |
||
28 | * @since 1.0.0 |
||
29 | */ |
||
30 | public function __construct($url, $method = 'GET', array $options = null) |
||
31 | { |
||
32 | if (!function_exists('curl_init')) { |
||
33 | throw new \Exception('Error: cURL is not available.'); |
||
34 | } |
||
35 | $this->resource = curl_init(); |
||
|
|||
36 | |||
37 | $this->setUrl($url); |
||
38 | $this->setMethod($method); |
||
39 | $this->setOption(CURLOPT_URL, $this->url); |
||
40 | $this->setOption(CURLOPT_HEADER, true); |
||
41 | $this->setOption(CURLOPT_RETURNTRANSFER, true); |
||
42 | |||
43 | if ($options !== null) { |
||
44 | $this->setOptions($options); |
||
45 | } |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Set the method. |
||
50 | * |
||
51 | * @param (string) $method |
||
52 | * |
||
53 | * @since 1.0.0 |
||
54 | * |
||
55 | * @return object |
||
56 | */ |
||
57 | public function setMethod($method) |
||
58 | { |
||
59 | parent::setMethod($method); |
||
60 | if ($method !== 'GET') { |
||
61 | switch ($method) { |
||
62 | case 'POST': |
||
63 | $this->setOption(CURLOPT_POST, true); |
||
64 | break; |
||
65 | default: |
||
66 | $this->setOption(CURLOPT_CUSTOMREQUEST, $this->method); |
||
67 | break; |
||
68 | } |
||
69 | } |
||
70 | |||
71 | return $this; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Return cURL resource (alias to $this->getResource()). |
||
76 | * |
||
77 | * @since 1.0.0 |
||
78 | * |
||
79 | * @return resource |
||
80 | */ |
||
81 | public function curl() |
||
82 | { |
||
83 | return $this->resource; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Create and open cURL resource. |
||
88 | * |
||
89 | * @since 1.0.0 |
||
90 | * |
||
91 | * @return object |
||
92 | */ |
||
93 | public function open() |
||
94 | { |
||
95 | $url = $this->url; |
||
96 | $headers = []; |
||
97 | |||
98 | // Set query data if there is any |
||
99 | if (count($this->fields) > 0) { |
||
100 | if ($this->method == 'GET') { |
||
101 | $url = $this->options[CURLOPT_URL].'?'.$this->getQuery(); |
||
102 | $this->setOption(CURLOPT_URL, $url); |
||
103 | } else { |
||
104 | if (isset($this->requestHeaders['Content-Type']) && ($this->requestHeaders['Content-Type'] != 'multipart/form-data')) { |
||
105 | $this->setOption(CURLOPT_POSTFIELDS, $this->getQuery()); |
||
106 | $this->setRequestHeader('Content-Length', $this->getQueryLength()); |
||
107 | } else { |
||
108 | $this->setOption(CURLOPT_POSTFIELDS, $this->fields); |
||
109 | } |
||
110 | $this->setOption(CURLOPT_POSTFIELDS, $this->fields); |
||
111 | $this->setOption(CURLOPT_URL, $url); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | if ($this->hasRequestHeaders()) { |
||
116 | foreach ($this->requestHeaders as $header => $value) { |
||
117 | $headers[] = $header.': '.$value; |
||
118 | } |
||
119 | $this->setOption(CURLOPT_HTTPHEADER, $headers); |
||
120 | } |
||
121 | |||
122 | $this->response = curl_exec($this->resource); |
||
123 | |||
124 | if ($this->response === false) { |
||
125 | $this->throwError('Error: '.curl_errno($this->resource).' => '.curl_error($this->resource).'.'); |
||
126 | } |
||
127 | |||
128 | return $this; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Set curl session option. |
||
133 | * |
||
134 | * @param (mixed) $option |
||
135 | * (mixed) $value |
||
136 | * |
||
137 | * @since 1.0.0 |
||
138 | * |
||
139 | * @return object |
||
140 | */ |
||
141 | public function setOption($option, $value) |
||
142 | { |
||
143 | $this->options[$option] = $value; |
||
144 | curl_setopt($this->resource, $option, $value); |
||
145 | |||
146 | return $this; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Set curl session options. |
||
151 | * |
||
152 | * @param (array) $options |
||
153 | * |
||
154 | * @since 1.0.0 |
||
155 | * |
||
156 | * @return object |
||
157 | */ |
||
158 | public function setOptios($options) |
||
159 | { |
||
160 | foreach ($options as $option => $value) { |
||
161 | $this->setOption($option, $value); |
||
162 | } |
||
163 | curl_setopt_array($this->resource, $options); |
||
164 | |||
165 | return $this; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Set curl return header. |
||
170 | * |
||
171 | * @param (bool) $header |
||
172 | * |
||
173 | * @since 1.0.0 |
||
174 | * |
||
175 | * @return object |
||
176 | */ |
||
177 | public function setReturnHeader($header = true) |
||
178 | { |
||
179 | $this->setOption(CURLOPT_HEADER, (bool) $header); |
||
180 | |||
181 | return $this; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Set curl return tranfer header. |
||
186 | * |
||
187 | * @param (bool) $header |
||
188 | * |
||
189 | * @since 1.0.0 |
||
190 | * |
||
191 | * @return object |
||
192 | */ |
||
193 | public function setReturnTransfer($header = true) |
||
194 | { |
||
195 | $this->setOption(CURLOPT_RETURNTRANSFER, (bool) $header); |
||
196 | |||
197 | return $this; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Set curl return header. |
||
202 | * |
||
203 | * @since 1.0.0 |
||
204 | * |
||
205 | * @return bool |
||
206 | */ |
||
207 | public function isReturnHeader() |
||
208 | { |
||
209 | return isset($this->options[CURLOPT_HEADER]) && ($this->options[CURLOPT_HEADER] == true); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Set curl return tranfer header. |
||
214 | * |
||
215 | * @since 1.0.0 |
||
216 | * |
||
217 | * @return bool |
||
218 | */ |
||
219 | public function isReturnTransfer() |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Get curl session option. |
||
226 | * |
||
227 | * @param (mixes) $option |
||
228 | * |
||
229 | * @since 1.0.0 |
||
230 | * |
||
231 | * @return mixes |
||
232 | */ |
||
233 | public function getOption($option) |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Get curl last session info. |
||
240 | * |
||
241 | * @param (mixes) $option |
||
242 | * |
||
243 | * @since 1.0.0 |
||
244 | * |
||
245 | * @return mixes |
||
246 | */ |
||
247 | public function getInfo($option = null) |
||
248 | { |
||
249 | return ($option !== null) ? curl_getinfo($this->resource, $option) : curl_getinfo($this->resource); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Method to send the request and get the response. |
||
254 | * |
||
255 | * @since 1.0.0 |
||
256 | * |
||
257 | * @return void |
||
258 | */ |
||
259 | public function send() |
||
280 | } |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Return the cURL version. |
||
285 | * |
||
286 | * @since 1.0.0 |
||
287 | * |
||
288 | * @return array |
||
289 | */ |
||
290 | public function version() |
||
291 | { |
||
292 | return curl_version(); |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Close the cURL connection. |
||
297 | * |
||
298 | * @since 1.0.0 |
||
299 | * |
||
300 | * @return void |
||
301 | */ |
||
302 | public function close() |
||
306 | } |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Parse headers. |
||
311 | * |
||
312 | * @since 1.0.0 |
||
313 | * |
||
314 | * @return void |
||
315 | */ |
||
316 | protected function parseResponseHeaders() |
||
336 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.