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:
| 1 | <?php |
||
| 17 | class TwitterOAuth |
||
| 18 | { |
||
| 19 | /* Contains the last HTTP status code returned. */ |
||
| 20 | public $http_code; |
||
| 21 | /* Contains the last API call. */ |
||
| 22 | public $url; |
||
| 23 | /* Set up the API root URL. */ |
||
| 24 | public $host = "https://api.twitter.com/1.1/"; |
||
| 25 | /* Set timeout default. */ |
||
| 26 | public $timeout = 30; |
||
| 27 | /* Set connect timeout. */ |
||
| 28 | public $connecttimeout = 30; |
||
| 29 | /* Verify SSL Cert. */ |
||
| 30 | public $ssl_verifypeer = false; |
||
| 31 | /* Respons format. */ |
||
| 32 | public $format = 'json'; |
||
| 33 | /* Decode returned json data. */ |
||
| 34 | public $decode_json = true; |
||
| 35 | /* Contains the last HTTP headers returned. */ |
||
| 36 | public $http_info; |
||
| 37 | /* Set the useragnet. */ |
||
| 38 | public $useragent = 'TwitterOAuth v0.2.0-beta2'; |
||
| 39 | /* Immediately retry the API call if the response was not successful. */ |
||
| 40 | //public $retry = TRUE; |
||
| 41 | |||
| 42 | |||
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * Set API URLS |
||
| 47 | */ |
||
| 48 | function accessTokenURL() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Debug helpers |
||
| 67 | */ |
||
| 68 | function lastStatusCode() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * construct TwitterOAuth object |
||
| 79 | */ |
||
| 80 | function __construct($consumer_key, $consumer_secret, $oauth_token = null, $oauth_token_secret = null) |
||
| 90 | |||
| 91 | |||
| 92 | /** |
||
| 93 | * Get a request_token from Twitter |
||
| 94 | * |
||
| 95 | * @returns a key/value array containing oauth_token and oauth_token_secret |
||
| 96 | */ |
||
| 97 | View Code Duplication | function getRequestToken($oauth_callback) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Get the authorize URL |
||
| 109 | * |
||
| 110 | * @returns a string |
||
| 111 | */ |
||
| 112 | function getAuthorizeURL($token, $sign_in_with_twitter = true) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Exchange request token and secret for an access token and |
||
| 126 | * secret, to sign API calls. |
||
| 127 | * |
||
| 128 | * @returns array("oauth_token" => "the-access-token", |
||
| 129 | * "oauth_token_secret" => "the-access-secret", |
||
| 130 | * "user_id" => "9436992", |
||
| 131 | * "screen_name" => "abraham") |
||
| 132 | */ |
||
| 133 | View Code Duplication | function getAccessToken($oauth_verifier) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * One time exchange of username and password for access token and secret. |
||
| 145 | * |
||
| 146 | * @returns array("oauth_token" => "the-access-token", |
||
| 147 | * "oauth_token_secret" => "the-access-secret", |
||
| 148 | * "user_id" => "9436992", |
||
| 149 | * "screen_name" => "abraham", |
||
| 150 | * "x_auth_expires" => "0") |
||
| 151 | */ |
||
| 152 | function getXAuthToken($username, $password) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * GET wrapper for oAuthRequest. |
||
| 166 | */ |
||
| 167 | View Code Duplication | function get($url, $parameters = array()) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * POST wrapper for oAuthRequest. |
||
| 178 | */ |
||
| 179 | View Code Duplication | function post($url, $parameters = array()) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * DELETE wrapper for oAuthReqeust. |
||
| 190 | */ |
||
| 191 | View Code Duplication | function delete($url, $parameters = array()) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Format and sign an OAuth / API request |
||
| 202 | */ |
||
| 203 | function oAuthRequest($url, $method, $parameters) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Make an HTTP request |
||
| 220 | * |
||
| 221 | * @return API results |
||
| 222 | */ |
||
| 223 | function http($url, $method, $postfields = null) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Get the header info to store. |
||
| 262 | */ |
||
| 263 | function getHeader($ch, $header) |
||
| 273 | } |
||
| 274 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.