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 | protected $api_version = '1.1'; |
||
19 | protected $api_host = 'https://api.twitter.com'; |
||
20 | protected $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 | |||
33 | /** |
||
34 | * Constructor |
||
35 | * |
||
36 | * @param string $consumerKey The Application Consumer Key |
||
37 | * @param string $consumerSecret The Application Consumer Secret |
||
38 | * @param string|null $oauthToken The Client Token (optional) |
||
39 | * @param string|null $oauthTokenSecret The Client Token Secret (optional) |
||
40 | */ |
||
41 | public function __construct($consumerKey, $consumerSecret, $oauthToken = null, $oauthTokenSecret = null) |
||
53 | |||
54 | /** |
||
55 | * @param string $oauthToken |
||
56 | * @param string $oauthTokenSecret |
||
57 | */ |
||
58 | public function setOauthToken($oauthToken, $oauthTokenSecret) |
||
62 | |||
63 | /** |
||
64 | * @return string|null |
||
65 | */ |
||
66 | public function getLastApiPath() |
||
70 | |||
71 | /** |
||
72 | * @return int |
||
73 | */ |
||
74 | public function getLastHttpCode() |
||
78 | |||
79 | /** |
||
80 | * @return array |
||
81 | */ |
||
82 | public function getLastXHeaders() |
||
86 | |||
87 | /** |
||
88 | * @return array|object|null |
||
89 | */ |
||
90 | public function getLastBody() |
||
94 | |||
95 | /** |
||
96 | * Resets the last response cache. |
||
97 | */ |
||
98 | public function resetLastResponse() |
||
102 | |||
103 | /** |
||
104 | * Make URLs for user browser navigation. |
||
105 | * |
||
106 | * @param string $path |
||
107 | * @param array $parameters |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | public function url($path, array $parameters) |
||
118 | |||
119 | /** |
||
120 | * Make /oauth/* requests to the API. |
||
121 | * |
||
122 | * @param string $path |
||
123 | * @param array $parameters |
||
124 | * |
||
125 | * @return array |
||
126 | * @throws TwitterOAuthException |
||
127 | */ |
||
128 | public function oauth($path, array $parameters = []) |
||
145 | |||
146 | /** |
||
147 | * Make /oauth2/* requests to the API. |
||
148 | * |
||
149 | * @param string $path |
||
150 | * @param array $parameters |
||
151 | * |
||
152 | * @return array|object |
||
153 | */ |
||
154 | public function oauth2($path, array $parameters = []) |
||
167 | |||
168 | /** |
||
169 | * Make GET requests to the API. |
||
170 | * |
||
171 | * @param string $path |
||
172 | * @param array $parameters |
||
173 | * |
||
174 | * @return array|object |
||
175 | */ |
||
176 | public function get($path, array $parameters = []) |
||
180 | |||
181 | /** |
||
182 | * Make POST requests to the API. |
||
183 | * |
||
184 | * @param string $path |
||
185 | * @param array $parameters |
||
186 | * |
||
187 | * @return array|object |
||
188 | */ |
||
189 | public function post($path, array $parameters = []) |
||
193 | |||
194 | /** |
||
195 | * Make DELETE requests to the API. |
||
196 | * |
||
197 | * @param string $path |
||
198 | * @param array $parameters |
||
199 | * |
||
200 | * @return array|object |
||
201 | */ |
||
202 | public function delete($path, array $parameters = []) |
||
206 | |||
207 | /** |
||
208 | * Make PUT requests to the API. |
||
209 | * |
||
210 | * @param string $path |
||
211 | * @param array $parameters |
||
212 | * |
||
213 | * @return array|object |
||
214 | */ |
||
215 | public function put($path, array $parameters = []) |
||
219 | |||
220 | /** |
||
221 | * Upload media to upload.twitter.com. |
||
222 | * |
||
223 | * @param string $path |
||
224 | * @param array $parameters |
||
225 | * @param boolean $chunked |
||
226 | * |
||
227 | * @return array|object |
||
228 | */ |
||
229 | public function upload($path, array $parameters = [], $chunked = false) |
||
237 | |||
238 | /** |
||
239 | * Set Twitter API version |
||
240 | * |
||
241 | * @param string $api_version |
||
242 | * |
||
243 | * @return array|object |
||
244 | */ |
||
245 | public function setApiVersion($api_version) |
||
249 | |||
250 | /** |
||
251 | * Set Twitter API endPoint |
||
252 | * |
||
253 | * @param string $api_host |
||
254 | * |
||
255 | * @return array|object |
||
256 | */ |
||
257 | public function setApiHost($api_host) |
||
261 | |||
262 | /** |
||
263 | * Set Twitter API upload endPoint |
||
264 | * |
||
265 | * @param string $upload_host |
||
266 | * |
||
267 | * @return array|object |
||
268 | */ |
||
269 | public function setUploadHost($upload_host) |
||
273 | |||
274 | /** |
||
275 | * Private method to upload media (not chunked) to upload.twitter.com. |
||
276 | * |
||
277 | * @param string $path |
||
278 | * @param array $parameters |
||
279 | * |
||
280 | * @return array|object |
||
281 | */ |
||
282 | private function uploadMediaNotChunked($path, array $parameters) |
||
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) |
||
345 | |||
346 | /** |
||
347 | * @param string $method |
||
348 | * @param string $host |
||
349 | * @param string $path |
||
350 | * @param array $parameters |
||
351 | * |
||
352 | * @return array|object |
||
353 | */ |
||
354 | private function http($method, $host, $path, array $parameters) |
||
364 | |||
365 | /** |
||
366 | * Format and sign an OAuth / API request |
||
367 | * |
||
368 | * @param string $url |
||
369 | * @param string $method |
||
370 | * @param array $parameters |
||
371 | * |
||
372 | * @return string |
||
373 | * @throws TwitterOAuthException |
||
374 | */ |
||
375 | private function oAuthRequest($url, $method, array $parameters) |
||
395 | |||
396 | /** |
||
397 | * Set Curl options. |
||
398 | * |
||
399 | * @return array |
||
400 | */ |
||
401 | private function curlOptions() |
||
432 | |||
433 | /** |
||
434 | * Make an HTTP request |
||
435 | * |
||
436 | * @param string $url |
||
437 | * @param string $method |
||
438 | * @param string $authorization |
||
439 | * @param array $postfields |
||
440 | * |
||
441 | * @return string |
||
442 | * @throws TwitterOAuthException |
||
443 | */ |
||
444 | private function request($url, $method, $authorization, array $postfields) |
||
489 | |||
490 | /** |
||
491 | * Get the header info to store. |
||
492 | * |
||
493 | * @param string $header |
||
494 | * |
||
495 | * @return array |
||
496 | */ |
||
497 | private function parseHeaders($header) |
||
509 | |||
510 | /** |
||
511 | * Encode application authorization header with base64. |
||
512 | * |
||
513 | * @param Consumer $consumer |
||
514 | * |
||
515 | * @return string |
||
516 | */ |
||
517 | private function encodeAppAuthorization(Consumer $consumer) |
||
523 | |||
524 | /** |
||
525 | * Is the code running from a Phar module. |
||
526 | * |
||
527 | * @return boolean |
||
528 | */ |
||
529 | private function pharRunning() |
||
533 | |||
534 | /** |
||
535 | * Use included CA file instead of OS provided list. |
||
536 | * |
||
537 | * @return boolean |
||
538 | */ |
||
539 | private function useCAFile() |
||
544 | } |
||
545 |
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.