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 |
||
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 | * Set API URLS. |
||
81 | */ |
||
82 | public function accessTokenURL() |
||
86 | |||
87 | public function authenticateURL() |
||
91 | |||
92 | public function authorizeURL() |
||
96 | |||
97 | public function requestTokenURL() |
||
101 | |||
102 | /** |
||
103 | * @param string $accountId |
||
104 | * |
||
105 | * @return Account|Cursor |
||
106 | */ |
||
107 | public function getAccounts($accountId = '') |
||
113 | |||
114 | /** |
||
115 | * @param string $oauthToken |
||
116 | * @param string $oauthTokenSecret |
||
117 | */ |
||
118 | public function setOauthToken($oauthToken, $oauthTokenSecret) |
||
122 | |||
123 | /** |
||
124 | * @return string|null |
||
125 | */ |
||
126 | public function getLastApiPath() |
||
130 | |||
131 | /** |
||
132 | * @return int |
||
133 | */ |
||
134 | public function getLastHttpCode() |
||
138 | |||
139 | /** |
||
140 | * @return array |
||
141 | */ |
||
142 | public function getLastXHeaders() |
||
146 | |||
147 | /** |
||
148 | * @return array|object|null |
||
149 | */ |
||
150 | public function getLastBody() |
||
154 | |||
155 | /** |
||
156 | * Resets the last response cache. |
||
157 | */ |
||
158 | public function resetLastResponse() |
||
162 | |||
163 | /** |
||
164 | * Make URLs for user browser navigation. |
||
165 | * |
||
166 | * @param string $path |
||
167 | * @param array $parameters |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | public function url($path, array $parameters) |
||
179 | |||
180 | /** |
||
181 | * Make /oauth/* requests to the API. |
||
182 | * |
||
183 | * @param string $path |
||
184 | * @param array $parameters |
||
185 | * @return array |
||
186 | * @throws Exception |
||
187 | */ |
||
188 | public function oauth($path, array $parameters = []) |
||
205 | |||
206 | /** |
||
207 | * Make /oauth2/* requests to the API. |
||
208 | * |
||
209 | * @param string $path |
||
210 | * @param array $parameters |
||
211 | * |
||
212 | * @return array|object |
||
213 | */ |
||
214 | public function oauth2($path, array $parameters = []) |
||
228 | |||
229 | public function verifyCredentials($parameters = []) |
||
233 | |||
234 | /** |
||
235 | * Make GET requests to the API. |
||
236 | * |
||
237 | * @param string $path |
||
238 | * @param array $parameters |
||
239 | * |
||
240 | * @return Response |
||
241 | */ |
||
242 | public function get($path, array $parameters = []) |
||
246 | |||
247 | /** |
||
248 | * Make POST requests to the API. |
||
249 | * |
||
250 | * @param string $path |
||
251 | * @param array $parameters |
||
252 | * |
||
253 | * @param array $headers |
||
254 | * @return Response |
||
255 | */ |
||
256 | public function post($path, array $parameters = [], array $headers = []) |
||
260 | |||
261 | /** |
||
262 | * Make DELETE requests to the API. |
||
263 | * |
||
264 | * @param string $path |
||
265 | * @param array $parameters |
||
266 | * |
||
267 | * @return Response |
||
268 | */ |
||
269 | public function delete($path, array $parameters = []) |
||
273 | |||
274 | /** |
||
275 | * Make PUT requests to the API. |
||
276 | * |
||
277 | * @param string $path |
||
278 | * @param array $parameters |
||
279 | * |
||
280 | * @return Response |
||
281 | */ |
||
282 | public function put($path, array $parameters = [], array $headers = []) |
||
286 | |||
287 | /** |
||
288 | * Upload media to upload.twitter.com. |
||
289 | * |
||
290 | * @param array $parameters |
||
291 | * @param bool $chunked |
||
292 | * |
||
293 | * @return array|object |
||
294 | */ |
||
295 | public function upload(array $parameters = [], $chunked = false) |
||
303 | |||
304 | /** |
||
305 | * Private method to upload media (not chunked) to upload.twitter.com. |
||
306 | * |
||
307 | * @param string $path |
||
308 | * @param array $parameters |
||
309 | * |
||
310 | * @return array|object |
||
311 | */ |
||
312 | private function uploadMediaNotChunked($path, $parameters) |
||
320 | |||
321 | /** |
||
322 | * Private method to upload media (chunked) to upload.twitter.com. |
||
323 | * |
||
324 | * @param string $path |
||
325 | * @param array $parameters |
||
326 | * |
||
327 | * @return array|object |
||
328 | */ |
||
329 | private function uploadMediaChunked($path, $parameters) |
||
395 | |||
396 | /** |
||
397 | * @param string $method |
||
398 | * @param string $host |
||
399 | * @param string $path |
||
400 | * @param array $parameters |
||
401 | * @param array $headers |
||
402 | * @return Response |
||
403 | * @throws BadRequest |
||
404 | * @throws Forbidden |
||
405 | * @throws NotAuthorized |
||
406 | * @throws NotFound |
||
407 | * @throws RateLimit |
||
408 | * @throws ServerError |
||
409 | * @throws ServiceUnavailable |
||
410 | */ |
||
411 | private function http($method, $host, $path, array $parameters, $headers = []) |
||
435 | |||
436 | /** |
||
437 | * @param $response |
||
438 | * @throws BadRequest |
||
439 | * @throws Forbidden |
||
440 | * @throws NotAuthorized |
||
441 | * @throws NotFound |
||
442 | * @throws RateLimit |
||
443 | * @throws ServerError |
||
444 | * @throws ServiceUnavailable |
||
445 | */ |
||
446 | public function manageErrors($response){ |
||
466 | |||
467 | /** |
||
468 | * Format and sign an OAuth / API request. |
||
469 | * |
||
470 | * @param string $url |
||
471 | * @param string $method |
||
472 | * @param array $parameters |
||
473 | * |
||
474 | * @param array $headers |
||
475 | * @return string |
||
476 | * @throws TwitterAdsException |
||
477 | * @throws TwitterOAuthException |
||
478 | */ |
||
479 | private function oAuthRequest($url, $method, array $parameters, $headers = []) |
||
499 | |||
500 | /** |
||
501 | * Make an HTTP request. |
||
502 | * |
||
503 | * @param string $url |
||
504 | * @param string $method |
||
505 | * @param string $authorization |
||
506 | * @param array $postfields |
||
507 | * |
||
508 | * @param array $headers |
||
509 | * @return string |
||
510 | * @throws TwitterAdsException |
||
511 | */ |
||
512 | private function request($url, $method, $authorization, $postfields, $headers = []) |
||
580 | |||
581 | /** |
||
582 | * Get the header info to store. |
||
583 | * |
||
584 | * @param string $header |
||
585 | * |
||
586 | * @return array |
||
587 | */ |
||
588 | private function parseHeaders($header) |
||
601 | |||
602 | /** |
||
603 | * Encode application authorization header with base64. |
||
604 | * |
||
605 | * @param Consumer $consumer |
||
606 | * |
||
607 | * @return string |
||
608 | */ |
||
609 | private function encodeAppAuthorization($consumer) |
||
617 | |||
618 | View Code Duplication | public function getRequestToken($oauth_callback) |
|
627 | |||
628 | /** |
||
629 | * Exchange request token and secret for an access token and |
||
630 | * secret, to sign API calls. |
||
631 | * |
||
632 | * @param $oauth_verifier |
||
633 | * @return array ("oauth_token" => "the-access-token", |
||
634 | * "oauth_token_secret" => "the-access-secret", |
||
635 | * "user_id" => "9436992", |
||
636 | * "screen_name" => "abraham") |
||
637 | */ |
||
638 | View Code Duplication | public function getAccessToken($oauth_verifier) |
|
647 | |||
648 | // This function takes a input like a=b&a=c&d=e and returns the parsed |
||
649 | // parameters like this |
||
650 | // array('a' => array('b','c'), 'd' => 'e') |
||
651 | View Code Duplication | public static function parse_parameters($input) |
|
683 | |||
684 | public static function urlencode_rfc3986($input) |
||
698 | |||
699 | // This decode function isn't taking into consideration the above |
||
700 | // modifications to the encoding process. However, this method doesn't |
||
701 | // seem to be used anywhere so leaving it as is. |
||
702 | public static function urldecode_rfc3986($string) |
||
706 | |||
707 | /** |
||
708 | * Get the authorize URL. |
||
709 | * |
||
710 | * @param $token |
||
711 | * @param bool $sign_in_with_twitter |
||
712 | * @return string |
||
713 | */ |
||
714 | public function getAuthorizeURL($token, $sign_in_with_twitter = true) |
||
725 | |||
726 | /** |
||
727 | * Return current response. Allows inheritance. |
||
728 | * |
||
729 | * @return Response |
||
730 | */ |
||
731 | public function getResponse() |
||
735 | |||
736 | /** |
||
737 | * @return string |
||
738 | */ |
||
739 | public function getMethod() |
||
743 | |||
744 | /** |
||
745 | * @return string |
||
746 | */ |
||
747 | public function getResource() |
||
751 | } |
||
752 |
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: