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 | * @param string $accountId |
||
81 | * |
||
82 | * @return Account|Cursor |
||
83 | */ |
||
84 | public function getAccounts($accountId = '') |
||
90 | |||
91 | /** |
||
92 | * @param string $oauthToken |
||
93 | * @param string $oauthTokenSecret |
||
94 | */ |
||
95 | public function setOauthToken($oauthToken, $oauthTokenSecret) |
||
99 | |||
100 | /** |
||
101 | * @return string|null |
||
102 | */ |
||
103 | public function getLastApiPath() |
||
107 | |||
108 | /** |
||
109 | * @return int |
||
110 | */ |
||
111 | public function getLastHttpCode() |
||
115 | |||
116 | /** |
||
117 | * @return array |
||
118 | */ |
||
119 | public function getLastXHeaders() |
||
123 | |||
124 | /** |
||
125 | * @return array|object|null |
||
126 | */ |
||
127 | public function getLastBody() |
||
131 | |||
132 | /** |
||
133 | * Resets the last response cache. |
||
134 | */ |
||
135 | public function resetLastResponse() |
||
139 | |||
140 | /** |
||
141 | * Make URLs for user browser navigation. |
||
142 | * |
||
143 | * @param string $path |
||
144 | * @param array $parameters |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | public function url($path, array $parameters) |
||
156 | |||
157 | /** |
||
158 | * Make /oauth/* requests to the API. |
||
159 | * |
||
160 | * @param string $path |
||
161 | * @param array $parameters |
||
162 | * @return array |
||
163 | * @throws Exception |
||
164 | */ |
||
165 | public function oauth($path, array $parameters = []) |
||
182 | |||
183 | /** |
||
184 | * Make /oauth2/* requests to the API. |
||
185 | * |
||
186 | * @param string $path |
||
187 | * @param array $parameters |
||
188 | * |
||
189 | * @return array|object |
||
190 | */ |
||
191 | public function oauth2($path, array $parameters = []) |
||
205 | |||
206 | public function verifyCredentials($parameters = []) |
||
210 | |||
211 | /** |
||
212 | * Make GET requests to the API. |
||
213 | * |
||
214 | * @param string $path |
||
215 | * @param array $parameters |
||
216 | * |
||
217 | * @return Response |
||
218 | */ |
||
219 | public function get($path, array $parameters = []) |
||
223 | |||
224 | /** |
||
225 | * Make POST requests to the API. |
||
226 | * |
||
227 | * @param string $path |
||
228 | * @param array $parameters |
||
229 | * |
||
230 | * @param array $headers |
||
231 | * @return Response |
||
232 | */ |
||
233 | public function post($path, array $parameters = [], array $headers = []) |
||
237 | |||
238 | /** |
||
239 | * Make DELETE requests to the API. |
||
240 | * |
||
241 | * @param string $path |
||
242 | * @param array $parameters |
||
243 | * |
||
244 | * @return Response |
||
245 | */ |
||
246 | public function delete($path, array $parameters = []) |
||
250 | |||
251 | /** |
||
252 | * Make PUT requests to the API. |
||
253 | * |
||
254 | * @param string $path |
||
255 | * @param array $parameters |
||
256 | * |
||
257 | * @return Response |
||
258 | */ |
||
259 | public function put($path, array $parameters = [], array $headers = []) |
||
263 | |||
264 | /** |
||
265 | * Upload media to upload.twitter.com. |
||
266 | * |
||
267 | * @param array $parameters |
||
268 | * @param bool $chunked |
||
269 | * |
||
270 | * @return array|object |
||
271 | */ |
||
272 | public function upload(array $parameters = [], $chunked = false) |
||
280 | |||
281 | /** |
||
282 | * Private method to upload media (not chunked) to upload.twitter.com. |
||
283 | * |
||
284 | * @param string $path |
||
285 | * @param array $parameters |
||
286 | * |
||
287 | * @return array|object |
||
288 | */ |
||
289 | private function uploadMediaNotChunked($path, $parameters) |
||
297 | |||
298 | /** |
||
299 | * Private method to upload media (chunked) to upload.twitter.com. |
||
300 | * |
||
301 | * @param string $path |
||
302 | * @param array $parameters |
||
303 | * |
||
304 | * @return array|object |
||
305 | */ |
||
306 | private function uploadMediaChunked($path, $parameters) |
||
349 | |||
350 | /** |
||
351 | * @param string $method |
||
352 | * @param string $host |
||
353 | * @param string $path |
||
354 | * @param array $parameters |
||
355 | * @param array $headers |
||
356 | * @return Response |
||
357 | * @throws BadRequest |
||
358 | * @throws Forbidden |
||
359 | * @throws NotAuthorized |
||
360 | * @throws NotFound |
||
361 | * @throws RateLimit |
||
362 | * @throws ServerError |
||
363 | * @throws ServiceUnavailable |
||
364 | */ |
||
365 | private function http($method, $host, $path, array $parameters, $headers = []) |
||
389 | |||
390 | /** |
||
391 | * @param $response |
||
392 | * @throws BadRequest |
||
393 | * @throws Forbidden |
||
394 | * @throws NotAuthorized |
||
395 | * @throws NotFound |
||
396 | * @throws RateLimit |
||
397 | * @throws ServerError |
||
398 | * @throws ServiceUnavailable |
||
399 | */ |
||
400 | public function manageErrors($response){ |
||
420 | |||
421 | /** |
||
422 | * Format and sign an OAuth / API request. |
||
423 | * |
||
424 | * @param string $url |
||
425 | * @param string $method |
||
426 | * @param array $parameters |
||
427 | * |
||
428 | * @param array $headers |
||
429 | * @return string |
||
430 | * @throws TwitterAdsException |
||
431 | * @throws TwitterOAuthException |
||
432 | */ |
||
433 | private function oAuthRequest($url, $method, array $parameters, $headers = []) |
||
453 | |||
454 | /** |
||
455 | * Make an HTTP request. |
||
456 | * |
||
457 | * @param string $url |
||
458 | * @param string $method |
||
459 | * @param string $authorization |
||
460 | * @param array $postfields |
||
461 | * |
||
462 | * @param array $headers |
||
463 | * @return string |
||
464 | * @throws TwitterAdsException |
||
465 | */ |
||
466 | private function request($url, $method, $authorization, $postfields, $headers = []) |
||
534 | |||
535 | /** |
||
536 | * Get the header info to store. |
||
537 | * |
||
538 | * @param string $header |
||
539 | * |
||
540 | * @return array |
||
541 | */ |
||
542 | private function parseHeaders($header) |
||
555 | |||
556 | /** |
||
557 | * Encode application authorization header with base64. |
||
558 | * |
||
559 | * @param Consumer $consumer |
||
560 | * |
||
561 | * @return string |
||
562 | */ |
||
563 | private function encodeAppAuthorization($consumer) |
||
571 | |||
572 | /** |
||
573 | * Return current response. Allows inheritance. |
||
574 | * |
||
575 | * @return Response |
||
576 | */ |
||
577 | public function getResponse() |
||
581 | |||
582 | /** |
||
583 | * @return string |
||
584 | */ |
||
585 | public function getMethod() |
||
589 | |||
590 | /** |
||
591 | * @return string |
||
592 | */ |
||
593 | public function getResource() |
||
597 | } |
||
598 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.