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 | |||
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 | * Private method to upload media (not chunked) to upload.twitter.com. |
||
240 | * |
||
241 | * @param string $path |
||
242 | * @param array $parameters |
||
243 | * |
||
244 | * @return array|object |
||
245 | */ |
||
246 | private function uploadMediaNotChunked($path, array $parameters) |
||
253 | |||
254 | /** |
||
255 | * Private method to upload media (chunked) to upload.twitter.com. |
||
256 | * |
||
257 | * @param string $path |
||
258 | * @param array $parameters |
||
259 | * |
||
260 | * @return array|object |
||
261 | */ |
||
262 | private function uploadMediaChunked($path, array $parameters) |
||
285 | |||
286 | /** |
||
287 | * Private method to get params for upload media chunked init. |
||
288 | * Twitter docs: https://dev.twitter.com/rest/reference/post/media/upload-init.html |
||
289 | * |
||
290 | * @param array $parameters |
||
291 | * |
||
292 | * @return array |
||
293 | */ |
||
294 | private function mediaInitParameters(array $parameters) |
||
309 | |||
310 | /** |
||
311 | * @param string $method |
||
312 | * @param string $host |
||
313 | * @param string $path |
||
314 | * @param array $parameters |
||
315 | * |
||
316 | * @return array|object |
||
317 | */ |
||
318 | private function http($method, $host, $path, array $parameters) |
||
328 | |||
329 | /** |
||
330 | * Format and sign an OAuth / API request |
||
331 | * |
||
332 | * @param string $url |
||
333 | * @param string $method |
||
334 | * @param array $parameters |
||
335 | * |
||
336 | * @return string |
||
337 | * @throws TwitterOAuthException |
||
338 | */ |
||
339 | private function oAuthRequest($url, $method, array $parameters) |
||
359 | |||
360 | /** |
||
361 | * Set Curl options. |
||
362 | * |
||
363 | * @return array |
||
364 | */ |
||
365 | private function curlOptions() |
||
396 | |||
397 | /** |
||
398 | * Make an HTTP request |
||
399 | * |
||
400 | * @param string $url |
||
401 | * @param string $method |
||
402 | * @param string $authorization |
||
403 | * @param array $postfields |
||
404 | * |
||
405 | * @return string |
||
406 | * @throws TwitterOAuthException |
||
407 | */ |
||
408 | private function request($url, $method, $authorization, array $postfields) |
||
453 | |||
454 | /** |
||
455 | * Get the header info to store. |
||
456 | * |
||
457 | * @param string $header |
||
458 | * |
||
459 | * @return array |
||
460 | */ |
||
461 | private function parseHeaders($header) |
||
473 | |||
474 | /** |
||
475 | * Encode application authorization header with base64. |
||
476 | * |
||
477 | * @param Consumer $consumer |
||
478 | * |
||
479 | * @return string |
||
480 | */ |
||
481 | private function encodeAppAuthorization(Consumer $consumer) |
||
487 | |||
488 | /** |
||
489 | * Is the code running from a Phar module. |
||
490 | * |
||
491 | * @return boolean |
||
492 | */ |
||
493 | private function pharRunning() |
||
497 | |||
498 | /** |
||
499 | * Use included CA file instead of OS provided list. |
||
500 | * |
||
501 | * @return boolean |
||
502 | */ |
||
503 | private function useCAFile() |
||
508 | } |
||
509 |
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.