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 | public function resetAttemptsNumber() |
||
112 | |||
113 | /** |
||
114 | * Make URLs for user browser navigation. |
||
115 | * |
||
116 | * @param string $path |
||
117 | * @param array $parameters |
||
118 | * |
||
119 | * @return string |
||
120 | */ |
||
121 | public function url($path, array $parameters) |
||
128 | |||
129 | /** |
||
130 | * Make /oauth/* requests to the API. |
||
131 | * |
||
132 | * @param string $path |
||
133 | * @param array $parameters |
||
134 | * |
||
135 | * @return array |
||
136 | * @throws TwitterOAuthException |
||
137 | */ |
||
138 | public function oauth($path, array $parameters = []) |
||
155 | |||
156 | /** |
||
157 | * Make /oauth2/* requests to the API. |
||
158 | * |
||
159 | * @param string $path |
||
160 | * @param array $parameters |
||
161 | * |
||
162 | * @return array|object |
||
163 | */ |
||
164 | public function oauth2($path, array $parameters = []) |
||
177 | |||
178 | /** |
||
179 | * Make GET requests to the API. |
||
180 | * |
||
181 | * @param string $path |
||
182 | * @param array $parameters |
||
183 | * |
||
184 | * @return array|object |
||
185 | */ |
||
186 | public function get($path, array $parameters = []) |
||
190 | |||
191 | /** |
||
192 | * Make POST requests to the API. |
||
193 | * |
||
194 | * @param string $path |
||
195 | * @param array $parameters |
||
196 | * |
||
197 | * @return array|object |
||
198 | */ |
||
199 | public function post($path, array $parameters = []) |
||
203 | |||
204 | /** |
||
205 | * Make DELETE requests to the API. |
||
206 | * |
||
207 | * @param string $path |
||
208 | * @param array $parameters |
||
209 | * |
||
210 | * @return array|object |
||
211 | */ |
||
212 | public function delete($path, array $parameters = []) |
||
216 | |||
217 | /** |
||
218 | * Make PUT requests to the API. |
||
219 | * |
||
220 | * @param string $path |
||
221 | * @param array $parameters |
||
222 | * |
||
223 | * @return array|object |
||
224 | */ |
||
225 | public function put($path, array $parameters = []) |
||
229 | |||
230 | /** |
||
231 | * Upload media to upload.twitter.com. |
||
232 | * |
||
233 | * @param string $path |
||
234 | * @param array $parameters |
||
235 | * @param boolean $chunked |
||
236 | * |
||
237 | * @return array|object |
||
238 | */ |
||
239 | public function upload($path, array $parameters = [], $chunked = false) |
||
247 | |||
248 | /** |
||
249 | * Private method to upload media (not chunked) to upload.twitter.com. |
||
250 | * |
||
251 | * @param string $path |
||
252 | * @param array $parameters |
||
253 | * |
||
254 | * @return array|object |
||
255 | */ |
||
256 | private function uploadMediaNotChunked($path, array $parameters) |
||
263 | |||
264 | /** |
||
265 | * Private method to upload media (chunked) to upload.twitter.com. |
||
266 | * |
||
267 | * @param string $path |
||
268 | * @param array $parameters |
||
269 | * |
||
270 | * @return array|object |
||
271 | */ |
||
272 | private function uploadMediaChunked($path, array $parameters) |
||
294 | |||
295 | /** |
||
296 | * Private method to get params for upload media chunked init. |
||
297 | * Twitter docs: https://dev.twitter.com/rest/reference/post/media/upload-init.html |
||
298 | * |
||
299 | * @param array $parameters |
||
300 | * |
||
301 | * @return array |
||
302 | */ |
||
303 | private function mediaInitParameters(array $parameters) |
||
318 | |||
319 | /** |
||
320 | * @param string $method |
||
321 | * @param string $host |
||
322 | * @param string $path |
||
323 | * @param array $parameters |
||
324 | * |
||
325 | * @return array|object |
||
326 | */ |
||
327 | private function http($method, $host, $path, array $parameters) |
||
347 | |||
348 | /** |
||
349 | * Format and sign an OAuth / API request |
||
350 | * |
||
351 | * @param string $url |
||
352 | * @param string $method |
||
353 | * @param array $parameters |
||
354 | * |
||
355 | * @return string |
||
356 | * @throws TwitterOAuthException |
||
357 | */ |
||
358 | private function oAuthRequest($url, $method, array $parameters) |
||
378 | |||
379 | /** |
||
380 | * Set Curl options. |
||
381 | * |
||
382 | * @return array |
||
383 | */ |
||
384 | private function curlOptions() |
||
415 | |||
416 | /** |
||
417 | * Make an HTTP request |
||
418 | * |
||
419 | * @param string $url |
||
420 | * @param string $method |
||
421 | * @param string $authorization |
||
422 | * @param array $postfields |
||
423 | * |
||
424 | * @return string |
||
425 | * @throws TwitterOAuthException |
||
426 | */ |
||
427 | private function request($url, $method, $authorization, array $postfields) |
||
472 | |||
473 | /** |
||
474 | * Get the header info to store. |
||
475 | * |
||
476 | * @param string $header |
||
477 | * |
||
478 | * @return array |
||
479 | */ |
||
480 | private function parseHeaders($header) |
||
492 | |||
493 | /** |
||
494 | * Encode application authorization header with base64. |
||
495 | * |
||
496 | * @param Consumer $consumer |
||
497 | * |
||
498 | * @return string |
||
499 | */ |
||
500 | private function encodeAppAuthorization(Consumer $consumer) |
||
506 | |||
507 | /** |
||
508 | * Is the code running from a Phar module. |
||
509 | * |
||
510 | * @return boolean |
||
511 | */ |
||
512 | private function pharRunning() |
||
516 | |||
517 | /** |
||
518 | * Use included CA file instead of OS provided list. |
||
519 | * |
||
520 | * @return boolean |
||
521 | */ |
||
522 | private function useCAFile() |
||
527 | } |
||
528 |
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.