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 |
||
| 23 | class TwitterAds extends Config |
||
| 24 | { |
||
| 25 | const API_VERSION = '1'; |
||
| 26 | const API_REST_VERSION = '1.1'; |
||
| 27 | const API_HOST = 'https://ads-api.twitter.com'; |
||
| 28 | const API_HOST_SANDBOX = 'https://ads-api-sandbox.twitter.com'; |
||
| 29 | const API_HOST_OAUTH = 'https://api.twitter.com'; |
||
| 30 | const UPLOAD_HOST = 'https://upload.twitter.com'; |
||
| 31 | const UPLOAD_PATH = 'media/upload.json'; |
||
| 32 | const UPLOAD_CHUNK = 40960; // 1024 * 40 |
||
| 33 | |||
| 34 | /** @var TwitterAds */ |
||
| 35 | protected static $instance; |
||
| 36 | /** @var string Method used for the request */ |
||
| 37 | private $method; |
||
| 38 | /** @var string Resource used for the request */ |
||
| 39 | private $resource; |
||
| 40 | /** @var Response details about the result of the last request */ |
||
| 41 | private $response; |
||
| 42 | /** @var string|null Application bearer token */ |
||
| 43 | private $bearer; |
||
| 44 | /** @var Consumer Twitter application details */ |
||
| 45 | private $consumer; |
||
| 46 | /** @var Token|null User access token details */ |
||
| 47 | private $token; |
||
| 48 | /** @var HmacSha1 OAuth 1 signature type used by Twitter */ |
||
| 49 | private $signatureMethod; |
||
| 50 | /** @var bool Sandbox allows to make requests thought sandbox environment */ |
||
| 51 | private $sandbox; |
||
| 52 | /** @var string */ |
||
| 53 | private $accountId; |
||
| 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 string $accountId |
||
| 79 | * @param bool $sandbox The Sandbox environment (optional) |
||
| 80 | */ |
||
| 81 | public function __construct($consumerKey, $consumerSecret, $oauthToken = '', $oauthTokenSecret = '', $accountId = '', $sandbox = false) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param $consumerKey |
||
| 98 | * @param $consumerSecret |
||
| 99 | * @param $oauthToken |
||
| 100 | * @param $oauthTokenSecret |
||
| 101 | * @param string $accountId |
||
| 102 | * @param bool $sandbox |
||
| 103 | * @return static |
||
| 104 | */ |
||
| 105 | public static function init($consumerKey, $consumerSecret, $oauthToken = '', $oauthTokenSecret = '', $accountId = '', $sandbox = false) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return Account|Cursor |
||
| 116 | */ |
||
| 117 | public function getAccounts() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param string $oauthToken |
||
| 124 | * @param string $oauthTokenSecret |
||
| 125 | */ |
||
| 126 | public function setOauthToken($oauthToken, $oauthTokenSecret) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return string|null |
||
| 133 | */ |
||
| 134 | public function getLastApiPath() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return int |
||
| 141 | */ |
||
| 142 | public function getLastHttpCode() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | public function getLastXHeaders() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return array|object|null |
||
| 157 | */ |
||
| 158 | public function getLastBody() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Resets the last response cache. |
||
| 165 | */ |
||
| 166 | public function resetLastResponse() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Make URLs for user browser navigation. |
||
| 173 | * |
||
| 174 | * @param string $path |
||
| 175 | * @param array $parameters |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | public function url($path, array $parameters) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Make /oauth/* requests to the API. |
||
| 190 | * |
||
| 191 | * @param string $path |
||
| 192 | * @param array $parameters |
||
| 193 | * @return array |
||
| 194 | * @throws Exception |
||
| 195 | */ |
||
| 196 | public function oauth($path, array $parameters = []) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Make /oauth2/* requests to the API. |
||
| 216 | * |
||
| 217 | * @param string $path |
||
| 218 | * @param array $parameters |
||
| 219 | * |
||
| 220 | * @return array|object |
||
| 221 | */ |
||
| 222 | public function oauth2($path, array $parameters = []) |
||
| 236 | |||
| 237 | public function verifyCredentials($parameters = []) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Make GET requests to the API. |
||
| 244 | * |
||
| 245 | * @param string $path |
||
| 246 | * @param array $parameters |
||
| 247 | * |
||
| 248 | * @return Response |
||
| 249 | */ |
||
| 250 | public function get($path, array $parameters = []) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Make POST requests to the API. |
||
| 257 | * |
||
| 258 | * @param string $path |
||
| 259 | * @param array $parameters |
||
| 260 | * |
||
| 261 | * @param array $headers |
||
| 262 | * @return Response |
||
| 263 | */ |
||
| 264 | public function post($path, array $parameters = [], array $headers = []) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Make DELETE requests to the API. |
||
| 271 | * |
||
| 272 | * @param string $path |
||
| 273 | * @param array $parameters |
||
| 274 | * |
||
| 275 | * @return Response |
||
| 276 | */ |
||
| 277 | public function delete($path, array $parameters = []) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Make PUT requests to the API. |
||
| 284 | * |
||
| 285 | * @param string $path |
||
| 286 | * @param array $parameters |
||
| 287 | * |
||
| 288 | * @return Response |
||
| 289 | */ |
||
| 290 | public function put($path, array $parameters = [], array $headers = []) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Upload media to upload.twitter.com. |
||
| 297 | * |
||
| 298 | * @param array $parameters |
||
| 299 | * @param bool $chunked |
||
| 300 | * |
||
| 301 | * @return array|object |
||
| 302 | */ |
||
| 303 | public function upload(array $parameters = [], $chunked = false) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Private method to upload media (not chunked) to upload.twitter.com. |
||
| 314 | * |
||
| 315 | * @param string $path |
||
| 316 | * @param array $parameters |
||
| 317 | * |
||
| 318 | * @return array|object |
||
| 319 | */ |
||
| 320 | private function uploadMediaNotChunked($path, $parameters) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Private method to upload media (chunked) to upload.twitter.com. |
||
| 331 | * |
||
| 332 | * @param string $path |
||
| 333 | * @param array $parameters |
||
| 334 | * |
||
| 335 | * @return array|object |
||
| 336 | */ |
||
| 337 | private function uploadMediaChunked($path, $parameters) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param string $method |
||
| 406 | * @param string $host |
||
| 407 | * @param string $path |
||
| 408 | * @param array $parameters |
||
| 409 | * @param array $headers |
||
| 410 | * @return Response |
||
| 411 | * @throws BadRequest |
||
| 412 | * @throws Forbidden |
||
| 413 | * @throws NotAuthorized |
||
| 414 | * @throws NotFound |
||
| 415 | * @throws RateLimit |
||
| 416 | * @throws ServerError |
||
| 417 | * @throws ServiceUnavailable |
||
| 418 | */ |
||
| 419 | private function http($method, $host, $path, array $parameters, $headers = []) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param $response |
||
| 446 | * @throws BadRequest |
||
| 447 | * @throws Forbidden |
||
| 448 | * @throws NotAuthorized |
||
| 449 | * @throws NotFound |
||
| 450 | * @throws RateLimit |
||
| 451 | * @throws ServerError |
||
| 452 | * @throws ServiceUnavailable |
||
| 453 | */ |
||
| 454 | 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 | */ |
||
| 487 | private function oAuthRequest($url, $method, array $parameters, $headers = []) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Make an HTTP request. |
||
| 510 | * |
||
| 511 | * @param string $url |
||
| 512 | * @param string $method |
||
| 513 | * @param string $authorization |
||
| 514 | * @param array $postfields |
||
| 515 | * |
||
| 516 | * @param array $headers |
||
| 517 | * @return string |
||
| 518 | * @throws TwitterAdsException |
||
| 519 | */ |
||
| 520 | private function request($url, $method, $authorization, $postfields, $headers = []) |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Get the header info to store. |
||
| 591 | * |
||
| 592 | * @param string $header |
||
| 593 | * |
||
| 594 | * @return array |
||
| 595 | */ |
||
| 596 | private function parseHeaders($header) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Set API URLS. |
||
| 612 | */ |
||
| 613 | public function accessTokenURL() |
||
| 617 | |||
| 618 | public function authenticateURL() |
||
| 622 | |||
| 623 | public function authorizeURL() |
||
| 627 | |||
| 628 | public function requestTokenURL() |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Encode application authorization header with base64. |
||
| 635 | * |
||
| 636 | * @param Consumer $consumer |
||
| 637 | * |
||
| 638 | * @return string |
||
| 639 | */ |
||
| 640 | private function encodeAppAuthorization($consumer) |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Return current response. Allows inheritance. |
||
| 651 | * |
||
| 652 | * @return Response |
||
| 653 | */ |
||
| 654 | public function getResponse() |
||
| 658 | |||
| 659 | /** |
||
| 660 | * @return string |
||
| 661 | */ |
||
| 662 | public function getMethod() |
||
| 666 | |||
| 667 | /** |
||
| 668 | * @return string |
||
| 669 | */ |
||
| 670 | public function getResource() |
||
| 674 | |||
| 675 | /** |
||
| 676 | * @return string |
||
| 677 | */ |
||
| 678 | public function getAccountId() |
||
| 682 | |||
| 683 | /** |
||
| 684 | * @param string $accountId |
||
| 685 | */ |
||
| 686 | public function setAccountId($accountId) |
||
| 690 | |||
| 691 | View Code Duplication | public function getRequestToken($oauth_callback) |
|
| 700 | |||
| 701 | /** |
||
| 702 | * Exchange request token and secret for an access token and |
||
| 703 | * secret, to sign API calls. |
||
| 704 | * |
||
| 705 | * @param $oauth_verifier |
||
| 706 | * @return array ("oauth_token" => "the-access-token", |
||
| 707 | * "oauth_token_secret" => "the-access-secret", |
||
| 708 | * "user_id" => "9436992", |
||
| 709 | * "screen_name" => "abraham") |
||
| 710 | */ |
||
| 711 | View Code Duplication | public function getAccessToken($oauth_verifier) |
|
| 720 | |||
| 721 | // This function takes a input like a=b&a=c&d=e and returns the parsed |
||
| 722 | // parameters like this |
||
| 723 | // array('a' => array('b','c'), 'd' => 'e') |
||
| 724 | View Code Duplication | public static function parse_parameters($input) |
|
| 756 | |||
| 757 | public static function urlencode_rfc3986($input) |
||
| 771 | |||
| 772 | // This decode function isn't taking into consideration the above |
||
| 773 | // modifications to the encoding process. However, this method doesn't |
||
| 774 | // seem to be used anywhere so leaving it as is. |
||
| 775 | public static function urldecode_rfc3986($string) |
||
| 779 | |||
| 780 | /** |
||
| 781 | * Get the authorize URL. |
||
| 782 | * |
||
| 783 | * @param $token |
||
| 784 | * @param bool $sign_in_with_twitter |
||
| 785 | * @return string |
||
| 786 | */ |
||
| 787 | public function getAuthorizeURL($token, $sign_in_with_twitter = true) |
||
| 798 | } |
||
| 799 |
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: