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) |
||
411 | |||
412 | /** |
||
413 | * @param string $method |
||
414 | * @param string $host |
||
415 | * @param string $path |
||
416 | * @param array $parameters |
||
417 | * @param array $headers |
||
418 | * @return Response |
||
419 | * @throws BadRequest |
||
420 | * @throws Forbidden |
||
421 | * @throws NotAuthorized |
||
422 | * @throws NotFound |
||
423 | * @throws RateLimit |
||
424 | * @throws ServerError |
||
425 | * @throws ServiceUnavailable |
||
426 | */ |
||
427 | private function http($method, $host, $path, array $parameters, $headers = []) |
||
451 | |||
452 | /** |
||
453 | * @param $response |
||
454 | * @throws BadRequest |
||
455 | * @throws Forbidden |
||
456 | * @throws NotAuthorized |
||
457 | * @throws NotFound |
||
458 | * @throws RateLimit |
||
459 | * @throws ServerError |
||
460 | * @throws ServiceUnavailable |
||
461 | */ |
||
462 | public function manageErrors($response) |
||
483 | |||
484 | /** |
||
485 | * Format and sign an OAuth / API request. |
||
486 | * |
||
487 | * @param string $url |
||
488 | * @param string $method |
||
489 | * @param array $parameters |
||
490 | * |
||
491 | * @param array $headers |
||
492 | * @return string |
||
493 | * @throws TwitterAdsException |
||
494 | */ |
||
495 | private function oAuthRequest($url, $method, array $parameters, $headers = []) |
||
515 | |||
516 | /** |
||
517 | * Make an HTTP request. |
||
518 | * |
||
519 | * @param string $url |
||
520 | * @param string $method |
||
521 | * @param string $authorization |
||
522 | * @param array $postfields |
||
523 | * |
||
524 | * @param array $headers |
||
525 | * @return string |
||
526 | * @throws TwitterAdsException |
||
527 | */ |
||
528 | private function request($url, $method, $authorization, $postfields, $headers = []) |
||
596 | |||
597 | /** |
||
598 | * Get the header info to store. |
||
599 | * |
||
600 | * @param string $header |
||
601 | * |
||
602 | * @return array |
||
603 | */ |
||
604 | private function parseHeaders($header) |
||
617 | |||
618 | /** |
||
619 | * Set API URLS. |
||
620 | */ |
||
621 | public function accessTokenURL() |
||
625 | |||
626 | public function authenticateURL() |
||
630 | |||
631 | public function authorizeURL() |
||
635 | |||
636 | public function requestTokenURL() |
||
640 | |||
641 | /** |
||
642 | * Encode application authorization header with base64. |
||
643 | * |
||
644 | * @param Consumer $consumer |
||
645 | * |
||
646 | * @return string |
||
647 | */ |
||
648 | private function encodeAppAuthorization($consumer) |
||
656 | |||
657 | /** |
||
658 | * Return current response. Allows inheritance. |
||
659 | * |
||
660 | * @return Response |
||
661 | */ |
||
662 | public function getResponse() |
||
666 | |||
667 | /** |
||
668 | * @return string |
||
669 | */ |
||
670 | public function getMethod() |
||
674 | |||
675 | /** |
||
676 | * @return string |
||
677 | */ |
||
678 | public function getResource() |
||
682 | |||
683 | /** |
||
684 | * @return string |
||
685 | */ |
||
686 | public function getAccountId() |
||
690 | |||
691 | /** |
||
692 | * @param string $accountId |
||
693 | */ |
||
694 | public function setAccountId($accountId) |
||
698 | |||
699 | View Code Duplication | public function getRequestToken($oauth_callback) |
|
708 | |||
709 | /** |
||
710 | * Exchange request token and secret for an access token and |
||
711 | * secret, to sign API calls. |
||
712 | * |
||
713 | * @param $oauth_verifier |
||
714 | * @return array ("oauth_token" => "the-access-token", |
||
715 | * "oauth_token_secret" => "the-access-secret", |
||
716 | * "user_id" => "9436992", |
||
717 | * "screen_name" => "abraham") |
||
718 | */ |
||
719 | View Code Duplication | public function getAccessToken($oauth_verifier) |
|
728 | |||
729 | // This function takes a input like a=b&a=c&d=e and returns the parsed |
||
730 | // parameters like this |
||
731 | // array('a' => array('b','c'), 'd' => 'e') |
||
732 | View Code Duplication | public static function parse_parameters($input) |
|
764 | |||
765 | public static function urlencode_rfc3986($input) |
||
779 | |||
780 | // This decode function isn't taking into consideration the above |
||
781 | // modifications to the encoding process. However, this method doesn't |
||
782 | // seem to be used anywhere so leaving it as is. |
||
783 | public static function urldecode_rfc3986($string) |
||
787 | |||
788 | /** |
||
789 | * Get the authorize URL. |
||
790 | * |
||
791 | * @param $token |
||
792 | * @param bool $sign_in_with_twitter |
||
793 | * @return string |
||
794 | */ |
||
795 | public function getAuthorizeURL($token, $sign_in_with_twitter = true) |
||
806 | } |
||
807 |
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: