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) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param string $oauthTokenSecret |
||
| 68 | */ |
||
| 69 | public function setBearer($oauthTokenSecret) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @return string|null |
||
| 77 | */ |
||
| 78 | public function getLastApiPath() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @return int |
||
| 85 | */ |
||
| 86 | public function getLastHttpCode() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @return array |
||
| 93 | */ |
||
| 94 | public function getLastXHeaders() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return array|object|null |
||
| 101 | */ |
||
| 102 | public function getLastBody() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Resets the last response cache. |
||
| 109 | */ |
||
| 110 | public function resetLastResponse() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Resets the attempts number. |
||
| 117 | */ |
||
| 118 | private function resetAttemptsNumber() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Delays the retries when they're activated. |
||
| 125 | */ |
||
| 126 | private function sleepIfNeeded() |
||
| 132 | |||
| 133 | |||
| 134 | /** |
||
| 135 | * Make URLs for user browser navigation. |
||
| 136 | * |
||
| 137 | * @param string $path |
||
| 138 | * @param array $parameters |
||
| 139 | * |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | public function url($path, array $parameters) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Make /oauth/* requests to the API. |
||
| 152 | * |
||
| 153 | * @param string $path |
||
| 154 | * @param array $parameters |
||
| 155 | * |
||
| 156 | * @return array |
||
| 157 | * @throws TwitterOAuthException |
||
| 158 | */ |
||
| 159 | public function oauth($path, array $parameters = []) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Make /oauth2/* requests to the API. |
||
| 179 | * |
||
| 180 | * @param string $path |
||
| 181 | * @param array $parameters |
||
| 182 | * |
||
| 183 | * @return array|object |
||
| 184 | */ |
||
| 185 | public function oauth2($path, array $parameters = []) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Make GET requests to the API. |
||
| 201 | * |
||
| 202 | * @param string $path |
||
| 203 | * @param array $parameters |
||
| 204 | * |
||
| 205 | * @return array|object |
||
| 206 | */ |
||
| 207 | public function get($path, array $parameters = []) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Make POST requests to the API. |
||
| 214 | * |
||
| 215 | * @param string $path |
||
| 216 | * @param array $parameters |
||
| 217 | * |
||
| 218 | * @return array|object |
||
| 219 | */ |
||
| 220 | public function post($path, array $parameters = []) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Make DELETE requests to the API. |
||
| 227 | * |
||
| 228 | * @param string $path |
||
| 229 | * @param array $parameters |
||
| 230 | * |
||
| 231 | * @return array|object |
||
| 232 | */ |
||
| 233 | public function delete($path, array $parameters = []) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Make PUT requests to the API. |
||
| 240 | * |
||
| 241 | * @param string $path |
||
| 242 | * @param array $parameters |
||
| 243 | * |
||
| 244 | * @return array|object |
||
| 245 | */ |
||
| 246 | public function put($path, array $parameters = []) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Upload media to upload.twitter.com. |
||
| 253 | * |
||
| 254 | * @param string $path |
||
| 255 | * @param array $parameters |
||
| 256 | * @param boolean $chunked |
||
| 257 | * |
||
| 258 | * @return array|object |
||
| 259 | */ |
||
| 260 | public function upload($path, array $parameters = [], $chunked = false) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Private method to upload media (not chunked) to upload.twitter.com. |
||
| 271 | * |
||
| 272 | * @param string $path |
||
| 273 | * @param array $parameters |
||
| 274 | * |
||
| 275 | * @return array|object |
||
| 276 | */ |
||
| 277 | private function uploadMediaNotChunked($path, array $parameters) |
||
| 278 | { |
||
| 279 | if (! is_readable($parameters['media']) || |
||
| 280 | ($file = file_get_contents($parameters['media'])) === false) { |
||
| 281 | throw new \InvalidArgumentException('You must supply a readable file'); |
||
| 282 | } |
||
| 283 | $parameters['media'] = base64_encode($file); |
||
| 284 | return $this->http('POST', self::UPLOAD_HOST, $path, $parameters); |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Private method to upload media (chunked) to upload.twitter.com. |
||
| 289 | * |
||
| 290 | * @param string $path |
||
| 291 | * @param array $parameters |
||
| 292 | * |
||
| 293 | * @return array|object |
||
| 294 | */ |
||
| 295 | private function uploadMediaChunked($path, array $parameters) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Private method to get params for upload media chunked init. |
||
| 320 | * Twitter docs: https://dev.twitter.com/rest/reference/post/media/upload-init.html |
||
| 321 | * |
||
| 322 | * @param array $parameters |
||
| 323 | * |
||
| 324 | * @return array |
||
| 325 | */ |
||
| 326 | private function mediaInitParameters(array $parameters) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param string $method |
||
| 344 | * @param string $host |
||
| 345 | * @param string $path |
||
| 346 | * @param array $parameters |
||
| 347 | * |
||
| 348 | * @return array|object |
||
| 349 | */ |
||
| 350 | private function http($method, $host, $path, array $parameters) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * |
||
| 361 | * Make requests and retry them (if enabled) in case of Twitter's problems. |
||
| 362 | * |
||
| 363 | * @param string $method |
||
| 364 | * @param string $url |
||
| 365 | * @param string $method |
||
| 366 | * @param array $parameters |
||
| 367 | * |
||
| 368 | * @return array|object |
||
| 369 | */ |
||
| 370 | private function makeRequests($url, $method, array $parameters) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Checks if we have to retry request if API is down. |
||
| 386 | * |
||
| 387 | * @return bool |
||
| 388 | */ |
||
| 389 | private function requestsAvailable() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Format and sign an OAuth / API request |
||
| 396 | * |
||
| 397 | * @param string $url |
||
| 398 | * @param string $method |
||
| 399 | * @param array $parameters |
||
| 400 | * |
||
| 401 | * @return string |
||
| 402 | * @throws TwitterOAuthException |
||
| 403 | */ |
||
| 404 | private function oAuthRequest($url, $method, array $parameters) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Set Curl options. |
||
| 427 | * |
||
| 428 | * @return array |
||
| 429 | */ |
||
| 430 | private function curlOptions() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Make an HTTP request |
||
| 464 | * |
||
| 465 | * @param string $url |
||
| 466 | * @param string $method |
||
| 467 | * @param string $authorization |
||
| 468 | * @param array $postfields |
||
| 469 | * |
||
| 470 | * @return string |
||
| 471 | * @throws TwitterOAuthException |
||
| 472 | */ |
||
| 473 | private function request($url, $method, $authorization, array $postfields) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Get the header info to store. |
||
| 521 | * |
||
| 522 | * @param string $header |
||
| 523 | * |
||
| 524 | * @return array |
||
| 525 | */ |
||
| 526 | private function parseHeaders($header) |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Encode application authorization header with base64. |
||
| 541 | * |
||
| 542 | * @param Consumer $consumer |
||
| 543 | * |
||
| 544 | * @return string |
||
| 545 | */ |
||
| 546 | private function encodeAppAuthorization(Consumer $consumer) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Is the code running from a Phar module. |
||
| 555 | * |
||
| 556 | * @return boolean |
||
| 557 | */ |
||
| 558 | private function pharRunning() |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Use included CA file instead of OS provided list. |
||
| 565 | * |
||
| 566 | * @return boolean |
||
| 567 | */ |
||
| 568 | private function useCAFile() |
||
| 573 | } |
||
| 574 |
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.