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 |
||
15 | class twitteroauth |
||
16 | { |
||
17 | /* Contains the last HTTP status code returned. */ |
||
18 | public $http_code; |
||
19 | /* Contains the last API call. */ |
||
20 | public $url; |
||
21 | /* Set up the API root URL. */ |
||
22 | public $host = 'https://api.twitter.com/1.1/'; |
||
23 | /* Set timeout default. */ |
||
24 | public $timeout = 30; |
||
25 | /* Set connect timeout. */ |
||
26 | public $connecttimeout = 30; |
||
27 | /* Verify SSL Cert. */ |
||
28 | public $ssl_verifypeer = false; |
||
29 | /* Respons format. */ |
||
30 | public $format = 'json'; |
||
31 | /* Decode returned json data. */ |
||
32 | public $decode_json = true; |
||
33 | /* Contains the last HTTP headers returned. */ |
||
34 | public $http_info; |
||
35 | /* Set the useragnet. */ |
||
36 | public $useragent = 'TwitterOAuth v0.2.0-beta2'; |
||
37 | /* Immediately retry the API call if the response was not successful. */ |
||
38 | //public $retry = TRUE; |
||
39 | |||
40 | /** |
||
41 | * Set API URLS. |
||
42 | */ |
||
43 | public function accessTokenURL() |
||
47 | |||
48 | public function authenticateURL() |
||
52 | |||
53 | public function authorizeURL() |
||
57 | |||
58 | public function requestTokenURL() |
||
62 | |||
63 | /** |
||
64 | * Debug helpers. |
||
65 | */ |
||
66 | public function lastStatusCode() |
||
70 | |||
71 | public function lastAPICall() |
||
75 | |||
76 | /** |
||
77 | * construct TwitterOAuth object. |
||
78 | */ |
||
79 | public function __construct($consumer_key, $consumer_secret, $oauth_token = null, $oauth_token_secret = null) |
||
89 | |||
90 | /** |
||
91 | * Get a request_token from Twitter. |
||
92 | * |
||
93 | * @returns a key/value array containing oauth_token and oauth_token_secret |
||
94 | */ |
||
95 | View Code Duplication | public function getRequestToken($oauth_callback) |
|
105 | |||
106 | /** |
||
107 | * Get the authorize URL. |
||
108 | * |
||
109 | * @returns a string |
||
110 | */ |
||
111 | public function getAuthorizeURL($token, $sign_in_with_twitter = true) |
||
122 | |||
123 | /** |
||
124 | * Exchange request token and secret for an access token and |
||
125 | * secret, to sign API calls. |
||
126 | * |
||
127 | * @returns array("oauth_token" => "the-access-token", |
||
128 | * "oauth_token_secret" => "the-access-secret", |
||
129 | * "user_id" => "9436992", |
||
130 | * "screen_name" => "abraham") |
||
131 | */ |
||
132 | View Code Duplication | public 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 | public function getXAuthToken($username, $password) |
||
164 | |||
165 | /** |
||
166 | * GET wrapper for oAuthRequest. |
||
167 | */ |
||
168 | View Code Duplication | public function get($url, $parameters = []) |
|
177 | |||
178 | /** |
||
179 | * POST wrapper for oAuthRequest. |
||
180 | */ |
||
181 | View Code Duplication | public function post($url, $parameters = []) |
|
190 | |||
191 | /** |
||
192 | * DELETE wrapper for oAuthReqeust. |
||
193 | */ |
||
194 | View Code Duplication | public function delete($url, $parameters = []) |
|
203 | |||
204 | /** |
||
205 | * Format and sign an OAuth / API request. |
||
206 | */ |
||
207 | public function oAuthRequest($url, $method, $parameters) |
||
221 | |||
222 | /** |
||
223 | * Make an HTTP request. |
||
224 | * |
||
225 | * @return API results |
||
226 | */ |
||
227 | public function http($url, $method, $postfields = null) |
||
264 | |||
265 | /** |
||
266 | * Get the header info to store. |
||
267 | */ |
||
268 | public function getHeader($ch, $header) |
||
279 | } |
||
280 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: