Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 22 | class TwitterAds extends Config |
||
| 23 | { |
||
| 24 | const API_VERSION = '3'; |
||
| 25 | const API_REST_VERSION = '1.1'; |
||
| 26 | const API_HOST = 'https://ads-api.twitter.com'; |
||
| 27 | const API_HOST_SANDBOX = 'https://ads-api-sandbox.twitter.com'; |
||
| 28 | const API_HOST_OAUTH = 'https://api.twitter.com'; |
||
| 29 | const UPLOAD_HOST = 'https://upload.twitter.com'; |
||
| 30 | const UPLOAD_PATH = 'media/upload.json'; |
||
| 31 | const UPLOAD_CHUNK = 40960; // 1024 * 40 |
||
| 32 | |||
| 33 | /** @var TwitterAds */ |
||
| 34 | protected static $instance; |
||
| 35 | /** @var string Method used for the request */ |
||
| 36 | private $method; |
||
| 37 | /** @var string Resource used for the request */ |
||
| 38 | private $resource; |
||
| 39 | /** @var Response details about the result of the last request */ |
||
| 40 | private $response; |
||
| 41 | /** @var string|null Application bearer token */ |
||
| 42 | private $bearer; |
||
| 43 | /** @var Consumer Twitter application details */ |
||
| 44 | private $consumer; |
||
| 45 | /** @var Token|null User access token details */ |
||
| 46 | private $token; |
||
| 47 | /** @var HmacSha1 OAuth 1 signature type used by Twitter */ |
||
| 48 | private $signatureMethod; |
||
| 49 | /** @var bool Sandbox allows to make requests thought sandbox environment */ |
||
| 50 | private $sandbox; |
||
| 51 | |||
| 52 | /** @var Account */ |
||
| 53 | private $account; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @return TwitterAds|null |
||
| 57 | */ |
||
| 58 | public static function instance() |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param TwitterAds $instance |
||
| 65 | */ |
||
| 66 | public static function setInstance(TwitterAds $instance) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Constructor. |
||
| 73 | * |
||
| 74 | * @param string $consumerKey The Application Consumer Key |
||
| 75 | * @param string $consumerSecret The Application Consumer Secret |
||
| 76 | * @param string|null $oauthToken The Client Token |
||
| 77 | * @param string|null $oauthTokenSecret The Client Token Secret |
||
| 78 | * @param Account $account | null |
||
| 79 | * @param bool $sandbox The Sandbox environment (optional) |
||
| 80 | */ |
||
| 81 | public function __construct($consumerKey, $consumerSecret, $oauthToken = '', $oauthTokenSecret = '', $account = null, $sandbox = false) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param $consumerKey |
||
| 98 | * @param $consumerSecret |
||
| 99 | * @param $oauthToken |
||
| 100 | * @param $oauthTokenSecret |
||
| 101 | * @param Account $account | null |
||
| 102 | * @param bool $sandbox |
||
| 103 | * @return static |
||
| 104 | */ |
||
| 105 | public static function init($consumerKey, $consumerSecret, $oauthToken = '', $oauthTokenSecret = '', $account = null, $sandbox = false) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @return Account|Cursor |
||
| 115 | */ |
||
| 116 | public function getAccounts() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param string $oauthToken |
||
| 123 | * @param string $oauthTokenSecret |
||
| 124 | */ |
||
| 125 | public function setOauthToken($oauthToken, $oauthTokenSecret) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return string|null |
||
| 132 | */ |
||
| 133 | public function getLastApiPath() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @return int |
||
| 140 | */ |
||
| 141 | public function getLastHttpCode() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return array |
||
| 148 | */ |
||
| 149 | public function getLastXHeaders() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @return array|object|null |
||
| 156 | */ |
||
| 157 | public function getLastBody() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Resets the last response cache. |
||
| 164 | */ |
||
| 165 | public function resetLastResponse() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Make URLs for user browser navigation. |
||
| 172 | * |
||
| 173 | * @param string $path |
||
| 174 | * @param array $parameters |
||
| 175 | * |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | public function url($path, array $parameters) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Make /oauth/* requests to the API. |
||
| 189 | * |
||
| 190 | * @param string $path |
||
| 191 | * @param array $parameters |
||
| 192 | * @return array |
||
| 193 | * @throws Exception |
||
| 194 | */ |
||
| 195 | public function oauth($path, array $parameters = []) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Make /oauth2/* requests to the API. |
||
| 215 | * |
||
| 216 | * @param string $path |
||
| 217 | * @param array $parameters |
||
| 218 | * |
||
| 219 | * @return array|object |
||
| 220 | */ |
||
| 221 | public function oauth2($path, array $parameters = []) |
||
| 235 | |||
| 236 | public function verifyCredentials($parameters = []) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Make GET requests to the API. |
||
| 243 | * |
||
| 244 | * @param string $path |
||
| 245 | * @param array $parameters |
||
| 246 | * |
||
| 247 | * @return Response |
||
| 248 | */ |
||
| 249 | public function get($path, array $parameters = []) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Make POST requests to the API. |
||
| 256 | * |
||
| 257 | * @param string $path |
||
| 258 | * @param array $parameters |
||
| 259 | * |
||
| 260 | * @param array $headers |
||
| 261 | * @return Response |
||
| 262 | */ |
||
| 263 | public function post($path, array $parameters = [], array $headers = []) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Make DELETE requests to the API. |
||
| 270 | * |
||
| 271 | * @param string $path |
||
| 272 | * @param array $parameters |
||
| 273 | * |
||
| 274 | * @return Response |
||
| 275 | */ |
||
| 276 | public function delete($path, array $parameters = []) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Make PUT requests to the API. |
||
| 283 | * |
||
| 284 | * @param string $path |
||
| 285 | * @param array $parameters |
||
| 286 | * |
||
| 287 | * @return Response |
||
| 288 | */ |
||
| 289 | public function put($path, array $parameters = [], array $headers = []) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Upload media to upload.twitter.com. |
||
| 296 | * |
||
| 297 | * @param array $parameters |
||
| 298 | * @param bool $chunked |
||
| 299 | * |
||
| 300 | * @return array|object |
||
| 301 | */ |
||
| 302 | public function upload(array $parameters = [], $chunked = false) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Private method to upload media (not chunked) to upload.twitter.com. |
||
| 313 | * |
||
| 314 | * @param string $path |
||
| 315 | * @param array $parameters |
||
| 316 | * |
||
| 317 | * @return array|object |
||
| 318 | */ |
||
| 319 | private function uploadMediaNotChunked($path, $parameters) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Private method to upload media (chunked) to upload.twitter.com. |
||
| 330 | * |
||
| 331 | * @param string $path |
||
| 332 | * @param array $parameters |
||
| 333 | * |
||
| 334 | * @return array|object |
||
| 335 | */ |
||
| 336 | private function uploadMediaChunked($path, $parameters) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param string $method |
||
| 413 | * @param string $host |
||
| 414 | * @param string $path |
||
| 415 | * @param array $parameters |
||
| 416 | * @param array $headers |
||
| 417 | * @return Response |
||
| 418 | * @throws BadRequest |
||
| 419 | * @throws Forbidden |
||
| 420 | * @throws NotAuthorized |
||
| 421 | * @throws NotFound |
||
| 422 | * @throws RateLimit |
||
| 423 | * @throws ServerError |
||
| 424 | * @throws ServiceUnavailable |
||
| 425 | */ |
||
| 426 | private function http($method, $host, $path, array $parameters, $headers = []) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param $response |
||
| 453 | * @throws BadRequest |
||
| 454 | * @throws Forbidden |
||
| 455 | * @throws NotAuthorized |
||
| 456 | * @throws NotFound |
||
| 457 | * @throws RateLimit |
||
| 458 | * @throws ServerError |
||
| 459 | * @throws ServiceUnavailable |
||
| 460 | */ |
||
| 461 | public function manageErrors($response) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Format and sign an OAuth / API request. |
||
| 485 | * |
||
| 486 | * @param string $url |
||
| 487 | * @param string $method |
||
| 488 | * @param array $parameters |
||
| 489 | * |
||
| 490 | * @param array $headers |
||
| 491 | * @return string |
||
| 492 | * @throws TwitterAdsException |
||
| 493 | */ |
||
| 494 | private function oAuthRequest($url, $method, array $parameters, $headers = []) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Make an HTTP request. |
||
| 516 | * |
||
| 517 | * @param string $url |
||
| 518 | * @param string $method |
||
| 519 | * @param string $authorization |
||
| 520 | * @param array $postfields |
||
| 521 | * |
||
| 522 | * @param array $headers |
||
| 523 | * @return string |
||
| 524 | * @throws TwitterAdsException |
||
| 525 | */ |
||
| 526 | private function request($url, $method, $authorization, $postfields, $headers = []) |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Get the header info to store. |
||
| 597 | * |
||
| 598 | * @param string $header |
||
| 599 | * |
||
| 600 | * @return array |
||
| 601 | */ |
||
| 602 | private function parseHeaders($header) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Set API URLS. |
||
| 618 | */ |
||
| 619 | public function accessTokenURL() |
||
| 623 | |||
| 624 | public function authenticateURL() |
||
| 628 | |||
| 629 | public function authorizeURL() |
||
| 633 | |||
| 634 | public function requestTokenURL() |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Encode application authorization header with base64. |
||
| 641 | * |
||
| 642 | * @param Consumer $consumer |
||
| 643 | * |
||
| 644 | * @return string |
||
| 645 | */ |
||
| 646 | private function encodeAppAuthorization($consumer) |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Return current response. Allows inheritance. |
||
| 657 | * |
||
| 658 | * @return Response |
||
| 659 | */ |
||
| 660 | public function getResponse() |
||
| 664 | |||
| 665 | /** |
||
| 666 | * @return string |
||
| 667 | */ |
||
| 668 | public function getMethod() |
||
| 672 | |||
| 673 | /** |
||
| 674 | * @return string |
||
| 675 | */ |
||
| 676 | public function getResource() |
||
| 680 | |||
| 681 | /** |
||
| 682 | * @return string |
||
| 683 | */ |
||
| 684 | public function getAccountId() |
||
| 691 | |||
| 692 | /** |
||
| 693 | * @return string |
||
| 694 | */ |
||
| 695 | public function getAccountTimezone() |
||
| 702 | |||
| 703 | /** |
||
| 704 | * @param Account $account |
||
| 705 | */ |
||
| 706 | public function setAccount($account) |
||
| 710 | |||
| 711 | View Code Duplication | public function getRequestToken($oauth_callback) |
|
| 720 | |||
| 721 | /** |
||
| 722 | * Exchange request token and secret for an access token and |
||
| 723 | * secret, to sign API calls. |
||
| 724 | * |
||
| 725 | * @param $oauth_verifier |
||
| 726 | * @return array ("oauth_token" => "the-access-token", |
||
| 727 | * "oauth_token_secret" => "the-access-secret", |
||
| 728 | * "user_id" => "9436992", |
||
| 729 | * "screen_name" => "abraham") |
||
| 730 | */ |
||
| 731 | View Code Duplication | public function getAccessToken($oauth_verifier) |
|
| 740 | |||
| 741 | // This function takes a input like a=b&a=c&d=e and returns the parsed |
||
| 742 | // parameters like this |
||
| 743 | // array('a' => array('b','c'), 'd' => 'e') |
||
| 744 | View Code Duplication | public static function parse_parameters($input) |
|
| 776 | |||
| 777 | public static function urlencode_rfc3986($input) |
||
| 791 | |||
| 792 | // This decode function isn't taking into consideration the above |
||
| 793 | // modifications to the encoding process. However, this method doesn't |
||
| 794 | // seem to be used anywhere so leaving it as is. |
||
| 795 | public static function urldecode_rfc3986($string) |
||
| 799 | |||
| 800 | /** |
||
| 801 | * Get the authorize URL. |
||
| 802 | * |
||
| 803 | * @param $token |
||
| 804 | * @param bool $sign_in_with_twitter |
||
| 805 | * @return string |
||
| 806 | */ |
||
| 807 | public function getAuthorizeURL($token, $sign_in_with_twitter = true) |
||
| 818 | } |
||
| 819 |
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: