Complex classes like TwitterAds 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 TwitterAds, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class TwitterAds extends Config |
||
| 20 | { |
||
| 21 | const API_VERSION = '1'; |
||
| 22 | const API_HOST = 'https://ads-api.twitter.com'; |
||
| 23 | const API_HOST_SANDBOX = 'https://ads-api-sandbox.twitter.com'; |
||
| 24 | const API_HOST_OAUTH = 'https://api.twitter.com'; |
||
| 25 | const UPLOAD_HOST = 'https://upload.twitter.com'; |
||
| 26 | const UPLOAD_CHUNK = 40960; // 1024 * 40 |
||
| 27 | |||
| 28 | /** @var string Method used for the request */ |
||
| 29 | private $method; |
||
| 30 | /** @var string Resource used for the request */ |
||
| 31 | private $resource; |
||
| 32 | /** @var Response details about the result of the last request */ |
||
| 33 | private $response; |
||
| 34 | /** @var string|null Application bearer token */ |
||
| 35 | private $bearer; |
||
| 36 | /** @var Consumer Twitter application details */ |
||
| 37 | private $consumer; |
||
| 38 | /** @var Token|null User access token details */ |
||
| 39 | private $token; |
||
| 40 | /** @var HmacSha1 OAuth 1 signature type used by Twitter */ |
||
| 41 | private $signatureMethod; |
||
| 42 | /** @var bool Sandbox allows to make requests thought sandbox environment */ |
||
| 43 | private $sandbox; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Constructor. |
||
| 47 | * |
||
| 48 | * @param string $consumerKey The Application Consumer Key |
||
| 49 | * @param string $consumerSecret The Application Consumer Secret |
||
| 50 | * @param string|null $oauthToken The Client Token (optional) |
||
| 51 | * @param string|null $oauthTokenSecret The Client Token Secret (optional) |
||
| 52 | * @param bool $sandbox The Sandbox environment (optional) |
||
| 53 | */ |
||
| 54 | public function __construct($consumerKey, $consumerSecret, $oauthToken = null, $oauthTokenSecret = null, $sandbox = false) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param string $accountId |
||
| 70 | * |
||
| 71 | * @return Account|Cursor |
||
| 72 | */ |
||
| 73 | public function getAccounts($accountId = '') |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param string $oauthToken |
||
| 82 | * @param string $oauthTokenSecret |
||
| 83 | */ |
||
| 84 | public function setOauthToken($oauthToken, $oauthTokenSecret) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return string|null |
||
| 91 | */ |
||
| 92 | public function getLastApiPath() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return int |
||
| 99 | */ |
||
| 100 | public function getLastHttpCode() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @return array |
||
| 107 | */ |
||
| 108 | public function getLastXHeaders() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @return array|object|null |
||
| 115 | */ |
||
| 116 | public function getLastBody() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Resets the last response cache. |
||
| 123 | */ |
||
| 124 | public function resetLastResponse() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Make URLs for user browser navigation. |
||
| 131 | * |
||
| 132 | * @param string $path |
||
| 133 | * @param array $parameters |
||
| 134 | * |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | public function url($path, array $parameters) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Make /oauth/* requests to the API. |
||
| 148 | * |
||
| 149 | * @param string $path |
||
| 150 | * @param array $parameters |
||
| 151 | * @return array |
||
| 152 | * @throws Exception |
||
| 153 | */ |
||
| 154 | public function oauth($path, array $parameters = []) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Make /oauth2/* requests to the API. |
||
| 174 | * |
||
| 175 | * @param string $path |
||
| 176 | * @param array $parameters |
||
| 177 | * |
||
| 178 | * @return array|object |
||
| 179 | */ |
||
| 180 | public function oauth2($path, array $parameters = []) |
||
| 194 | |||
| 195 | public function verifyCredentials($parameters = []) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Make GET requests to the API. |
||
| 202 | * |
||
| 203 | * @param string $path |
||
| 204 | * @param array $parameters |
||
| 205 | * |
||
| 206 | * @return array|object |
||
| 207 | */ |
||
| 208 | public function get($path, array $parameters = []) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Make POST requests to the API. |
||
| 215 | * |
||
| 216 | * @param string $path |
||
| 217 | * @param array $parameters |
||
| 218 | * |
||
| 219 | * @return array|object |
||
| 220 | */ |
||
| 221 | public function post($path, array $parameters = []) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Make DELETE requests to the API. |
||
| 228 | * |
||
| 229 | * @param string $path |
||
| 230 | * @param array $parameters |
||
| 231 | * |
||
| 232 | * @return array|object |
||
| 233 | */ |
||
| 234 | public function delete($path, array $parameters = []) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Make PUT requests to the API. |
||
| 241 | * |
||
| 242 | * @param string $path |
||
| 243 | * @param array $parameters |
||
| 244 | * |
||
| 245 | * @return array|object |
||
| 246 | */ |
||
| 247 | public function put($path, array $parameters = []) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Upload media to upload.twitter.com. |
||
| 254 | * |
||
| 255 | * @param string $path |
||
| 256 | * @param array $parameters |
||
| 257 | * @param bool $chunked |
||
| 258 | * |
||
| 259 | * @return array|object |
||
| 260 | */ |
||
| 261 | public function upload($path, array $parameters = [], $chunked = false) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Private method to upload media (not chunked) to upload.twitter.com. |
||
| 272 | * |
||
| 273 | * @param string $path |
||
| 274 | * @param array $parameters |
||
| 275 | * |
||
| 276 | * @return array|object |
||
| 277 | */ |
||
| 278 | private function uploadMediaNotChunked($path, $parameters) |
||
| 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, $parameters) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param string $method |
||
| 341 | * @param string $host |
||
| 342 | * @param string $path |
||
| 343 | * @param array $parameters |
||
| 344 | * @return resource |
||
| 345 | * @throws BadRequest |
||
| 346 | * @throws Forbidden |
||
| 347 | * @throws NotAuthorized |
||
| 348 | * @throws NotFound |
||
| 349 | * @throws RateLimit |
||
| 350 | * @throws ServerError |
||
| 351 | * @throws ServiceUnavailable |
||
| 352 | */ |
||
| 353 | private function http($method, $host, $path, array $parameters) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param $response |
||
| 372 | * @throws BadRequest |
||
| 373 | * @throws Forbidden |
||
| 374 | * @throws NotAuthorized |
||
| 375 | * @throws NotFound |
||
| 376 | * @throws RateLimit |
||
| 377 | * @throws ServerError |
||
| 378 | * @throws ServiceUnavailable |
||
| 379 | */ |
||
| 380 | private function manageErrors($response){ |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Format and sign an OAuth / API request. |
||
| 403 | * |
||
| 404 | * @param string $url |
||
| 405 | * @param string $method |
||
| 406 | * @param array $parameters |
||
| 407 | * |
||
| 408 | * @return string |
||
| 409 | * |
||
| 410 | * @throws TwitterAdsException |
||
| 411 | */ |
||
| 412 | private function oAuthRequest($url, $method, array $parameters) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Make an HTTP request. |
||
| 431 | * |
||
| 432 | * @param string $url |
||
| 433 | * @param string $method |
||
| 434 | * @param string $authorization |
||
| 435 | * @param array $postfields |
||
| 436 | * |
||
| 437 | * @return string |
||
| 438 | * |
||
| 439 | * @throws TwitterAdsException |
||
| 440 | */ |
||
| 441 | private function request($url, $method, $authorization, $postfields) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Get the header info to store. |
||
| 508 | * |
||
| 509 | * @param string $header |
||
| 510 | * |
||
| 511 | * @return array |
||
| 512 | */ |
||
| 513 | private function parseHeaders($header) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Encode application authorization header with base64. |
||
| 529 | * |
||
| 530 | * @param Consumer $consumer |
||
| 531 | * |
||
| 532 | * @return string |
||
| 533 | */ |
||
| 534 | private function encodeAppAuthorization($consumer) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Return current response. Allows inheritance. |
||
| 545 | * |
||
| 546 | * @return Response |
||
| 547 | */ |
||
| 548 | public function getResponse() |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @return string |
||
| 555 | */ |
||
| 556 | public function getMethod() |
||
| 560 | |||
| 561 | /** |
||
| 562 | * @return string |
||
| 563 | */ |
||
| 564 | public function getResource() |
||
| 568 | } |
||
| 569 |
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.