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 |
||
16 | class TwitterOAuth extends Config |
||
17 | { |
||
18 | const API_VERSION = '1.1'; |
||
19 | const API_HOST = 'https://api.twitter.com'; |
||
20 | const UPLOAD_HOST = 'https://upload.twitter.com'; |
||
21 | |||
22 | /** @var Response details about the result of the last request */ |
||
23 | private $response; |
||
24 | /** @var string|null Application bearer token */ |
||
25 | private $bearer; |
||
26 | /** @var Consumer Twitter application details */ |
||
27 | private $consumer; |
||
28 | /** @var Token|null User access token details */ |
||
29 | private $token; |
||
30 | /** @var HmacSha1 OAuth 1 signature type used by Twitter */ |
||
31 | private $signatureMethod; |
||
32 | /** @var int Number of attempts we made for the request */ |
||
33 | private $attempts = 0; |
||
34 | |||
35 | /** |
||
36 | * Constructor |
||
37 | * |
||
38 | * @param string $consumerKey The Application Consumer Key |
||
39 | * @param string $consumerSecret The Application Consumer Secret |
||
40 | * @param string|null $oauthToken The Client Token (optional) |
||
41 | * @param string|null $oauthTokenSecret The Client Token Secret (optional) |
||
42 | */ |
||
43 | public function __construct($consumerKey, $consumerSecret, $oauthToken = null, $oauthTokenSecret = null) |
||
55 | |||
56 | /** |
||
57 | * @param string $oauthToken |
||
58 | * @param string $oauthTokenSecret |
||
59 | */ |
||
60 | public function setOauthToken($oauthToken, $oauthTokenSecret) |
||
64 | |||
65 | /** |
||
66 | * @return string|null |
||
67 | */ |
||
68 | public function getLastApiPath() |
||
72 | |||
73 | /** |
||
74 | * @return int |
||
75 | */ |
||
76 | public function getLastHttpCode() |
||
80 | |||
81 | /** |
||
82 | * @return array |
||
83 | */ |
||
84 | public function getLastXHeaders() |
||
88 | |||
89 | /** |
||
90 | * @return array|object|null |
||
91 | */ |
||
92 | public function getLastBody() |
||
96 | |||
97 | /** |
||
98 | * Resets the last response cache. |
||
99 | */ |
||
100 | public function resetLastResponse() |
||
104 | |||
105 | /** |
||
106 | * Resets the attempts number. |
||
107 | */ |
||
108 | private function resetAttemptsNumber() |
||
112 | |||
113 | /** |
||
114 | * Delays the retries when they're activated. |
||
115 | */ |
||
116 | private function sleepIfNeeded() |
||
122 | |||
123 | |||
124 | /** |
||
125 | * Make URLs for user browser navigation. |
||
126 | * |
||
127 | * @param string $path |
||
128 | * @param array $parameters |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | public function url($path, array $parameters) |
||
139 | |||
140 | /** |
||
141 | * Make /oauth/* requests to the API. |
||
142 | * |
||
143 | * @param string $path |
||
144 | * @param array $parameters |
||
145 | * |
||
146 | * @return array |
||
147 | * @throws TwitterOAuthException |
||
148 | */ |
||
149 | public function oauth($path, array $parameters = []) |
||
166 | |||
167 | /** |
||
168 | * Make /oauth2/* requests to the API. |
||
169 | * |
||
170 | * @param string $path |
||
171 | * @param array $parameters |
||
172 | * |
||
173 | * @return array|object |
||
174 | */ |
||
175 | public function oauth2($path, array $parameters = []) |
||
188 | |||
189 | /** |
||
190 | * Make GET requests to the API. |
||
191 | * |
||
192 | * @param string $path |
||
193 | * @param array $parameters |
||
194 | * |
||
195 | * @return array|object |
||
196 | */ |
||
197 | public function get($path, array $parameters = []) |
||
201 | |||
202 | /** |
||
203 | * Make POST requests to the API. |
||
204 | * |
||
205 | * @param string $path |
||
206 | * @param array $parameters |
||
207 | * |
||
208 | * @return array|object |
||
209 | */ |
||
210 | public function post($path, array $parameters = []) |
||
214 | |||
215 | /** |
||
216 | * Make DELETE requests to the API. |
||
217 | * |
||
218 | * @param string $path |
||
219 | * @param array $parameters |
||
220 | * |
||
221 | * @return array|object |
||
222 | */ |
||
223 | public function delete($path, array $parameters = []) |
||
227 | |||
228 | /** |
||
229 | * Make PUT requests to the API. |
||
230 | * |
||
231 | * @param string $path |
||
232 | * @param array $parameters |
||
233 | * |
||
234 | * @return array|object |
||
235 | */ |
||
236 | public function put($path, array $parameters = []) |
||
240 | |||
241 | /** |
||
242 | * Upload media to upload.twitter.com. |
||
243 | * |
||
244 | * @param string $path |
||
245 | * @param array $parameters |
||
246 | * @param boolean $chunked |
||
247 | * |
||
248 | * @return array|object |
||
249 | */ |
||
250 | public function upload($path, array $parameters = [], $chunked = false) |
||
258 | |||
259 | /** |
||
260 | * Private method to upload media (not chunked) to upload.twitter.com. |
||
261 | * |
||
262 | * @param string $path |
||
263 | * @param array $parameters |
||
264 | * |
||
265 | * @return array|object |
||
266 | */ |
||
267 | private function uploadMediaNotChunked($path, array $parameters) |
||
274 | |||
275 | /** |
||
276 | * Private method to upload media (chunked) to upload.twitter.com. |
||
277 | * |
||
278 | * @param string $path |
||
279 | * @param array $parameters |
||
280 | * |
||
281 | * @return array|object |
||
282 | */ |
||
283 | private function uploadMediaChunked($path, array $parameters) |
||
310 | |||
311 | /** |
||
312 | * Private method to get params for upload media chunked init. |
||
313 | * Twitter docs: https://dev.twitter.com/rest/reference/post/media/upload-init.html |
||
314 | * |
||
315 | * @param array $parameters |
||
316 | * |
||
317 | * @return array |
||
318 | */ |
||
319 | private function mediaInitParameters(array $parameters) |
||
334 | |||
335 | /** |
||
336 | * @param string $method |
||
337 | * @param string $host |
||
338 | * @param string $path |
||
339 | * @param array $parameters |
||
340 | * |
||
341 | * @return array|object |
||
342 | */ |
||
343 | private function http($method, $host, $path, array $parameters) |
||
351 | |||
352 | /** |
||
353 | * |
||
354 | * Make requests and retry them (if enabled) in case of Twitter's problems. |
||
355 | * |
||
356 | * @param string $method |
||
357 | * @param string $url |
||
358 | * @param string $method |
||
359 | * @param array $parameters |
||
360 | * |
||
361 | * @return array|object |
||
362 | */ |
||
363 | private function makeRequests($url, $method, array $parameters) |
||
376 | |||
377 | /** |
||
378 | * Checks if we have to retry request if API is down. |
||
379 | * |
||
380 | * @return bool |
||
381 | */ |
||
382 | private function requestsAvailable() |
||
386 | |||
387 | /** |
||
388 | * Format and sign an OAuth / API request |
||
389 | * |
||
390 | * @param string $url |
||
391 | * @param string $method |
||
392 | * @param array $parameters |
||
393 | * |
||
394 | * @return string |
||
395 | * @throws TwitterOAuthException |
||
396 | */ |
||
397 | private function oAuthRequest($url, $method, array $parameters) |
||
417 | |||
418 | /** |
||
419 | * Set Curl options. |
||
420 | * |
||
421 | * @return array |
||
422 | */ |
||
423 | private function curlOptions() |
||
454 | |||
455 | /** |
||
456 | * Make an HTTP request |
||
457 | * |
||
458 | * @param string $url |
||
459 | * @param string $method |
||
460 | * @param string $authorization |
||
461 | * @param array $postfields |
||
462 | * |
||
463 | * @return string |
||
464 | * @throws TwitterOAuthException |
||
465 | */ |
||
466 | private function request($url, $method, $authorization, array $postfields) |
||
511 | |||
512 | /** |
||
513 | * Get the header info to store. |
||
514 | * |
||
515 | * @param string $header |
||
516 | * |
||
517 | * @return array |
||
518 | */ |
||
519 | private function parseHeaders($header) |
||
531 | |||
532 | /** |
||
533 | * Encode application authorization header with base64. |
||
534 | * |
||
535 | * @param Consumer $consumer |
||
536 | * |
||
537 | * @return string |
||
538 | */ |
||
539 | private function encodeAppAuthorization(Consumer $consumer) |
||
545 | |||
546 | /** |
||
547 | * Is the code running from a Phar module. |
||
548 | * |
||
549 | * @return boolean |
||
550 | */ |
||
551 | private function pharRunning() |
||
555 | |||
556 | /** |
||
557 | * Use included CA file instead of OS provided list. |
||
558 | * |
||
559 | * @return boolean |
||
560 | */ |
||
561 | private function useCAFile() |
||
566 | } |
||
567 |
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.