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) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Private method to get params for upload media chunked init. |
||
| 308 | * Twitter docs: https://dev.twitter.com/rest/reference/post/media/upload-init.html |
||
| 309 | * |
||
| 310 | * @param array $parameters |
||
| 311 | * |
||
| 312 | * @return array |
||
| 313 | */ |
||
| 314 | private function mediaInitParameters(array $parameters) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param string $method |
||
| 332 | * @param string $host |
||
| 333 | * @param string $path |
||
| 334 | * @param array $parameters |
||
| 335 | * |
||
| 336 | * @return array|object |
||
| 337 | */ |
||
| 338 | private function http($method, $host, $path, array $parameters) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * |
||
| 349 | * Make requests and retry them (if enabled) in case of Twitter's problems. |
||
| 350 | * |
||
| 351 | * @param string $method |
||
| 352 | * @param string $url |
||
| 353 | * @param string $method |
||
| 354 | * @param array $parameters |
||
| 355 | * |
||
| 356 | * @return array|object |
||
| 357 | */ |
||
| 358 | private function makeRequests($url, $method, array $parameters) |
||
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * Format and sign an OAuth / API request |
||
| 375 | * |
||
| 376 | * @param string $url |
||
| 377 | * @param string $method |
||
| 378 | * @param array $parameters |
||
| 379 | * |
||
| 380 | * @return string |
||
| 381 | * @throws TwitterOAuthException |
||
| 382 | */ |
||
| 383 | private function oAuthRequest($url, $method, array $parameters) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Set Curl options. |
||
| 406 | * |
||
| 407 | * @return array |
||
| 408 | */ |
||
| 409 | private function curlOptions() |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Make an HTTP request |
||
| 443 | * |
||
| 444 | * @param string $url |
||
| 445 | * @param string $method |
||
| 446 | * @param string $authorization |
||
| 447 | * @param array $postfields |
||
| 448 | * |
||
| 449 | * @return string |
||
| 450 | * @throws TwitterOAuthException |
||
| 451 | */ |
||
| 452 | private function request($url, $method, $authorization, array $postfields) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Get the header info to store. |
||
| 500 | * |
||
| 501 | * @param string $header |
||
| 502 | * |
||
| 503 | * @return array |
||
| 504 | */ |
||
| 505 | private function parseHeaders($header) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Encode application authorization header with base64. |
||
| 520 | * |
||
| 521 | * @param Consumer $consumer |
||
| 522 | * |
||
| 523 | * @return string |
||
| 524 | */ |
||
| 525 | private function encodeAppAuthorization(Consumer $consumer) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Is the code running from a Phar module. |
||
| 534 | * |
||
| 535 | * @return boolean |
||
| 536 | */ |
||
| 537 | private function pharRunning() |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Use included CA file instead of OS provided list. |
||
| 544 | * |
||
| 545 | * @return boolean |
||
| 546 | */ |
||
| 547 | private function useCAFile() |
||
| 552 | } |
||
| 553 |
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.