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 |
||
| 28 | class TwitterAds extends Config |
||
| 29 | { |
||
| 30 | const API_VERSION = '1'; |
||
| 31 | const API_REST_VERSION = '1.1'; |
||
| 32 | const API_HOST = 'https://ads-api.twitter.com'; |
||
| 33 | const API_HOST_SANDBOX = 'https://ads-api-sandbox.twitter.com'; |
||
| 34 | const API_HOST_OAUTH = 'https://api.twitter.com'; |
||
| 35 | const UPLOAD_HOST = 'https://upload.twitter.com'; |
||
| 36 | const UPLOAD_PATH = 'media/upload.json'; |
||
| 37 | const UPLOAD_CHUNK = 40960; // 1024 * 40 |
||
| 38 | |||
| 39 | /** @var string Method used for the request */ |
||
| 40 | private $method; |
||
| 41 | /** @var string Resource used for the request */ |
||
| 42 | private $resource; |
||
| 43 | /** @var Response details about the result of the last request */ |
||
| 44 | private $response; |
||
| 45 | /** @var string|null Application bearer token */ |
||
| 46 | private $bearer; |
||
| 47 | /** @var Consumer Twitter application details */ |
||
| 48 | private $consumer; |
||
| 49 | /** @var Token|null User access token details */ |
||
| 50 | private $token; |
||
| 51 | /** @var HmacSha1 OAuth 1 signature type used by Twitter */ |
||
| 52 | private $signatureMethod; |
||
| 53 | /** @var bool Sandbox allows to make requests thought sandbox environment */ |
||
| 54 | private $sandbox; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Constructor. |
||
| 58 | * |
||
| 59 | * @param string $consumerKey The Application Consumer Key |
||
| 60 | * @param string $consumerSecret The Application Consumer Secret |
||
| 61 | * @param string|null $oauthToken The Client Token (optional) |
||
| 62 | * @param string|null $oauthTokenSecret The Client Token Secret (optional) |
||
| 63 | * @param bool $sandbox The Sandbox environment (optional) |
||
| 64 | */ |
||
| 65 | public function __construct($consumerKey, $consumerSecret, $oauthToken = null, $oauthTokenSecret = null, $sandbox = false) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param string $accountId |
||
| 81 | * |
||
| 82 | * @return Account|Cursor |
||
| 83 | */ |
||
| 84 | public function getAccounts($accountId = '') |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param string $oauthToken |
||
| 93 | * @param string $oauthTokenSecret |
||
| 94 | */ |
||
| 95 | public function setOauthToken($oauthToken, $oauthTokenSecret) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return string|null |
||
| 102 | */ |
||
| 103 | public function getLastApiPath() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return int |
||
| 110 | */ |
||
| 111 | public function getLastHttpCode() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @return array |
||
| 118 | */ |
||
| 119 | public function getLastXHeaders() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return array|object|null |
||
| 126 | */ |
||
| 127 | public function getLastBody() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Resets the last response cache. |
||
| 134 | */ |
||
| 135 | public function resetLastResponse() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Make URLs for user browser navigation. |
||
| 142 | * |
||
| 143 | * @param string $path |
||
| 144 | * @param array $parameters |
||
| 145 | * |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | public function url($path, array $parameters) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Make /oauth/* requests to the API. |
||
| 159 | * |
||
| 160 | * @param string $path |
||
| 161 | * @param array $parameters |
||
| 162 | * @return array |
||
| 163 | * @throws Exception |
||
| 164 | */ |
||
| 165 | public function oauth($path, array $parameters = []) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Make /oauth2/* requests to the API. |
||
| 185 | * |
||
| 186 | * @param string $path |
||
| 187 | * @param array $parameters |
||
| 188 | * |
||
| 189 | * @return array|object |
||
| 190 | */ |
||
| 191 | public function oauth2($path, array $parameters = []) |
||
| 205 | |||
| 206 | public function verifyCredentials($parameters = []) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Make GET requests to the API. |
||
| 213 | * |
||
| 214 | * @param string $path |
||
| 215 | * @param array $parameters |
||
| 216 | * |
||
| 217 | * @return Response |
||
| 218 | */ |
||
| 219 | public function get($path, array $parameters = []) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Make POST requests to the API. |
||
| 226 | * |
||
| 227 | * @param string $path |
||
| 228 | * @param array $parameters |
||
| 229 | * |
||
| 230 | * @param array $headers |
||
| 231 | * @return Response |
||
| 232 | */ |
||
| 233 | public function post($path, array $parameters = [], array $headers = []) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Make DELETE requests to the API. |
||
| 240 | * |
||
| 241 | * @param string $path |
||
| 242 | * @param array $parameters |
||
| 243 | * |
||
| 244 | * @return Response |
||
| 245 | */ |
||
| 246 | public function delete($path, array $parameters = []) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Make PUT requests to the API. |
||
| 253 | * |
||
| 254 | * @param string $path |
||
| 255 | * @param array $parameters |
||
| 256 | * |
||
| 257 | * @return Response |
||
| 258 | */ |
||
| 259 | public function put($path, array $parameters = [], array $headers = []) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Upload media to upload.twitter.com. |
||
| 266 | * |
||
| 267 | * @param array $parameters |
||
| 268 | * @param bool $chunked |
||
| 269 | * |
||
| 270 | * @return array|object |
||
| 271 | */ |
||
| 272 | public function upload(array $parameters = [], $chunked = false) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Private method to upload media (not chunked) to upload.twitter.com. |
||
| 283 | * |
||
| 284 | * @param string $path |
||
| 285 | * @param array $parameters |
||
| 286 | * |
||
| 287 | * @return array|object |
||
| 288 | */ |
||
| 289 | private function uploadMediaNotChunked($path, $parameters) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Private method to upload media (chunked) to upload.twitter.com. |
||
| 300 | * |
||
| 301 | * @param string $path |
||
| 302 | * @param array $parameters |
||
| 303 | * |
||
| 304 | * @return array|object |
||
| 305 | */ |
||
| 306 | private function uploadMediaChunked($path, $parameters) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param string $method |
||
| 375 | * @param string $host |
||
| 376 | * @param string $path |
||
| 377 | * @param array $parameters |
||
| 378 | * @param array $headers |
||
| 379 | * @return Response |
||
| 380 | * @throws BadRequest |
||
| 381 | * @throws Forbidden |
||
| 382 | * @throws NotAuthorized |
||
| 383 | * @throws NotFound |
||
| 384 | * @throws RateLimit |
||
| 385 | * @throws ServerError |
||
| 386 | * @throws ServiceUnavailable |
||
| 387 | */ |
||
| 388 | private function http($method, $host, $path, array $parameters, $headers = []) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param $response |
||
| 415 | * @throws BadRequest |
||
| 416 | * @throws Forbidden |
||
| 417 | * @throws NotAuthorized |
||
| 418 | * @throws NotFound |
||
| 419 | * @throws RateLimit |
||
| 420 | * @throws ServerError |
||
| 421 | * @throws ServiceUnavailable |
||
| 422 | */ |
||
| 423 | public function manageErrors($response){ |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Format and sign an OAuth / API request. |
||
| 446 | * |
||
| 447 | * @param string $url |
||
| 448 | * @param string $method |
||
| 449 | * @param array $parameters |
||
| 450 | * |
||
| 451 | * @param array $headers |
||
| 452 | * @return string |
||
| 453 | * @throws TwitterAdsException |
||
| 454 | * @throws TwitterOAuthException |
||
| 455 | */ |
||
| 456 | private function oAuthRequest($url, $method, array $parameters, $headers = []) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Make an HTTP request. |
||
| 479 | * |
||
| 480 | * @param string $url |
||
| 481 | * @param string $method |
||
| 482 | * @param string $authorization |
||
| 483 | * @param array $postfields |
||
| 484 | * |
||
| 485 | * @param array $headers |
||
| 486 | * @return string |
||
| 487 | * @throws TwitterAdsException |
||
| 488 | */ |
||
| 489 | private function request($url, $method, $authorization, $postfields, $headers = []) |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Get the header info to store. |
||
| 560 | * |
||
| 561 | * @param string $header |
||
| 562 | * |
||
| 563 | * @return array |
||
| 564 | */ |
||
| 565 | private function parseHeaders($header) |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Encode application authorization header with base64. |
||
| 581 | * |
||
| 582 | * @param Consumer $consumer |
||
| 583 | * |
||
| 584 | * @return string |
||
| 585 | */ |
||
| 586 | private function encodeAppAuthorization($consumer) |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Return current response. Allows inheritance. |
||
| 597 | * |
||
| 598 | * @return Response |
||
| 599 | */ |
||
| 600 | public function getResponse() |
||
| 604 | |||
| 605 | /** |
||
| 606 | * @return string |
||
| 607 | */ |
||
| 608 | public function getMethod() |
||
| 612 | |||
| 613 | /** |
||
| 614 | * @return string |
||
| 615 | */ |
||
| 616 | public function getResource() |
||
| 620 | } |
||
| 621 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: