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 API_EXTENSION = 'json'; |
||
| 21 | const UPLOAD_HOST = 'https://upload.twitter.com'; |
||
| 22 | const STATUS_SUCCES = 200; |
||
| 23 | |||
| 24 | /** @var Response details about the result of the last request */ |
||
| 25 | private $response; |
||
| 26 | /** @var string|null Application bearer token */ |
||
| 27 | private $bearer; |
||
| 28 | /** @var Consumer Twitter application details */ |
||
| 29 | private $consumer; |
||
| 30 | /** @var Token|null User access token details */ |
||
| 31 | private $token; |
||
| 32 | /** @var HmacSha1 OAuth 1 signature type used by Twitter */ |
||
| 33 | private $signatureMethod; |
||
| 34 | /** @var int Number of attempts we made for the request */ |
||
| 35 | private $attempts = 0; |
||
| 36 | /** @var string|null Version of the API */ |
||
| 37 | private $apiVersion; |
||
| 38 | /** @var string|null Host of the API */ |
||
| 39 | private $apiHost; |
||
| 40 | /** @var string|null Extension of the API */ |
||
| 41 | private $apiExtension; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Constructor |
||
| 45 | * |
||
| 46 | * @param string $consumerKey The Application Consumer Key |
||
| 47 | * @param string $consumerSecret The Application Consumer Secret |
||
| 48 | * @param string|null $oauthToken The Client Token (optional) |
||
| 49 | * @param string|null $oauthTokenSecret The Client Token Secret (optional) |
||
| 50 | */ |
||
| 51 | public function __construct($consumerKey, $consumerSecret, $oauthToken = null, $oauthTokenSecret = null) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param string $oauthToken |
||
| 69 | * @param string $oauthTokenSecret |
||
| 70 | */ |
||
| 71 | public function setOauthToken($oauthToken, $oauthTokenSecret) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param string $version |
||
| 78 | */ |
||
| 79 | public function setApiVersion($version) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | public function getApiVersion() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param string $host |
||
| 94 | */ |
||
| 95 | public function setApiHost($host) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | public function getApiHost() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param string $extension |
||
| 110 | */ |
||
| 111 | public function setApiExtension($extension) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | public function getApiExtension() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param boolean $decodeJsonAsArray |
||
| 126 | */ |
||
| 127 | public function setDecodeJsonAsArray($decodeJsonAsArray) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return boolean |
||
| 134 | */ |
||
| 135 | public function isDecodeJsonAsArray() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return string|null |
||
| 142 | */ |
||
| 143 | public function getLastApiPath() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return int |
||
| 150 | */ |
||
| 151 | public function getLastHttpCode() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @return array |
||
| 158 | */ |
||
| 159 | public function getLastXHeaders() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return array|object|null |
||
| 166 | */ |
||
| 167 | public function getLastBody() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Resets the last response cache. |
||
| 174 | */ |
||
| 175 | public function resetLastResponse() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Resets the attempts number. |
||
| 182 | */ |
||
| 183 | private function resetAttemptsNumber() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Delays the retries when they're activated. |
||
| 190 | */ |
||
| 191 | private function sleepIfNeeded() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Make URLs for user browser navigation. |
||
| 200 | * |
||
| 201 | * @param integer $code |
||
| 202 | * |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | private function getError($code) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Make URLs for user browser navigation. |
||
| 229 | * |
||
| 230 | * @param string $path |
||
| 231 | * @param array $parameters |
||
| 232 | * |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | public function url($path, array $parameters) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Make /oauth/* requests to the API. |
||
| 246 | * |
||
| 247 | * @param string $path |
||
| 248 | * @param array $parameters |
||
| 249 | * |
||
| 250 | * @return array |
||
| 251 | * @throws TwitterOAuthException |
||
| 252 | */ |
||
| 253 | public function oauth($path, array $parameters = []) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Make /oauth2/* requests to the API. |
||
| 274 | * |
||
| 275 | * @param string $path |
||
| 276 | * @param array $parameters |
||
| 277 | * |
||
| 278 | * @return array|object |
||
| 279 | */ |
||
| 280 | public function oauth2($path, array $parameters = []) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Make GET requests to the API. |
||
| 297 | * |
||
| 298 | * @param string $path |
||
| 299 | * @param array $parameters |
||
| 300 | * |
||
| 301 | * @return array|object |
||
| 302 | */ |
||
| 303 | public function get($path, array $parameters = []) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Make POST requests to the API. |
||
| 312 | * |
||
| 313 | * @param string $path |
||
| 314 | * @param array $parameters |
||
| 315 | * |
||
| 316 | * @return array|object |
||
| 317 | */ |
||
| 318 | public function post($path, array $parameters = []) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Make DELETE requests to the API. |
||
| 327 | * |
||
| 328 | * @param string $path |
||
| 329 | * @param array $parameters |
||
| 330 | * |
||
| 331 | * @return array|object |
||
| 332 | */ |
||
| 333 | public function delete($path, array $parameters = []) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Make PUT requests to the API. |
||
| 342 | * |
||
| 343 | * @param string $path |
||
| 344 | * @param array $parameters |
||
| 345 | * |
||
| 346 | * @return array|object |
||
| 347 | */ |
||
| 348 | public function put($path, array $parameters = []) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Upload media to upload.twitter.com. |
||
| 357 | * |
||
| 358 | * @param string $path |
||
| 359 | * @param array $parameters |
||
| 360 | * @param boolean $chunked |
||
| 361 | * |
||
| 362 | * @return array|object |
||
| 363 | */ |
||
| 364 | public function upload($path, array $parameters = [], $chunked = false) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Private method to upload media (not chunked) to upload.twitter.com. |
||
| 375 | * |
||
| 376 | * @param string $path |
||
| 377 | * @param array $parameters |
||
| 378 | * |
||
| 379 | * @return array|object |
||
| 380 | */ |
||
| 381 | private function uploadMediaNotChunked($path, array $parameters) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Private method to upload media (chunked) to upload.twitter.com. |
||
| 391 | * |
||
| 392 | * @param string $path |
||
| 393 | * @param array $parameters |
||
| 394 | * |
||
| 395 | * @return array|object |
||
| 396 | */ |
||
| 397 | private function uploadMediaChunked($path, array $parameters) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Private method to get params for upload media chunked init. |
||
| 422 | * Twitter docs: https://dev.twitter.com/rest/reference/post/media/upload-init.html |
||
| 423 | * |
||
| 424 | * @param array $parameters |
||
| 425 | * |
||
| 426 | * @return array |
||
| 427 | */ |
||
| 428 | private function mediaInitParameters(array $parameters) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param string $method |
||
| 446 | * @param string $host |
||
| 447 | * @param string $path |
||
| 448 | * @param array $parameters |
||
| 449 | * |
||
| 450 | * @return array|object |
||
| 451 | */ |
||
| 452 | private function http($method, $host, $path, array $parameters) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * |
||
| 469 | * Make requests and retry them (if enabled) in case of Twitter's problems. |
||
| 470 | * |
||
| 471 | * @param string $method |
||
| 472 | * @param string $url |
||
| 473 | * @param string $method |
||
| 474 | * @param array $parameters |
||
| 475 | * |
||
| 476 | * @return array|object |
||
| 477 | * @throws TwitterOAuthException |
||
| 478 | */ |
||
| 479 | private function makeRequests($url, $method, array $parameters) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Checks if we have to retry request if API is down. |
||
| 503 | * |
||
| 504 | * @return bool |
||
| 505 | */ |
||
| 506 | private function requestsAvailable() |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Format and sign an OAuth / API request |
||
| 513 | * |
||
| 514 | * @param string $url |
||
| 515 | * @param string $method |
||
| 516 | * @param array $parameters |
||
| 517 | * |
||
| 518 | * @return string |
||
| 519 | * @throws TwitterOAuthException |
||
| 520 | */ |
||
| 521 | private function oAuthRequest($url, $method, array $parameters) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Set Curl options. |
||
| 544 | * |
||
| 545 | * @return array |
||
| 546 | */ |
||
| 547 | private function curlOptions() |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Make an HTTP request |
||
| 581 | * |
||
| 582 | * @param string $url |
||
| 583 | * @param string $method |
||
| 584 | * @param string $authorization |
||
| 585 | * @param array $postfields |
||
| 586 | * |
||
| 587 | * @return string |
||
| 588 | * @throws TwitterOAuthException |
||
| 589 | */ |
||
| 590 | private function request($url, $method, $authorization, array $postfields) |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Get the header info to store. |
||
| 638 | * |
||
| 639 | * @param string $header |
||
| 640 | * |
||
| 641 | * @return array |
||
| 642 | */ |
||
| 643 | private function parseHeaders($header) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Encode application authorization header with base64. |
||
| 658 | * |
||
| 659 | * @param Consumer $consumer |
||
| 660 | * |
||
| 661 | * @return string |
||
| 662 | */ |
||
| 663 | private function encodeAppAuthorization(Consumer $consumer) |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Is the code running from a Phar module. |
||
| 672 | * |
||
| 673 | * @return boolean |
||
| 674 | */ |
||
| 675 | private function pharRunning() |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Use included CA file instead of OS provided list. |
||
| 682 | * |
||
| 683 | * @return boolean |
||
| 684 | */ |
||
| 685 | private function useCAFile() |
||
| 690 | } |
||
| 691 |
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.