Complex classes like TwitterOAuth 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 TwitterOAuth, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class TwitterOAuth extends Config |
||
18 | { |
||
19 | const API_VERSION = '1.1'; |
||
20 | const API_HOST = 'https://api.twitter.com'; |
||
21 | const UPLOAD_HOST = 'https://upload.twitter.com'; |
||
22 | |||
23 | /** @var Response details about the result of the last request */ |
||
24 | private $response; |
||
25 | /** @var string|null Application bearer token */ |
||
26 | private $bearer; |
||
27 | /** @var Consumer Twitter application details */ |
||
28 | private $consumer; |
||
29 | /** @var Token|null User access token details */ |
||
30 | private $token; |
||
31 | /** @var HmacSha1 OAuth 1 signature type used by Twitter */ |
||
32 | private $signatureMethod; |
||
33 | /** @var int Number of attempts we made for the request */ |
||
34 | private $attempts = 0; |
||
35 | |||
36 | /** |
||
37 | * Constructor. |
||
38 | * |
||
39 | * @param string $consumerKey The Application Consumer Key |
||
40 | * @param string $consumerSecret The Application Consumer Secret |
||
41 | * @param string|null $oauthToken The Client Token (optional) |
||
42 | * @param string|null $oauthTokenSecret The Client Token Secret (optional) |
||
43 | */ |
||
44 | public function __construct($consumerKey, $consumerSecret, $oauthToken = null, $oauthTokenSecret = null) |
||
45 | { |
||
46 | $this->resetLastResponse(); |
||
47 | $this->signatureMethod = new HmacSha1(); |
||
48 | $this->consumer = new Consumer($consumerKey, $consumerSecret); |
||
49 | if (!empty($oauthToken) && !empty($oauthTokenSecret)) { |
||
50 | $this->setOauthToken($oauthToken, $oauthTokenSecret); |
||
51 | } |
||
52 | if (empty($oauthToken) && !empty($oauthTokenSecret)) { |
||
53 | $this->setBearer($oauthTokenSecret); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param string $oauthToken |
||
59 | * @param string $oauthTokenSecret |
||
60 | */ |
||
61 | public function setOauthToken($oauthToken, $oauthTokenSecret) |
||
62 | { |
||
63 | $this->token = new Token($oauthToken, $oauthTokenSecret); |
||
64 | $this->bearer = null; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param string $oauthTokenSecret |
||
69 | */ |
||
70 | public function setBearer($oauthTokenSecret) |
||
71 | { |
||
72 | $this->bearer = $oauthTokenSecret; |
||
73 | $this->token = null; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @return string|null |
||
78 | */ |
||
79 | public function getLastApiPath() |
||
80 | { |
||
81 | return $this->response->getApiPath(); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @return int |
||
86 | */ |
||
87 | public function getLastHttpCode() |
||
88 | { |
||
89 | return $this->response->getHttpCode(); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @return array |
||
94 | */ |
||
95 | public function getLastXHeaders() |
||
96 | { |
||
97 | return $this->response->getXHeaders(); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @return array|object|null |
||
102 | */ |
||
103 | public function getLastBody() |
||
104 | { |
||
105 | return $this->response->getBody(); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Resets the last response cache. |
||
110 | */ |
||
111 | public function resetLastResponse() |
||
112 | { |
||
113 | $this->response = new Response(); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Resets the attempts number. |
||
118 | */ |
||
119 | private function resetAttemptsNumber() |
||
120 | { |
||
121 | $this->attempts = 0; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Delays the retries when they're activated. |
||
126 | */ |
||
127 | private function sleepIfNeeded() |
||
128 | { |
||
129 | if ($this->maxRetries && $this->attempts) { |
||
130 | sleep($this->retriesDelay); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Make URLs for user browser navigation. |
||
136 | * |
||
137 | * @param string $path |
||
138 | * @param array $parameters |
||
139 | * |
||
140 | * @return string |
||
141 | */ |
||
142 | public function url($path, array $parameters) |
||
143 | { |
||
144 | $this->resetLastResponse(); |
||
145 | $this->response->setApiPath($path); |
||
146 | $query = http_build_query($parameters); |
||
147 | |||
148 | return sprintf('%s/%s?%s', self::API_HOST, $path, $query); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Make /oauth/* requests to the API. |
||
153 | * |
||
154 | * @param string $path |
||
155 | * @param array $parameters |
||
156 | * |
||
157 | * @throws TwitterOAuthException |
||
158 | * |
||
159 | * @return array |
||
160 | */ |
||
161 | public function oauth($path, array $parameters = []) |
||
162 | { |
||
163 | $response = []; |
||
164 | $this->resetLastResponse(); |
||
165 | $this->response->setApiPath($path); |
||
166 | $url = sprintf('%s/%s', self::API_HOST, $path); |
||
167 | $result = $this->oAuthRequest($url, 'POST', $parameters); |
||
168 | |||
169 | if ($this->getLastHttpCode() != 200) { |
||
170 | throw new TwitterOAuthException($result); |
||
171 | } |
||
172 | |||
173 | parse_str($result, $response); |
||
174 | $this->response->setBody($response); |
||
|
|||
175 | |||
176 | return $response; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Make /oauth2/* requests to the API. |
||
181 | * |
||
182 | * @param string $path |
||
183 | * @param array $parameters |
||
184 | * |
||
185 | * @return array|object |
||
186 | */ |
||
187 | public function oauth2($path, array $parameters = []) |
||
188 | { |
||
189 | $method = 'POST'; |
||
190 | $this->resetLastResponse(); |
||
191 | $this->response->setApiPath($path); |
||
192 | $url = sprintf('%s/%s', self::API_HOST, $path); |
||
193 | $request = Request::fromConsumerAndToken($this->consumer, $this->token, $method, $url, $parameters); |
||
194 | $authorization = 'Authorization: Basic '.$this->encodeAppAuthorization($this->consumer); |
||
195 | $result = $this->request($request->getNormalizedHttpUrl(), $method, $authorization, $parameters); |
||
196 | $response = JsonDecoder::decode($result, $this->decodeJsonAsArray); |
||
197 | $this->response->setBody($response); |
||
198 | |||
199 | return $response; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Make GET requests to the API. |
||
204 | * |
||
205 | * @param string $path |
||
206 | * @param array $parameters |
||
207 | * |
||
208 | * @return array|object |
||
209 | */ |
||
210 | public function get($path, array $parameters = []) |
||
211 | { |
||
212 | return $this->http('GET', self::API_HOST, $path, $parameters); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Make POST requests to the API. |
||
217 | * |
||
218 | * @param string $path |
||
219 | * @param array $parameters |
||
220 | * @param string $host |
||
221 | * |
||
222 | * @return array|object |
||
223 | */ |
||
224 | public function post($path, array $parameters = [], $host = self::API_HOST) |
||
225 | { |
||
226 | return $this->http('POST', $host, $path, $parameters); |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Make DELETE requests to the API. |
||
231 | * |
||
232 | * @param string $path |
||
233 | * @param array $parameters |
||
234 | * |
||
235 | * @return array|object |
||
236 | */ |
||
237 | public function delete($path, array $parameters = []) |
||
238 | { |
||
239 | return $this->http('DELETE', self::API_HOST, $path, $parameters); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Make PUT requests to the API. |
||
244 | * |
||
245 | * @param string $path |
||
246 | * @param array $parameters |
||
247 | * |
||
248 | * @return array|object |
||
249 | */ |
||
250 | public function put($path, array $parameters = []) |
||
251 | { |
||
252 | return $this->http('PUT', self::API_HOST, $path, $parameters); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Upload media to upload.twitter.com. |
||
257 | * |
||
258 | * @param string $path |
||
259 | * @param array $parameters |
||
260 | * @param bool $chunked |
||
261 | * |
||
262 | * @return array|object |
||
263 | */ |
||
264 | public function upload($path, array $parameters = [], $chunked = false) |
||
265 | { |
||
266 | if ($chunked) { |
||
267 | return $this->uploadMediaChunked($path, $parameters); |
||
268 | } else { |
||
269 | return $this->uploadMediaNotChunked($path, $parameters); |
||
270 | } |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Private method to upload media (not chunked) to upload.twitter.com. |
||
275 | * |
||
276 | * @param string $path |
||
277 | * @param array $parameters |
||
278 | * |
||
279 | * @return array|object |
||
280 | */ |
||
281 | private function uploadMediaNotChunked($path, array $parameters) |
||
282 | { |
||
283 | $file = file_get_contents($parameters['media']); |
||
284 | $base = base64_encode($file); |
||
285 | $parameters['media'] = $base; |
||
286 | |||
287 | return $this->http('POST', self::UPLOAD_HOST, $path, $parameters); |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Private method to upload media (chunked) to upload.twitter.com. |
||
292 | * |
||
293 | * @param string $path |
||
294 | * @param array $parameters |
||
295 | * |
||
296 | * @return array|object |
||
297 | */ |
||
298 | private function uploadMediaChunked($path, array $parameters) |
||
321 | |||
322 | /** |
||
323 | * Private method to get params for upload media chunked init. |
||
324 | * Twitter docs: https://dev.twitter.com/rest/reference/post/media/upload-init.html. |
||
325 | * |
||
326 | * @param array $parameters |
||
327 | * |
||
328 | * @return array |
||
329 | */ |
||
330 | private function mediaInitParameters(array $parameters) |
||
331 | { |
||
332 | $return = [ |
||
333 | 'command' => 'INIT', |
||
334 | 'media_type' => $parameters['media_type'], |
||
335 | 'total_bytes' => filesize($parameters['media']), |
||
336 | ]; |
||
337 | if (isset($parameters['additional_owners'])) { |
||
338 | $return['additional_owners'] = $parameters['additional_owners']; |
||
339 | } |
||
340 | if (isset($parameters['media_category'])) { |
||
346 | |||
347 | /** |
||
348 | * @param string $method |
||
349 | * @param string $host |
||
350 | * @param string $path |
||
351 | * @param array $parameters |
||
352 | * |
||
353 | * @return array|object |
||
354 | */ |
||
355 | private function http($method, $host, $path, array $parameters) |
||
364 | |||
365 | /** |
||
366 | * Make requests and retry them (if enabled) in case of Twitter's problems. |
||
367 | * |
||
368 | * @param string $method |
||
369 | * @param string $url |
||
370 | * @param string $method |
||
371 | * @param array $parameters |
||
372 | * |
||
373 | * @return array|object |
||
374 | */ |
||
375 | private function makeRequests($url, $method, array $parameters) |
||
388 | |||
389 | /** |
||
390 | * Checks if we have to retry request if API is down. |
||
391 | * |
||
392 | * @return bool |
||
393 | */ |
||
394 | private function requestsAvailable() |
||
398 | |||
399 | /** |
||
400 | * Format and sign an OAuth / API request. |
||
401 | * |
||
402 | * @param string $url |
||
403 | * @param string $method |
||
404 | * @param array $parameters |
||
405 | * |
||
406 | * @throws TwitterOAuthException |
||
407 | * |
||
408 | * @return string |
||
409 | */ |
||
410 | private function oAuthRequest($url, $method, array $parameters) |
||
431 | |||
432 | /** |
||
433 | * Set Curl options. |
||
434 | * |
||
435 | * @return array |
||
436 | */ |
||
437 | private function curlOptions() |
||
468 | |||
469 | /** |
||
470 | * Make an HTTP request. |
||
471 | * |
||
472 | * @param string $url |
||
473 | * @param string $method |
||
474 | * @param string $authorization |
||
475 | * @param array $postfields |
||
476 | * |
||
477 | * @throws TwitterOAuthException |
||
478 | * |
||
479 | * @return string |
||
480 | */ |
||
481 | private function request($url, $method, $authorization, array $postfields) |
||
525 | |||
526 | /** |
||
527 | * Get the header info to store. |
||
528 | * |
||
529 | * @param string $header |
||
530 | * |
||
531 | * @return array |
||
532 | */ |
||
533 | private function parseHeaders($header) |
||
546 | |||
547 | /** |
||
548 | * Encode application authorization header with base64. |
||
549 | * |
||
550 | * @param Consumer $consumer |
||
551 | * |
||
552 | * @return string |
||
553 | */ |
||
554 | private function encodeAppAuthorization(Consumer $consumer) |
||
561 | |||
562 | /** |
||
563 | * Is the code running from a Phar module. |
||
564 | * |
||
565 | * @return bool |
||
566 | */ |
||
567 | private function pharRunning() |
||
571 | |||
572 | /** |
||
573 | * Use included CA file instead of OS provided list. |
||
574 | * |
||
575 | * @return bool |
||
576 | */ |
||
577 | private function useCAFile() |
||
582 | } |
||
583 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.