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 TwitterAds */ |
||
| 40 | protected static $instance; |
||
| 41 | /** @var string Method used for the request */ |
||
| 42 | private $method; |
||
| 43 | /** @var string Resource used for the request */ |
||
| 44 | private $resource; |
||
| 45 | /** @var Response details about the result of the last request */ |
||
| 46 | private $response; |
||
| 47 | /** @var string|null Application bearer token */ |
||
| 48 | private $bearer; |
||
| 49 | /** @var Consumer Twitter application details */ |
||
| 50 | private $consumer; |
||
| 51 | /** @var Token|null User access token details */ |
||
| 52 | private $token; |
||
| 53 | /** @var HmacSha1 OAuth 1 signature type used by Twitter */ |
||
| 54 | private $signatureMethod; |
||
| 55 | /** @var bool Sandbox allows to make requests thought sandbox environment */ |
||
| 56 | private $sandbox; |
||
| 57 | /** @var string */ |
||
| 58 | private $accountId; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @return TwitterAds|null |
||
| 62 | */ |
||
| 63 | public static function instance() { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param TwitterAds $instance |
||
| 69 | */ |
||
| 70 | public static function setInstance(TwitterAds $instance) { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Constructor. |
||
| 76 | * |
||
| 77 | * @param string $consumerKey The Application Consumer Key |
||
| 78 | * @param string $consumerSecret The Application Consumer Secret |
||
| 79 | * @param string|null $oauthToken The Client Token |
||
| 80 | * @param string|null $oauthTokenSecret The Client Token Secret |
||
| 81 | * @param string $accountId |
||
| 82 | * @param bool $sandbox The Sandbox environment (optional) |
||
| 83 | */ |
||
| 84 | public function __construct($consumerKey, $consumerSecret, $oauthToken, $oauthTokenSecret, $accountId = '', $sandbox = false) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param $consumerKey |
||
| 101 | * @param $consumerSecret |
||
| 102 | * @param $oauthToken |
||
| 103 | * @param $oauthTokenSecret |
||
| 104 | * @param string $accountId |
||
| 105 | * @param bool $sandbox |
||
| 106 | * @return static |
||
| 107 | */ |
||
| 108 | public static function init($consumerKey, $consumerSecret, $oauthToken, $oauthTokenSecret, $accountId = '', $sandbox = false) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return Account|Cursor |
||
| 117 | */ |
||
| 118 | public function getAccounts() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param string $oauthToken |
||
| 125 | * @param string $oauthTokenSecret |
||
| 126 | */ |
||
| 127 | public function setOauthToken($oauthToken, $oauthTokenSecret) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return string|null |
||
| 134 | */ |
||
| 135 | public function getLastApiPath() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return int |
||
| 142 | */ |
||
| 143 | public function getLastHttpCode() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | public function getLastXHeaders() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @return array|object|null |
||
| 158 | */ |
||
| 159 | public function getLastBody() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Resets the last response cache. |
||
| 166 | */ |
||
| 167 | public function resetLastResponse() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Make URLs for user browser navigation. |
||
| 174 | * |
||
| 175 | * @param string $path |
||
| 176 | * @param array $parameters |
||
| 177 | * |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public function url($path, array $parameters) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Make /oauth/* requests to the API. |
||
| 191 | * |
||
| 192 | * @param string $path |
||
| 193 | * @param array $parameters |
||
| 194 | * @return array |
||
| 195 | * @throws Exception |
||
| 196 | */ |
||
| 197 | public function oauth($path, array $parameters = []) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Make /oauth2/* requests to the API. |
||
| 217 | * |
||
| 218 | * @param string $path |
||
| 219 | * @param array $parameters |
||
| 220 | * |
||
| 221 | * @return array|object |
||
| 222 | */ |
||
| 223 | public function oauth2($path, array $parameters = []) |
||
| 237 | |||
| 238 | public function verifyCredentials($parameters = []) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Make GET requests to the API. |
||
| 245 | * |
||
| 246 | * @param string $path |
||
| 247 | * @param array $parameters |
||
| 248 | * |
||
| 249 | * @return Response |
||
| 250 | */ |
||
| 251 | public function get($path, array $parameters = []) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Make POST requests to the API. |
||
| 258 | * |
||
| 259 | * @param string $path |
||
| 260 | * @param array $parameters |
||
| 261 | * |
||
| 262 | * @param array $headers |
||
| 263 | * @return Response |
||
| 264 | */ |
||
| 265 | public function post($path, array $parameters = [], array $headers = []) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Make DELETE requests to the API. |
||
| 272 | * |
||
| 273 | * @param string $path |
||
| 274 | * @param array $parameters |
||
| 275 | * |
||
| 276 | * @return Response |
||
| 277 | */ |
||
| 278 | public function delete($path, array $parameters = []) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Make PUT requests to the API. |
||
| 285 | * |
||
| 286 | * @param string $path |
||
| 287 | * @param array $parameters |
||
| 288 | * |
||
| 289 | * @return Response |
||
| 290 | */ |
||
| 291 | public function put($path, array $parameters = [], array $headers = []) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Upload media to upload.twitter.com. |
||
| 298 | * |
||
| 299 | * @param array $parameters |
||
| 300 | * @param bool $chunked |
||
| 301 | * |
||
| 302 | * @return array|object |
||
| 303 | */ |
||
| 304 | public function upload(array $parameters = [], $chunked = false) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Private method to upload media (not chunked) to upload.twitter.com. |
||
| 315 | * |
||
| 316 | * @param string $path |
||
| 317 | * @param array $parameters |
||
| 318 | * |
||
| 319 | * @return array|object |
||
| 320 | */ |
||
| 321 | private function uploadMediaNotChunked($path, $parameters) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Private method to upload media (chunked) to upload.twitter.com. |
||
| 332 | * |
||
| 333 | * @param string $path |
||
| 334 | * @param array $parameters |
||
| 335 | * |
||
| 336 | * @return array|object |
||
| 337 | */ |
||
| 338 | private function uploadMediaChunked($path, $parameters) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @param string $method |
||
| 407 | * @param string $host |
||
| 408 | * @param string $path |
||
| 409 | * @param array $parameters |
||
| 410 | * @param array $headers |
||
| 411 | * @return Response |
||
| 412 | * @throws BadRequest |
||
| 413 | * @throws Forbidden |
||
| 414 | * @throws NotAuthorized |
||
| 415 | * @throws NotFound |
||
| 416 | * @throws RateLimit |
||
| 417 | * @throws ServerError |
||
| 418 | * @throws ServiceUnavailable |
||
| 419 | */ |
||
| 420 | private function http($method, $host, $path, array $parameters, $headers = []) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @param $response |
||
| 447 | * @throws BadRequest |
||
| 448 | * @throws Forbidden |
||
| 449 | * @throws NotAuthorized |
||
| 450 | * @throws NotFound |
||
| 451 | * @throws RateLimit |
||
| 452 | * @throws ServerError |
||
| 453 | * @throws ServiceUnavailable |
||
| 454 | */ |
||
| 455 | public function manageErrors($response){ |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Format and sign an OAuth / API request. |
||
| 478 | * |
||
| 479 | * @param string $url |
||
| 480 | * @param string $method |
||
| 481 | * @param array $parameters |
||
| 482 | * |
||
| 483 | * @param array $headers |
||
| 484 | * @return string |
||
| 485 | * @throws TwitterAdsException |
||
| 486 | * @throws TwitterOAuthException |
||
| 487 | */ |
||
| 488 | private function oAuthRequest($url, $method, array $parameters, $headers = []) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Make an HTTP request. |
||
| 511 | * |
||
| 512 | * @param string $url |
||
| 513 | * @param string $method |
||
| 514 | * @param string $authorization |
||
| 515 | * @param array $postfields |
||
| 516 | * |
||
| 517 | * @param array $headers |
||
| 518 | * @return string |
||
| 519 | * @throws TwitterAdsException |
||
| 520 | */ |
||
| 521 | private function request($url, $method, $authorization, $postfields, $headers = []) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Get the header info to store. |
||
| 592 | * |
||
| 593 | * @param string $header |
||
| 594 | * |
||
| 595 | * @return array |
||
| 596 | */ |
||
| 597 | private function parseHeaders($header) |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Encode application authorization header with base64. |
||
| 613 | * |
||
| 614 | * @param Consumer $consumer |
||
| 615 | * |
||
| 616 | * @return string |
||
| 617 | */ |
||
| 618 | private function encodeAppAuthorization($consumer) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Return current response. Allows inheritance. |
||
| 629 | * |
||
| 630 | * @return Response |
||
| 631 | */ |
||
| 632 | public function getResponse() |
||
| 636 | |||
| 637 | /** |
||
| 638 | * @return string |
||
| 639 | */ |
||
| 640 | public function getMethod() |
||
| 644 | |||
| 645 | /** |
||
| 646 | * @return string |
||
| 647 | */ |
||
| 648 | public function getResource() |
||
| 652 | |||
| 653 | /** |
||
| 654 | * @return string |
||
| 655 | */ |
||
| 656 | public function getAccountId() |
||
| 660 | |||
| 661 | /** |
||
| 662 | * @param string $accountId |
||
| 663 | */ |
||
| 664 | public function setAccountId($accountId) |
||
| 668 | } |
||
| 669 |
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: