Complex classes like TwitterOAuth 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 TwitterOAuth, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class TwitterOAuth extends Config |
||
17 | { |
||
18 | const API_VERSION = '1.1'; |
||
19 | const API_HOST = 'https://api.twitter.com'; |
||
20 | const UPLOAD_HOST = 'https://upload.twitter.com'; |
||
21 | const UPLOAD_CHUNK = 40960; // 1024 * 40 |
||
22 | |||
23 | /** @var Response details about the result of the last request */ |
||
24 | private $response; |
||
25 | /** @var string|null Application bearer token */ |
||
26 | private $bearer; |
||
27 | /** @var Consumer Twitter application details */ |
||
28 | private $consumer; |
||
29 | /** @var Token|null User access token details */ |
||
30 | private $token; |
||
31 | /** @var HmacSha1 OAuth 1 signature type used by Twitter */ |
||
32 | private $signatureMethod; |
||
33 | |||
34 | /** |
||
35 | * Constructor |
||
36 | * |
||
37 | * @param string $consumerKey The Application Consumer Key |
||
38 | * @param string $consumerSecret The Application Consumer Secret |
||
39 | * @param string|null $oauthToken The Client Token (optional) |
||
40 | * @param string|null $oauthTokenSecret The Client Token Secret (optional) |
||
41 | */ |
||
42 | public function __construct($consumerKey, $consumerSecret, $oauthToken = null, $oauthTokenSecret = null) |
||
43 | { |
||
44 | $this->resetLastResponse(); |
||
45 | $this->signatureMethod = new HmacSha1(); |
||
46 | $this->consumer = new Consumer($consumerKey, $consumerSecret); |
||
47 | if (!empty($oauthToken) && !empty($oauthTokenSecret)) { |
||
48 | $this->token = new Token($oauthToken, $oauthTokenSecret); |
||
49 | } |
||
50 | if (empty($oauthToken) && !empty($oauthTokenSecret)) { |
||
51 | $this->bearer = $oauthTokenSecret; |
||
52 | } |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param string $oauthToken |
||
57 | * @param string $oauthTokenSecret |
||
58 | */ |
||
59 | public function setOauthToken($oauthToken, $oauthTokenSecret) |
||
63 | |||
64 | /** |
||
65 | * @return string|null |
||
66 | */ |
||
67 | public function getLastApiPath() |
||
71 | |||
72 | /** |
||
73 | * @return int |
||
74 | */ |
||
75 | public function getLastHttpCode() |
||
79 | |||
80 | /** |
||
81 | * @return array |
||
82 | */ |
||
83 | public function getLastXHeaders() |
||
87 | |||
88 | /** |
||
89 | * @return array|object|null |
||
90 | */ |
||
91 | public function getLastBody() |
||
95 | |||
96 | /** |
||
97 | * Resets the last response cache. |
||
98 | */ |
||
99 | public function resetLastResponse() |
||
103 | |||
104 | /** |
||
105 | * Make URLs for user browser navigation. |
||
106 | * |
||
107 | * @param string $path |
||
108 | * @param array $parameters |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | public function url($path, array $parameters) |
||
113 | { |
||
114 | $this->resetLastResponse(); |
||
115 | $this->response->setApiPath($path); |
||
116 | $query = http_build_query($parameters); |
||
117 | return sprintf('%s/%s?%s', self::API_HOST, $path, $query); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Make /oauth/* requests to the API. |
||
122 | * |
||
123 | * @param string $path |
||
124 | * @param array $parameters |
||
125 | * |
||
126 | * @return array |
||
127 | * @throws TwitterOAuthException |
||
128 | */ |
||
129 | public function oauth($path, array $parameters = []) |
||
130 | { |
||
131 | $response = []; |
||
132 | $this->resetLastResponse(); |
||
133 | $this->response->setApiPath($path); |
||
134 | $url = sprintf('%s/%s', self::API_HOST, $path); |
||
135 | $result = $this->oAuthRequest($url, 'POST', $parameters); |
||
136 | |||
137 | if ($this->getLastHttpCode() != 200) { |
||
138 | throw new TwitterOAuthException($result); |
||
139 | } |
||
140 | |||
141 | parse_str($result, $response); |
||
142 | $this->response->setBody($response); |
||
|
|||
143 | |||
144 | return $response; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Make /oauth2/* requests to the API. |
||
149 | * |
||
150 | * @param string $path |
||
151 | * @param array $parameters |
||
152 | * |
||
153 | * @return array|object |
||
154 | */ |
||
155 | public function oauth2($path, array $parameters = []) |
||
156 | { |
||
157 | $method = 'POST'; |
||
158 | $this->resetLastResponse(); |
||
159 | $this->response->setApiPath($path); |
||
160 | $url = sprintf('%s/%s', self::API_HOST, $path); |
||
161 | $request = Request::fromConsumerAndToken($this->consumer, $this->token, $method, $url, $parameters); |
||
162 | $authorization = 'Authorization: Basic ' . $this->encodeAppAuthorization($this->consumer); |
||
163 | $result = $this->request($request->getNormalizedHttpUrl(), $method, $authorization, $parameters); |
||
164 | $response = JsonDecoder::decode($result, $this->decodeJsonAsArray); |
||
165 | $this->response->setBody($response); |
||
166 | return $response; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Make GET requests to the API. |
||
171 | * |
||
172 | * @param string $path |
||
173 | * @param array $parameters |
||
174 | * |
||
175 | * @return array|object |
||
176 | */ |
||
177 | public function get($path, array $parameters = []) |
||
178 | { |
||
179 | return $this->http('GET', self::API_HOST, $path, $parameters); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Make POST requests to the API. |
||
184 | * |
||
185 | * @param string $path |
||
186 | * @param array $parameters |
||
187 | * |
||
188 | * @return array|object |
||
189 | */ |
||
190 | public function post($path, array $parameters = []) |
||
191 | { |
||
192 | return $this->http('POST', self::API_HOST, $path, $parameters); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Make DELETE requests to the API. |
||
197 | * |
||
198 | * @param string $path |
||
199 | * @param array $parameters |
||
200 | * |
||
201 | * @return array|object |
||
202 | */ |
||
203 | public function delete($path, array $parameters = []) |
||
207 | |||
208 | /** |
||
209 | * Make PUT requests to the API. |
||
210 | * |
||
211 | * @param string $path |
||
212 | * @param array $parameters |
||
213 | * |
||
214 | * @return array|object |
||
215 | */ |
||
216 | public function put($path, array $parameters = []) |
||
220 | |||
221 | /** |
||
222 | * Upload media to upload.twitter.com. |
||
223 | * |
||
224 | * @param string $path |
||
225 | * @param array $parameters |
||
226 | * @param boolean $chunked |
||
227 | * |
||
228 | * @return array|object |
||
229 | */ |
||
230 | public function upload($path, array $parameters = [], $chunked = false) |
||
238 | |||
239 | /** |
||
240 | * Private method to upload media (not chunked) to upload.twitter.com. |
||
241 | * |
||
242 | * @param string $path |
||
243 | * @param array $parameters |
||
244 | * |
||
245 | * @return array|object |
||
246 | */ |
||
247 | private function uploadMediaNotChunked($path, array $parameters) |
||
254 | |||
255 | /** |
||
256 | * Checks to see whether the upload was successful or not |
||
257 | * |
||
258 | * @param $media_id |
||
259 | * @return array|object |
||
260 | */ |
||
261 | public function getUploadStatus($media_id) |
||
268 | |||
269 | /** |
||
270 | * Private method to upload media (chunked) to upload.twitter.com. |
||
271 | * |
||
272 | * @param string $path |
||
273 | * @param array $parameters |
||
274 | * |
||
275 | * @return array|object |
||
276 | */ |
||
277 | private function uploadMediaChunked($path, $parameters) |
||
311 | |||
312 | /** |
||
313 | * @param string $method |
||
314 | * @param string $host |
||
315 | * @param string $path |
||
316 | * @param array $parameters |
||
317 | * |
||
318 | * @return array|object |
||
319 | */ |
||
320 | private function http($method, $host, $path, array $parameters) |
||
330 | |||
331 | /** |
||
332 | * Format and sign an OAuth / API request |
||
333 | * |
||
334 | * @param string $url |
||
335 | * @param string $method |
||
336 | * @param array $parameters |
||
337 | * |
||
338 | * @return string |
||
339 | * @throws TwitterOAuthException |
||
340 | */ |
||
341 | private function oAuthRequest($url, $method, array $parameters) |
||
356 | |||
357 | /** |
||
358 | * Make an HTTP request |
||
359 | * |
||
360 | * @param string $url |
||
361 | * @param string $method |
||
362 | * @param string $authorization |
||
363 | * @param array $postfields |
||
364 | * |
||
365 | * @return string |
||
366 | * @throws TwitterOAuthException |
||
367 | */ |
||
368 | private function request($url, $method, $authorization, array $postfields) |
||
436 | |||
437 | /** |
||
438 | * Get the header info to store. |
||
439 | * |
||
440 | * @param string $header |
||
441 | * |
||
442 | * @return array |
||
443 | */ |
||
444 | private function parseHeaders($header) |
||
456 | |||
457 | /** |
||
458 | * Encode application authorization header with base64. |
||
459 | * |
||
460 | * @param Consumer $consumer |
||
461 | * |
||
462 | * @return string |
||
463 | */ |
||
464 | private function encodeAppAuthorization(Consumer $consumer) |
||
470 | } |
||
471 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.